|
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/ |
|
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating |
|
|
|
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel |
|
|
|
// MIT license |
|
|
|
(function() { |
|
var lastTime = 0; |
|
var vendors = ['ms', 'moz', 'webkit', 'o']; |
|
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { |
|
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; |
|
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] |
|
|| window[vendors[x]+'CancelRequestAnimationFrame']; |
|
} |
|
|
|
if (!window.requestAnimationFrame) |
|
window.requestAnimationFrame = function(callback, element) { |
|
var currTime = new Date().getTime(); |
|
var timeToCall = Math.max(0, 16 - (currTime - lastTime)); |
|
var id = window.setTimeout(function() { callback(currTime + timeToCall); }, |
|
timeToCall); |
|
lastTime = currTime + timeToCall; |
|
return id; |
|
}; |
|
|
|
if (!window.cancelAnimationFrame) |
|
window.cancelAnimationFrame = function(id) { |
|
clearTimeout(id); |
|
}; |
|
}()); |
|
|
|
|
|
(function() { |
|
|
|
// Get the buttons. |
|
var startBtn = document.getElementById('button'); |
|
/*var resetBtn = document.getElementById('resetBtn');*/ |
|
// A variable to store the requestID. |
|
var requestID; |
|
// Canvas |
|
var canvas = document.getElementById('canvas'); |
|
// 2d Drawing Context. |
|
var ctx = canvas.getContext('2d'); |
|
|
|
// Variables to for the drawing position and object. |
|
var posX = 0; |
|
var W = 246; |
|
var H = 60; |
|
var circles = []; |
|
|
|
//Get canvas size |
|
canvas.width = 246; |
|
canvas.height = 60; |
|
|
|
// Animate. |
|
function animate() { |
|
requestID = requestAnimationFrame(animate); |
|
//Fill canvas with black color |
|
//ctx.globalCompositeOperation = "source-over"; |
|
ctx.fillStyle = "rgba(0,0,0,0.15)"; |
|
ctx.fillRect(0, 0, W, H); |
|
|
|
//Fill the canvas with circles |
|
for(var j = 0; j < circles.length; j++){ |
|
var c = circles[j]; |
|
|
|
//Create the circles |
|
ctx.beginPath(); |
|
ctx.arc(c.x, c.y, c.radius, 0, Math.PI*2, false); |
|
ctx.fillStyle = "rgba("+c.r+", "+c.g+", "+c.b+", 0.5)"; |
|
ctx.fill(); |
|
|
|
c.x += c.vx; |
|
c.y += c.vy; |
|
c.radius -= .02; |
|
|
|
if(c.radius < 0) |
|
circles[j] = new create(); |
|
} |
|
|
|
|
|
|
|
} |
|
|
|
//Random Circles creator |
|
function create() { |
|
|
|
//Place the circles at the center |
|
|
|
this.x = W/2; |
|
this.y = H/2; |
|
|
|
|
|
//Random radius between 2 and 6 |
|
this.radius = 2 + Math.random()*3; |
|
|
|
//Random velocities |
|
this.vx = -5 + Math.random()*10; |
|
this.vy = -5 + Math.random()*10; |
|
|
|
//Random colors |
|
this.r = Math.round(Math.random())*255; |
|
this.g = Math.round(Math.random())*255; |
|
this.b = Math.round(Math.random())*255; |
|
} |
|
|
|
for (var i = 0; i < 500; i++) { |
|
circles.push(new create()); |
|
} |
|
|
|
// Event listener for the start button. |
|
startBtn.addEventListener('mouseover', function(e) { |
|
e.preventDefault(); |
|
|
|
// Start the animation. |
|
requestID = requestAnimationFrame(animate); |
|
}); |
|
|
|
|
|
// Event listener for the stop button. |
|
startBtn.addEventListener('mouseout', function(e) { |
|
e.preventDefault(); |
|
|
|
// Stop the animation; |
|
cancelAnimationFrame(requestID); |
|
|
|
e.preventDefault(); |
|
|
|
// Reset the X position to 0. |
|
posX = 0; |
|
|
|
// Clear the canvas. |
|
ctx.clearRect(0, 0, canvas.width, canvas.height); |
|
|
|
// Draw the initial box on the canvas. |
|
ctx.fillRect(posX, 0, boxWidth, canvas.height); |
|
|
|
}); |
|
|
|
|
|
/*// Event listener for the reset button. |
|
resetBtn.addEventListener('click', function(e) { |
|
e.preventDefault(); |
|
|
|
// Reset the X position to 0. |
|
posX = 0; |
|
|
|
// Clear the canvas. |
|
ctx.clearRect(0, 0, canvas.width, canvas.height); |
|
|
|
// Draw the initial box on the canvas. |
|
ctx.fillRect(posX, 0, boxWidth, canvas.height); |
|
});*/ |
|
|
|
|
|
}()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|