Last active
May 29, 2017 11:49
-
-
Save smt/d6490aee3eb9fc20da2f to your computer and use it in GitHub Desktop.
Mesmerizing
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<!-- | |
Based on: http://www.reddit.com/r/gifs/comments/2on8si/connecting_to_server_so_mesmerizing/ | |
See also: http://codepen.io/anon/pen/OPMvOb | |
http://jsbin.com/xecosiyomo/1/edit?js,output | |
--> | |
<head> | |
<title>Mesmerizing</title> | |
<style> | |
canvas { background-color: #000; } | |
</style> | |
<script> | |
window.requestAnimFrame = (function(callback) { | |
return window.requestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.oRequestAnimationFrame || | |
window.msRequestAnimationFrame || | |
function(callback) { | |
window.setTimeout(callback, 1000 / 60); | |
}; | |
})(); | |
var NUM_BALL = 60; | |
var BALL_HEIGHT = 200; | |
var timeStep = 0; | |
var radius = 4; | |
var color = '#0f0'; | |
var c, ctx; | |
function draw() { | |
c = document.getElementById('screen'); | |
if (c.getContext) { | |
ctx = c.getContext('2d'); | |
requestAnimFrame(animateBalls); | |
} | |
} | |
function animateBalls() { | |
ctx.clearRect(0, 0, c.width, c.height); | |
for (var i = 0; i < NUM_BALL; i++) { | |
ctx.beginPath(); | |
ctx.arc(8+16*i, getY(i, timeStep), radius, 0, Math.PI*2, true); | |
ctx.fillStyle = color; | |
ctx.fill(); | |
} | |
timeStep++; | |
requestAnimFrame(animateBalls); | |
} | |
function getY(i, t) { | |
return 150 + BALL_HEIGHT/2 * (1 + Math.sin((timeStep * (i/500 + 0.02)) % 2*Math.PI)); | |
} | |
window.addEventListener('load', draw); | |
</script> | |
</head> | |
<body> | |
<canvas id="screen" width="960" height="500"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment