Skip to content

Instantly share code, notes, and snippets.

@chikoski
Created November 6, 2013 13:54
Show Gist options
  • Select an option

  • Save chikoski/7336404 to your computer and use it in GitHub Desktop.

Select an option

Save chikoski/7336404 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<canvas id="canvas" width="480" height="340"></canvas>
</body>
</html>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var width = 480;
var height = 340;
var size = 4;
var speed = 6;
var interval = Math.floor(1000 / 24);
var particle;
var updateParticle = function(){
particle.x = particle.x + particle.dx;
particle.y = particle.y + particle.dy;
if(particle.x < 0 || particle.x > width){
particle.dx = particle.dx * -1;
}
if(particle.y < 0 || particle.y > height){
particle.dy = particle.dy * -1;
}
};
var draw = function(){
if(ctx !== null){
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "rgba(0, 8, 16, 0.4)";
ctx.fillRect(0, 0, width, height);
ctx.globalCompositeOperation = "lighter";
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size, 0, 2 * Math.PI);
ctx.closePath();
ctx.fillStyle = particle.color;
ctx.fill();
}
};
var update = function(){
updateParticle();
draw();
setTimeout(update, interval);
};
var initialize = function(){
particle = {
x: Math.floor(Math.random() * width),
y: Math.floor(Math.random() * height),
dx: speed,
dy: speed,
size: size,
color: "rgb(00, 150, 221)"
};
setTimeout(update, interval);
};
initialize();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment