canvas画出五角星

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
canvas{
border: 1px solid red;
background-color: red;
}
</style>
</head>
<body>
<!-- 如果你画的图像是扭曲的,不规则的,查看是否canvas设置了宽高 而不是css -->
<canvas width='500' height='500' id='canvas'>对不起,你的浏览器不支持canvas,请升级你的浏览器</cauvas>
<script>
var canvas = document.getElementById('canvas');
var ctx =canvas.getContext('2d');
ctx.beginPath();//开始绘制路径
ctx.moveTo(30,100)//那绘制点移动到200,200的位置
ctx.lineTo(230,100);//绘制的这条线经过250,300这个点
ctx.lineTo(70,220);//绘制的这条线经过300,250这个点
ctx.lineTo(130,30);//绘制的这条线经过300,250这个点
ctx.lineTo(190,220);//绘制的这条线经过300,250这个点
ctx.closePath();//关闭路径
ctx.strokeStyle='red';//笔触颜色
ctx.lineWidth='2';//笔触宽度
ctx.stroke();//空心显示笔触
ctx.fillStyle='yellow';//填充色
ctx.fill();//添加填充


</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/mrfano/article/details/73321148