HTML5技术

Html5 绘制五星红旗 - 飞翔的月亮

字号+ 作者:H5之家 来源:H5之家 2016-12-01 12:00 我要评论( )

Html5+JavaScript 在Canvas上绘制五星红旗,具体思路如下图所示: 绘制思路在上图中已有说明,具体代码如下: 1 script type="text/javascript" drawStar(ctx,starCenterX,starCenterY,starRadius) { 5 var aX = starCenterX; 6 var aY = starCenterY - star

Html5+JavaScript 在Canvas上绘制五星红旗,具体思路如下图所示:

绘制思路在上图中已有说明,具体代码如下:

1 <script type="text/javascript"> drawStar(ctx,starCenterX,starCenterY,starRadius) { 5 var aX = starCenterX; 6 var aY = starCenterY - starRadius; 7 var bX = starCenterX - Math.cos(18 * Math.PI / 180) * starRadius; 8 var bY = starCenterY - Math.sin(18 * Math.PI / 180) * starRadius; 9 var cX = starCenterX - Math.cos(54 * Math.PI / 180) * starRadius; 10 var cY = starCenterY + Math.sin(54 * Math.PI / 180) * starRadius; 11 var dX = starCenterX + Math.cos(54 * Math.PI / 180) * starRadius; 12 var dY = starCenterY + Math.sin(54 * Math.PI / 180) * starRadius; 13 var eX = starCenterX + Math.cos(18 * Math.PI / 180) * starRadius; 14 var eY = starCenterY - Math.sin(18 * Math.PI / 180) * starRadius; 15 ctx.beginPath(); 16 ctx.fillStyle = "yellow"; 17 ctx.moveTo(aX, aY); 18 ctx.lineTo(cX, cY); 19 ctx.lineTo(eX, eY); 20 ctx.lineTo(bX, bY); 21 ctx.lineTo(dX, dY); 22 ctx.lineTo(aX, aY); 23 ctx.fill(); 24 ctx.closePath(); 25 } 26 27 window.onload = function () { 28 var c = document.getElementById("myCanvas"); 29 var ctx = c.getContext("2d"); 30 ctx.clearRect(0, 0, c.width, c.height); 31 var width = 300*1.5; 32 var height = 200*1.5; sY = 50; step = 10*1.5; ctx.beginPath(); 38 ctx.lineWidth = 1; 39 ctx.fillStyle = "red"; 40 ctx.fillRect(sX, sY, width, height); 41 ctx.closePath(); bigStarCenterX = sX + 5 * step; 44 var bigStarCenterY = sY + 5 * step; drawStar(ctx, bigStarCenterX, bigStarCenterY, bigStarRadius); smallStarRadius = (height * 1 / 10) / 2; smallStarCenterX_1 = sX + 10 * step; 51 var smallStarCenterY_1 = sY + 2 * step; 52 drawStar(ctx, smallStarCenterX_1, smallStarCenterY_1, smallStarRadius); 53 var smallStarCenterX_2 = sX + 12 * step; 54 var smallStarCenterY_2 = sY + 4 * step; 55 drawStar(ctx, smallStarCenterX_2, smallStarCenterY_2, smallStarRadius); 56 var smallStarCenterX_3 = sX + 12 * step; 57 var smallStarCenterY_3 = sY + 7 * step; 58 drawStar(ctx, smallStarCenterX_3, smallStarCenterY_3, smallStarRadius); 59 var smallStarCenterX_4 = sX + 10 * step; 60 var smallStarCenterY_4 = sY + 9 * step; 61 drawStar(ctx, smallStarCenterX_4, smallStarCenterY_4, smallStarRadius); 62 }; 63 </script>

View Code

 

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • HTML5 学习总结(四)——canvas绘图、WebGL、SVG - 张果

    HTML5 学习总结(四)——canvas绘图、WebGL、SVG - 张果

    2016-12-01 13:00

  • Html5 布局方式 - 飞翔的月亮

    Html5 布局方式 - 飞翔的月亮

    2016-12-01 11:00

  • HTML5中Video和Audio - 进击的小前端

    HTML5中Video和Audio - 进击的小前端

    2016-12-01 10:00

  • Html5 绘制旋转的太极图 - 飞翔的月亮

    Html5 绘制旋转的太极图 - 飞翔的月亮

    2016-11-30 17:00

网友点评
a