為什么我的星星都在一坨?
<!DOCTYPE html>
<html lang="en">
<head>
? ? <meta charset="UTF-8">
? ? <meta http-equiv="X-UA-Compatible" content="IE=edge">
? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">
? ? <title>Document</title>
</head>
<body>
? ? <canvas id="star" ></canvas>
? ? <script>
? ? ? ? window.onload = function () {
? ? ? ? ? ? let canvas = document.getElementById('star');
? ? ? ? ? ? canvas.width = 800;
? ? ? ? ? ? canvas.height = 800;
? ? ? ? ? ? let context = canvas.getContext('2d');
? ? ? ? ? ? context.fillStyle = 'black';
? ? ? ? ? ? context.fillRect( 0,0,canvas.width,canvas.height);
? ? ? ? ? ? // frawStar(context,150,300,400,400,30);//調(diào)用五角星函數(shù)
? ? ? ? ? ? for(var i = 0; i < 20; i ++){
? ? ? ? ? ? ? ? var r = Math.random() * 10 + 10;
? ? ? ? ? ? ? ? var x = Math.random() * canvas.width/10;
? ? ? ? ? ? ? ? var y = Math.random() * canvas.height/10;
? ? ? ? ? ? ? ? var a = Math.random() * 360;
? ? ? ? ? ? ? ? console.log(r,x,y,a)
? ? ? ? ? ? ? ? frawStar(context , x , y , r , (r / 2.0) , a);//調(diào)用五角星函數(shù)
? ? ? ? ? ? }
? ? ? ? ? ? // context.lineWidth = 10;
? ? ? ? ? ? context.stroke();
? ? ? ? }
? ? ? ? //五角星的函數(shù)
? ? ? ? /* r 小圓半徑,R大圓半徑,x 偏移值x坐標(biāo),y 偏移值 y坐標(biāo),rot五角星旋轉(zhuǎn)角度*/
? ? ? ? function frawStar(cxt, r, R, x, y, rot) {
? ? ? ? ? ? cxt.beginPath();
? ? ? ? ? ? for (var 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.fillStyle = '#fb3';
? ? ? ? ? ? // ?cxt.fillStyle = '#fff';
? ? ? ? ? ? cxt.strokeStyle = '##fd5';
? ? ? ? ? ? cxt.lineWidth = 3;
? ? ? ? ? ? cxt.lineJoin = 'round';
? ? ? ? ? ? cxt.fill();
? ? ? ? ? ? cxt.stroke();
? ? ? ? }
? ? </script>
</body>
</html>
2023-05-07
改成如下即可