js绘制canvas图形

var cr = document.getElementById(“cardioid”);

var W = cr.width/2, H = cr.height/3, R = 150;

var c = cr.getContext(“2d”); var G = 360, g = 0, T = Math.PI*2, t = T/G;

c.save(); c.translate(W, H);c.rotate(-T/4);

//c.fillStyle = “red”;

while(g < G){

c.save();//c.translate(W, H);c.rotate(g*t);c.beginPath();c.arc(0, -R*(1-Math.sin(++g*t)), 13, 0, 360, false);c.closePath();c.fill();c.restore();

}

c.restore(); g = 0;

(function draw(){

if(g < G){ c.save(); c.translate(W, H); c.rotate(-T/4 + g*t);c.fillStyle = “red”;c.beginPath();c.arc(0, -R*(1-Math.sin(++g*t)), 5, 0, 360, false); c.closePath(); c.fill(); c.restore();

} setTimeout(draw, 1);

})();