Created
January 22, 2013 09:01
-
-
Save sauravtom/4593194 to your computer and use it in GitHub Desktop.
A CodePen by Rafał Krupiński.
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
<canvas id="c" width="400" height="400"></canvas> |
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
//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 ); | |
}()); |
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 { | |
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