更新时间:2023-01-08 09:38:36
星星基本上是一个正多边形,上面有交替点内半径和外半径。
A star is basically a regular polygon with alternating points on an inner and an outer radius.
以下是绘制星形的灵活函数示例。
Here's an example of a flexible function to draw a star shape.
你可以设置位置,#spikes和内部&尖峰的外半径:
You can set the position, #spikes and the inner & outer radius of the spikes:
示例代码和演示: http:// jsfiddle.net/m1erickson/8j6kdf4o/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
function drawStar(cx,cy,spikes,outerRadius,innerRadius){
var rot=Math.PI/2*3;
var x=cx;
var y=cy;
var step=Math.PI/spikes;
ctx.beginPath();
ctx.moveTo(cx,cy-outerRadius)
for(i=0;i<spikes;i++){
x=cx+Math.cos(rot)*outerRadius;
y=cy+Math.sin(rot)*outerRadius;
ctx.lineTo(x,y)
rot+=step
x=cx+Math.cos(rot)*innerRadius;
y=cy+Math.sin(rot)*innerRadius;
ctx.lineTo(x,y)
rot+=step
}
ctx.lineTo(cx,cy-outerRadius);
ctx.closePath();
ctx.lineWidth=5;
ctx.strokeStyle='blue';
ctx.stroke();
ctx.fillStyle='skyblue';
ctx.fill();
}
drawStar(100,100,5,30,15);
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=300 height=300></canvas>
</body>
</html>