var sprites = []; var count = 0; //sprite image var photon = new Image(); photon.src = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/131045/photon.png'; function addSprite() { var sprite = { vx: Math.random(), vy: Math.random(), x: Math.random() * 5000, y: Math.random() * 5000, image: photon }; sprites.push(sprite); count++; } //change the number of photons here (the 5000) for ( i = 0; i < 5000; i++) { addSprite(); } var canvas = document.getElementById('canv'); //canvas width / height var width = 1900; var height = 900; var ctx = canvas.getContext('2d'); //Leave this section alone - sprite rendering variables var s, t, i; function render() { canvas.setAttribute('width', width); canvas.setAttribute('height', height); for ( i = 0, l = count; i < l; i++) { s = sprites[i]; s.x += s.vx; s.y += s.vy; dx = clientX - s.x; dy = clientY - s.y; dist = Math.sqrt(dx * dx + dy * dy); if (dist > 0) { co = .3 / dist; s.vx += co * dx; s.vy += co * dy; } // drag s.vx *= .995; s.vy *= .995; ctx.drawImage(s.image, ~~s.x, ~~s.y); } } //mouse movements var clientX = 50, clientY = 50; document.addEventListener('mousemove', function(e) { clientX = e.clientX; clientY = e.clientY; }); var transformProperty; function setupBrowser() { if (window.requestAnimationFrame) { window.requestAnimFrame = window.requestAnimationFrame; transformProperty = 'transform'; } else if (window.webkitRequestAnimationFrame) { window.requestAnimFrame = window.webkitRequestAnimationFrame; transformProperty = 'webkitTransform'; } else if (window.mozRequestAnimationFrame) { window.requestAnimFrame = window.mozRequestAnimationFrame; transformProperty = 'MozTransform'; } else { window.requestAnimFrame = function(callback) { window.setTimeout(callback, 1000 / 60); }; } } setupBrowser(); (function animloop() { window.requestAnimFrame(animloop); render(); })();