Updated version of gravity submission. Click to add a dot, the size is determined by amount of time the mouse was down, position is determined by where you click and initial velocity is the distance f…
var a=document.getElementById("c");document.body.setAttribute("style","margin:0;left:0;top:0;padding:0");a.width=innerWidth;a.height=innerHeight;var c=a.getContext("2d"),f=[[300,100,10,-0.2,0],[300,300,10,0.2,0]],h=-1; setInterval(function(){if(h>0)f[h][2]+=0.1;c.clearRect(0,0,innerWidth,innerHeight);for(var d=f.length,b=d,e;b--;){for(e=d;e--;)if(e!=b&&b!=h&&e!=h){var i=f[e][0]-f[b][0],j=f[e][1]-f[b][1],g=i*i+j*j,k=Math.sqrt(g);g=f[e][2]/g;f[b][3]+=i/k*g;f[b][4]+=j/k*g}f[b][0]+=f[b][3];f[b][1]+=f[b][4];c.fillStyle="hsl("+Math.min(300,Math.floor(50*Math.sqrt(f[b][3]*f[b][3]+f[b][4]*f[b][4])))+",100%,40%)";c.beginPath();c.arc(f[b][0],f[b][1],f[b][2],0,Math.PI*2,true);c.fill()}},0); document.body.onmousedown=function(d){h=f.length;f.push([d.clientX,d.clientY,2,0,0]);return false};document.body.onmouseup=function(d){f[h][3]=(d.clientX-f[h][0])/100;f[h][4]=(d.clientY-f[h][1])/100;h=-1;return false}
dmFyIGE9ZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoImMiKTtkb2N1bWVudC5ib2R5LnNldEF0dHJpYnV0ZSgic3R5bGUiLCJtYXJnaW46MDtsZWZ0OjA7dG9wOjA7cGFkZGluZzowIik7YS53aWR0aD1pbm5lcldpZHRoO2EuaGVpZ2h0PWlubmVySGVpZ2h0O3ZhciBjPWEuZ2V0Q29udGV4dCgiMmQiKSxmPVtbMzAwLDEwMCwxMCwtMC4yLDBdLFszMDAsMzAwLDEwLDAuMiwwXV0saD0tMTsgc2V0SW50ZXJ2YWwoZnVuY3Rpb24oKXtpZihoPjApZltoXVsyXSs9MC4xO2MuY2xlYXJSZWN0KDAsMCxpbm5lcldpZHRoLGlubmVySGVpZ2h0KTtmb3IodmFyIGQ9Zi5sZW5ndGgsYj1kLGU7Yi0tOyl7Zm9yKGU9ZDtlLS07KWlmKGUhPWImJmIhPWgmJmUhPWgpe3ZhciBpPWZbZV1bMF0tZltiXVswXSxqPWZbZV1bMV0tZltiXVsxXSxnPWkqaStqKmosaz1NYXRoLnNxcnQoZyk7Zz1mW2VdWzJdL2c7ZltiXVszXSs9aS9rKmc7ZltiXVs0XSs9ai9rKmd9ZltiXVswXSs9ZltiXVszXTtmW2JdWzFdKz1mW2JdWzRdO2MuZmlsbFN0eWxlPSJoc2woIitNYXRoLm1pbigzMDAsTWF0aC5mbG9vcig1MCpNYXRoLnNxcnQoZltiXVszXSpmW2JdWzNdK2ZbYl1bNF0qZltiXVs0XSkpKSsiLDEwMCUsNDAlKSI7Yy5iZWdpblBhdGgoKTtjLmFyYyhmW2JdWzBdLGZbYl1bMV0sZltiXVsyXSwwLE1hdGguUEkqMix0cnVlKTtjLmZpbGwoKX19LDApOyBkb2N1bWVudC5ib2R5Lm9ubW91c2Vkb3duPWZ1bmN0aW9uKGQpe2g9Zi5sZW5ndGg7Zi5wdXNoKFtkLmNsaWVudFgsZC5jbGllbnRZLDIsMCwwXSk7cmV0dXJuIGZhbHNlfTtkb2N1bWVudC5ib2R5Lm9ubW91c2V1cD1mdW5jdGlvbihkKXtmW2hdWzNdPShkLmNsaWVudFgtZltoXVswXSkvMTAwO2ZbaF1bNF09KGQuY2xpZW50WS1mW2hdWzFdKS8xMDA7aD0tMTtyZXR1cm4gZmFsc2V9
<body>
<canvas id="c"></canvas>
<script>
var canvas = document.getElementById('c');
document.body.setAttribute('style','margin:0;left:0;top:0;padding:0')
canvas.width = innerWidth;
canvas.height = innerHeight;
var context = canvas.getContext('2d')
var planets = [
[300,100,10,-0.2,0],
[300,300,10,0.2,0], //x,y,r,xv,yv
];
var selected = -1;
setInterval(function(){
if(selected > 0) planets[selected][2]+=0.1;
context.clearRect(0,0,innerWidth,innerHeight)
for(var plen = planets.length, a = plen, b; a--;){
for(b = plen; b--;){
if(b!=a && a != selected && b != selected){ //there is no friction in space. Plus it'll yield a divide by zero error
//inverse square law: force = mass/distance-squared;
var dx = planets[b][0] - planets[a][0];
var dy = planets[b][1] - planets[a][1];
var d2 = dx*dx + dy*dy;
var d = Math.sqrt(d2);
var force = planets[b][2]/d2;
planets[a][3] += dx / d * force; //possible optimization: force = force/d
planets[a][4] += dy / d * force; //and sumY += dy / force
}
}
planets[a][0] += planets[a][3];
planets[a][1] += planets[a][4];
context.fillStyle = "hsl(" + Math.min(300, Math.floor(50 * Math.sqrt(planets[a][3] * planets[a][3] + planets[a][4] * planets[a][4]))) + ",100%,40%)";
context.beginPath();
context.arc(planets[a][0], planets[a][1], planets[a][2], 0, Math.PI * 2, true);
context.fill()
}
},0)
document.body.onmousedown = function(e){
selected = planets.length;
planets.push([e.clientX,e.clientY,2,0,0])
return false
}
document.body.onmouseup = function(e){
planets[selected][3] = (e.clientX - planets[selected][0])/100;
planets[selected][4] = (e.clientY - planets[selected][1])/100;
selected = -1;
return false
}
</script>
</body>