Skip to content

Instantly share code, notes, and snippets.

@sauravtom
Created January 22, 2013 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauravtom/4593194 to your computer and use it in GitHub Desktop.
Save sauravtom/4593194 to your computer and use it in GitHub Desktop.
A CodePen by Rafał Krupiński.
<canvas id="c" width="400" height="400"></canvas>
//settings
var MAX_PARTICLES = 300,
MAX_PARTICLE_RADIUS = 8,
CHAR = '@';
//CHAR = '\u2764';
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
var canvas = document.querySelector('#c'),
ctx = canvas.getContext('2d'),
cWidth = canvas.width,
cHeight = canvas.height;
var buffer = document.createElement('canvas'),
bctx = buffer.getContext('2d');
buffer.width = cWidth;
buffer.height = cHeight;
bctx.font = 'bold 400px/1 Arial, sans-serif';
bctx.fillText(CHAR, 0, 300);
var particles = [],
particlesCount = 0;
function Particle( x, y ) {
this.x = x;
this.y = y;
this.radius = 0;
this.lifetime = 0;
this.color = 'rgba(255, 255, 255, ' + Math.random() + ')';
}
Particle.prototype.update = function() {
this.lifetime += 0.1;
this.radius = ~~(Math.abs(Math.sin(this.lifetime) * MAX_PARTICLE_RADIUS));
};
function update() {
particles.forEach(function( p ) {
p.update();
});
if (particlesCount === MAX_PARTICLES) return;
var x = ~~(Math.random() * cWidth),
y = ~~(Math.random() * cHeight);
if ( bctx.getImageData( x, y, 1, 1 ).data[3] ) {
particles.push( new Particle( x, y ) );
particlesCount += 1;
}
}
function render() {
ctx.clearRect( 0, 0, cWidth, cHeight );
particles.forEach(function( p ) {
ctx.save();
ctx.fillStyle = p.color;
ctx.beginPath();
ctx.arc( p.x, p.y, p.radius, 0, Math.PI * 2, false );
ctx.closePath();
ctx.fill();
ctx.restore();
});
}
(function loop() {
update();
render();
requestAnimFrame( loop );
}());
body {
overflow: hidden;
background: black;
}
#c {
display: block;
margin: 0 auto;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment