¿Crees que entiendes la gravedad? Esta página web te permitirá jugar con la gravedad e imaginarte como un ser todopoderoso capaz de crear estrellas de la nada. Este proyecto creado en Codepen nos permite añadir puntos gravitacionales con un simple click que afectarán a las partículas presentes.
|
etiquetas: gravedad , particula , interacción , experimento , estrellas , código , javascript
www.meneame.net/story/simulador-gravedad
Edito: también podéis entrar aquí: codepen.io/anon/pen/Wbgdxg
// Start Update
var loop = function() {
var i, len, g, p;
context.save();
context.fillStyle = BACKGROUND_COLOR;
context.fillRect(0, 0, screenWidth, screenHeight);
context.fillStyle = grad;
context.fillRect(0, 0, screenWidth, screenHeight);
context.restore();
for (i = 0, len = gravities.length; i < len; i++) {
g = gravities[i];
if (g.dragging) g.drag(mouse);
g.render(context);
if (g.destroyed) {
gravities.splice(i, 1);
len--;
i--;
}
}
bufferCtx.save();
bufferCtx.globalCompositeOperation = 'destination-out';
bufferCtx.globalAlpha = 0.35;
bufferCtx.fillRect(0, 0, screenWidth, screenHeight);
bufferCtx.restore();
// パーティクルをバッファに描画
// for (i = 0, len = particles.length; i < len; i++) {
// particles[i].render(bufferCtx);
// }
len = particles.length;
bufferCtx.save();
bufferCtx.lineCap = bufferCtx.lineJoin = 'round';
bufferCtx.lineWidth = PARTICLE_RADIUS * 2;
for (i = 0; i < len; i++) {
p = particles[i];
p.update();
var tono = p._speed.x * p._speed.x + p._speed.y * p._speed.y;
tono = tono.toFixed(0) * 2;
if (tono > 360) tono = 360;
var lum = Math.abs(tono - 180) / 3.6;
lum = 100 - lum.toFixed(0);
/*var rojo = 255 - tono;
var verde = Math.abs(tono - 128) * 2;
var azul = tono
if(i == 18)
document.getElementById('aaaa').innerHTML = p._speed.x * p._speed.x + p._speed.y * p._speed.y;;*/
bufferCtx.beginPath();
bufferCtx.moveTo(p.x, p.y);
bufferCtx.fillStyle = 'hsl(' + tono + ', 100%, ' + lum +'%)';
//'rgb(' + rojo + ',' + verde + ',' + azul + ')';
bufferCtx.strokeStyle = 'hsl(' + tono + ', 100%, ' + lum +'%)';
bufferCtx.lineTo(p._latest.x, p._latest.y);
bufferCtx.stroke();
}
/* bufferCtx.fillStyle = bufferCtx.strokeStyle = 'hsl(' + tono + ', 100%, 50%)';
bufferCtx.moveTo(400, 100);
bufferCtx.lineTo(50,40);
bufferCtx.stroke();*/
for (i = 0; i < len; i++) {
p = particles[i];
var tono = p._speed.x * p._speed.x + p._speed.y * p._speed.y;
tono = tono.toFixed(0) * 2;
if (tono > 360) tono = 360;
var lum = Math.abs(tono - 180) / 3.6;
lum = 100 - lum.toFixed(0);
/*var rojo = 255 - tono;
var verde = Math.abs(tono - 128) * 2;
var azul = tono;*/
bufferCtx.beginPath();
bufferCtx.moveTo(p.x, p.y);
bufferCtx.fillStyle = bufferCtx.strokeStyle ='hsl(' + tono + ', 100%, 50%)';
bufferCtx.arc(p.x, p.y, p.radius, 0, Math.PI * 2, false);
bufferCtx.fill();
}
bufferCtx.restore();
context.drawImage(bufferCvs, 0, 0);
// バッファをキャンバスに描画
requestAnimationFrame(loop);
};
loop();
})();
Así que os dejo esta otra versión:
codepen.io/anon/pen/qEMpyr
Ahora las partículas se ponen rojas cuando se alejan de un observador situado a la derecha, y se ponen moradas cuando el observador está a la izquierda.
www.giantbomb.com/inversion/3030-29087/