Skip to content

Instantly share code, notes, and snippets.

@maxlibin
Created November 18, 2013 10:36
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 maxlibin/7525767 to your computer and use it in GitHub Desktop.
Save maxlibin/7525767 to your computer and use it in GitHub Desktop.
A Pen by max.
<canvas id="myCanvas" width="800" height="600'></canvas>
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
canvas = document.getElementById("myCanvas");
ctx= canvas.getContext("2d");
var amount = 100,
gravity = 0.4;
function Snow(x, y, distance){
this.xP =x;
this.yP =y;
this.distance = distance;
this.velocity = distance * (Math.random());
this.radius = distance * 2;
}
Snow.prototype.draw = function(){
// Create particles
var radgrad = ctx.createRadialGradient(this.xP,this.yP,0,this.xP,this.yP,this.radius);
radgrad.addColorStop(0, 'rgba(255,255,255,.7)');
ctx.fillStyle= radgrad;
ctx.shadowColor= "#fff";
ctx.shadowBlur =10;
ctx.beginPath();
ctx.arc(this.xP, this.yP, this.radius, 0, Math.PI*2);
ctx.fill();
ctx.closePath();
}
var snow = [];
function loop(){
clearContext();
for(i =0; i < snow.length; i++){
var s = snow[i];
s.yP +=s.velocity * gravity;
// give some wind direction
/*var d = new Date();
t = d.getTime();
s.xP +=Math.sin(t/1200);*/
s.draw();
if(s.yP > canvas.height){
s.yP = 0;
}
/*if(s.xP > canvas.height){
s.xP = 0;
}*/
}
requestAnimationFrame(loop);
}
for(i=0; i < amount; i++){
var snows = new Snow(Math.random() * canvas.width, Math.random() * canvas.height, Math.random() * 3);
/*snows.draw();*/
snow.push(snows);
}
requestAnimFrame(loop);
clearContext = function(){
ctx.clearRect(0,0,canvas.width,canvas.height);
}
@import "compass";
body {
background:black;
}
#myCanvas {
@extend body;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment