• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦见艾七
博客园    首页    新随笔    联系   管理    订阅  订阅

用canvas画一个五角星

h5部分:

        <canvas id="canvas">
            您的浏览器不支持canvas,请升级浏览器。
        </canvas>

javascript部分:

<script type="text/javascript">
	var canvas = document.getElementById("canvas");
	var ctx = canvas.getContext("2d");
	canvas.width = 300;
	canvas.height = 300;
	canvas.style.border = "1px solid #ccc";
	
	function create5star(ctx,r,R,x,y,fillColor,strokeColor){
		ctx.beginPath();
		ctx.lineWidth = 5;
		ctx.fillStyle = fillColor;
		ctx.strokeStyle = strokeColor;
		for(var i=0;i<5;i++){
			var x1 = Math.cos((18+72*i)*Math.PI/180);
			var y1 = Math.sin((18+72*i)*Math.PI/180);
			var x2 = Math.cos((54+72*i)*Math.PI/180);
			var y2 = Math.sin((54+72*i)*Math.PI/180);
			ctx.lineTo(x1*R+x,-y1*R+y);
			ctx.lineTo(x2*r+x,-y2*r+y);
		}
		ctx.closePath();
		ctx.stroke();
		ctx.fill();
	}
	
	create5star(ctx,30,60,150,150,"yellow","greenyellow");
</script>

效果展示: 

角度说明:

 

posted on 2017-08-08 14:55  梦见艾七  阅读(158)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3