ES5 Canvas Thing
var canvas = document.body.appendChild(
document.createElement('canvas')
),
c = canvas.getContext("2d"),
size = canvas.width,
quarterSize = size / 4,
eightSize = size / 8,
halfSize = size / 2,
TWO_PI = Math.PI * 2,
bombNum = 3,
bombs = [],
wanderers = {},
wandererIndex = 0,
particles = {},
particleIndex = 0;
canvas.width = canvas.height = 300
c.fillStyle = "rgb(100,100,100)";
c.fillRect(0,0,size,size);
function Particle(x, y){
this.x = x;
this.y = y;
var rad = 3 + Math.random() * 6;
var theta = Math.random() * TWO_PI;
this.vx = rad * Math.cos(theta);
this.vy = rad * Math.sin(theta);
this.alpha = 1;
particleIndex++;
this.index = particleIndex;
particles[this.index] = this;
}
Particle.prototype.draw = function(){
this.x += this.vx;
this.y += this.vy;
this.vx *= 0.9;
this.vy *= 0.9;
this.alpha -= 0.05;
if (this.alpha <= 0){
this.alpha = 0;
delete particles[this.index];
}
c.fillStyle = "rgba(255,255,255,"+this.alpha+")";
c.beginPath();
c.arc(this.x, this.y, 1, 0, TWO_PI, false);
c.fill();
};
function Bomb(){
this.x = quarterSize + Math.random() * halfSize;
this.y = quarterSize + Math.random() * halfSize;
this.radius = 15 + Math.random() * 20;
}
Bomb.prototype.draw = function(){
c.fillStyle = "#e64c25";
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, TWO_PI, false);
c.fill();
};
function Wanderer(x, y){
this.x = x;
this.y = y;
this.vx = Math.random() * 4 - 2;
this.vy = Math.random() * 4 - 2;
particleIndex++;
this.index = particleIndex;
particles[this.index] = this;
}
Wanderer.prototype.die = function(){
for (var j = 0; j < 4; j++){
new Particle(this.x, this.y);
}
delete particles[this.index];
};
Wanderer.prototype.draw = function(){
this.x += this.vx;
this.y += this.vy;
if (Math.random() < 0.1){
this.vx = Math.random() * 4 - 2;
this.vy = Math.random() * 4 - 2;
}
if (this.x < 0) this.x = size;
if (this.x > size) this.x = 0;
if (this.y < 0) this.y = size;
if (this.y > size) this.y = 0;
c.fillStyle = "white";
c.beginPath();
c.arc(this.x, this.y, 2, 0, TWO_PI, false);
c.closePath();
c.fill();
for (var i = 0; i < bombNum; i++){
var bomb = bombs[i];
var dx = this.x - bomb.x;
var dy = this.y - bomb.y;
if (Math.sqrt(dx * dx + dy * dy) < bomb.radius){
this.die();
}
}
};
for (var i = 0; i < bombNum; i++){
bombs[i] = new Bomb();
}
new Wanderer(eightSize, eightSize);
setInterval(function(){
c.fillStyle = "rgba(100,100,100, 0.2)";
c.fillRect(0,0,size,size);
c.strokeStyle = "white";
c.beginPath();
c.arc(eightSize, eightSize, 5, 0, TWO_PI, false);
c.stroke();
if (Math.random() < 0.02){
new Wanderer(eightSize, eightSize);
}
for (var i = 0; i < bombNum; i++){
bombs[i].draw();
}
for (var i in wanderers){
wanderers[i].draw();
}
for (var i in particles){
particles[i].draw();
}
}, 16);
An old es5 speedcoded thing…