<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script type="text/javascript">
window.onload=function(){
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d");
canvas.width=800;
canvas.height=800;
context.fillStyle="black";
context.fillRect(0,0,canvas.width,canvas.height);
for(i=0;i<10;i++)
{
var r=Math.random()*10+10;
var x=Math.random()*canvas.width;
var y=Math.random()*canvas.height;
var a=Math.random()*360;
draw(context,r,r/2,x,y,a);
}
//rot 旋转角度
function draw(cxt,R,r,x,y,rot){
context.beginPath();
for(i=0;i<5;i++)
{
cxt.lineTo(Math.cos((18+i*72-rot)/180*Math.PI)*R+x,
-Math.sin((18+i*72-rot)/180*Math.PI)*R+y);
cxt.lineTo(Math.cos((54+i*72-rot)/180*Math.PI)*r+x,
-Math.sin((54+i*72-rot)/180*Math.PI)*r+y);
}
cxt.closePath();
cxt.lineWidth=3;
cxt.strokeStyle="#fd5";
cxt.lineJoin="round";
cxt.fillStyle="#fb3";
cxt.fill();
cxt.stroke();
}}
</script>
</head>
<body>
<canvas id="canvas" style="border:1px black solid;margin:100px auto;display:block;">
</canvas>
</body>
</html>
其他的问题,同一楼的,局部使用i时,请用var
draw函数里的context.beginPath(); 应该是cxt.beginPath();
为何你把draw函数定义在onload内部?移出去试试?
for(i=0;i<5;i++) --------------->for(var i=0;i<5;i++) 或者--------->var i=0;for(i=0;i<5;i++)