代码部分
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>棒棒糖</title>
<style type="text/css">
#canvas{
border: 1px solid red;
}
</style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.translate(250,250);
context.rotate(Math.PI0.05)
var r = 0;
var num = 0;
act();
function draw(color,x,y){
context.beginPath();
context.fillStyle = color;
context.arc(x,y,20,0,Math.PI2,false);
context.fill();
}
var percent = 0;
var color = rundColor();
function act(){
percent +=Math.PI/180;
r+=0.15;
var x = rMath.cos(percent)
var y = rMath.sin(percent)
if(r>150){
r=150;
num++;
}
draw(color,x,y);
res = window.requestAnimationFrame(act);
if( num > 360){
window.cancelAnimationFrame(res);
drawBangAct();
}
}
//随机数函数
function rundNumber(min,max){
return parseInt(Math.random()*(max-min) + min);
}
function rundColor(){
return "rgb("+rundNumber(0,256) +","+ rundNumber(0,256) +","+ rundNumber(0,256) + ")";
}
function drawbang(x,y){
context.beginPath();
context.lineWidth = 40 ;
context.moveTo(120,120);
context.strokeStyle = "pink";
context.lineTo(x,y);
context.stroke();
}
var z = 120;
var y = 120;
function drawBangAct(){
z++;
y++;
drawbang(z,y);
var aa = window.requestAnimationFrame(drawBangAct);
if(z>250){
window.cancelAnimationFrame(aa);
}
}
</script>
</html>
效果图