源自:4-3 应用translate、rotate和scale
为什么不出星星
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<canvas id="mycanvas"></canvas>
<script>
var canvas=document.getElementById("mycanvas");
canvas.width=400;
canvas.height=400;
var context=canvas.getContext("2d");
context.fillRect(0,0,canvas.width,canvas.height);
for(var i=0;i<100;i++){
r=Math.random()*10+10;
x=Math.random()*c.width;
y=Math.random()*c.height;
rot=Math.random()*360;
drawstar(context,x,y,r,rot);
}
function drawstar(cxt,x,y,r,rot){
cxt.save();
cxt.translate(x,y);
cxt.rotate(rot/180*Math.PI);
starPath(cxt);
cxt.fillStyle="#fb3";
cxt.strokeStyle="#fd5";
cxt.lineWidth=3;
cxt.lineJoin="round";
cxt.fill();
cxt.stroke();
cxt.restore();
}
function starPath(cxt){
cxt.beginPath();
for(var i=0;i<10;i++){
cxt.lineTo(Math.cos((18+(i*72))*Math.PI/180)*20,-Math.sin((18+(i*72))*Math.PI/180)*0.5*20);
cxt.lineTo(Math.cos((54+(i*72))*Math.PI/180)*20,-Math.sin((54+(i*72))*Math.PI/180)*0.5*20);
}
cxt.closePath();
}
</script>
</body>
</html>
提问者:来自火星的花
2017-04-03 22:14