Как насчет того, чтобы сделать его немного модульным ..
var c = document.getElementById('test').getContext('2d');
function Circle(_x, _y, _r){
var x = _x;
var y = _y;
var radius = _r;
var endAngle = (Math.PI/180)*360;
this.draw = function(canvas){
canvas.fillStyle = "red";
canvas.beginPath();
canvas.arc(x,y,radius,0,endAngle ,true);
canvas.fill();
};
this.delete = function(canvas){
canvas.fillStyle = "white";
canvas.beginPath();
canvas.arc(x,y,radius+1,0,endAngle ,true);
canvas.fill();
};
return this;
}
function Heart(_canvas, x,y,radius,increase){
var canvas = _canvas;
var currentState = 0;
var states = [
new Circle(x,y,radius),
new Circle(x,y,radius+increase/2),
new Circle(x,y,radius+increase)
];
var that = this;
this.beat = function(delay){
if (delay){
setTimeout(function(){that.beat(0);}, delay);
}
else{
if (currentState == states.length){
states[states.length-1].delete(canvas);
that.beat(1000);
currentState = 0;
}
else{
states[currentState++].draw(canvas);
setTimeout( function() {
that.beat();
}, 150);
}
}
}
return this;
}
var heart1 = new Heart(c, 50,50,30,10);
var heart2 = new Heart(c, 130,120,40,20);
var heart3 = new Heart(c, 40,160,20,10);
heart1.beat(100);
heart2.beat(500);
heart3.beat(1300);
Жить и биться в http://jsfiddle.net/gaby/NyPZM/