為什么明明設(shè)置了繪制10棵星星,運(yùn)行之后,出現(xiàn)滿畫布的星星
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>無標(biāo)題文檔</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 旋轉(zhuǎn)角度
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>
2015-11-05
其他的問題,同一樓的,局部使用i時(shí),請用var
2015-11-05
draw函數(shù)里的context.beginPath(); 應(yīng)該是cxt.beginPath();
2015-11-05
為何你把draw函數(shù)定義在onload內(nèi)部?移出去試試?
2015-09-18
for(i=0;i<5;i++) --------------->for(var i=0;i<5;i++) 或者--------->var i=0;for(i=0;i<5;i++)