Last active
August 29, 2015 14:06
-
-
Save olls/99836bb20e0616676315 to your computer and use it in GitHub Desktop.
A simple physics console animation in JS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var stop = false; | |
var loop = function (d) { | |
if (stop){ | |
return; | |
} | |
if (d == undefined) { | |
var d = { | |
width: 30, | |
height: 30, | |
x: 0, | |
y: 29, | |
dx: 2, | |
dy: 0, | |
f: .99, | |
r: .7 | |
}; | |
} | |
// Adjust velocity | |
if (d.y > 0){ | |
d.dy -= .1; | |
} | |
d.dx *= d.f; | |
// Update position | |
d.x += d.dx; | |
d.y += d.dy; | |
if (d.x < 0) d.x = 0; | |
if (d.x >= d.width) d.x = d.width-1; | |
if (d.y < 0) d.y = 0; | |
if (d.y >= d.height) d.y = d.height-1; | |
// Bounce | |
if (d.y <= 0 || d.y >= d.height-1){ | |
d.dy *= -d.r; | |
} | |
if (d.x <= 0 || d.x >= d.width-1){ | |
d.dx *= -d.r; | |
} | |
var frame = '' | |
for (var y = d.height-1; y >= 0; y--) { | |
for (var x = 0; x < d.width; x++) { | |
frame += ' ' + ((x == Math.floor(d.x) && y == Math.floor(d.y)) ? '#' : ' '); | |
} | |
frame += '\n'; | |
} | |
if (d.frame != frame) { | |
d.frame = frame; | |
console.log(d.frame); | |
} | |
setTimeout( | |
(function (d) { | |
return function () { | |
loop(d); | |
} | |
})(d), | |
100 | |
) | |
} | |
loop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body onload="y=p=29;c=2;u=x=a=0;setInterval(function(){x+=c*=.99*(x<=0|x>=p?-.7:1);x=x<0?0:x>=30?p:x;u*=y<=0|y>=p?-.7:1;y+=u-=y>0?.1:0;y=y<0?0:y>=30?p:y;for(i=30,o='';i--;)for(v=~~x,o+='\n';v--;)o+=' '+' #'[!v&i==~~y];if(o!=a)console.log(a=o)},99)"> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
data:text/html;base64,PHNjcmlwdD55PTI5O2M9Mjt1PXg9YT0wO3NldEludGVydmFsKGZ1bmN0aW9uKCl7dS09eT4wPy4xOjA7Yyo9Ljk5O3grPWM7eSs9dTtpZih4PDApeD0wO2lmKHg+PTMwKXg9Mjk7aWYoeTwwKXk9MDtpZih5Pj0zMCl5PTI5O2lmKHk8PTB8fHk+PTI5KXUqPS0uNztpZih4PD0wfHx4Pj0yOSljKj0tLjc7Zm9yKGk9MzAsbz0nJztpLS07KWZvcih2PTMwLG8rPSdcbic7di0tOylvKz0nICcrJyAjJ1t2PT1+fngmaT09fn55XTtpZihvIT1hKWNvbnNvbGUubG9nKGE9byl9LDk5KTwvc2NyaXB0Pg0K