Html5 繪製五星紅旗

來源:互聯網
上載者:User

標籤:canvas   bsp   get   .com   img   draw   思路   html   div   

Html5+JavaScript 在Canvas上繪製五星紅旗,具體思路如所示:

繪製思路在中已有說明,具體代碼如下:

 1 <script type="text/javascript"> 2  3         //繪製五角星,其中一點垂直向上,相隔的點相連,即可繪製。 4         function 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;33             var sX = 50; //其實座標34             var sY = 50; //其實座標35             var step = 10*1.5;36             //繪製矩形37             ctx.beginPath();38             ctx.lineWidth = 1;39             ctx.fillStyle = "red";40             ctx.fillRect(sX, sY, width, height);41             ctx.closePath();42             //繪製大五角星43             var bigStarCenterX = sX + 5 * step;44             var bigStarCenterY = sY + 5 * step;45             var bigStarRadius = (height * 3 / 10) / 2; //外接圓直徑為旗高3/10,半徑要再除以246             drawStar(ctx, bigStarCenterX, bigStarCenterY, bigStarRadius);47             //繪製小五角星48             var smallStarRadius = (height * 1 / 10) / 2; //外接圓直徑為旗高1/10,半徑要再除以249 50             var 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

 

Html5 繪製五星紅旗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.