canvas繪製五角星程式碼例項
分享一段程式碼例項,它實現了利用canvas繪製五角星的功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> </head> <body> <canvas id="canvas"></canvas> <script> var canvas = document.getElementById("canvas"); canvas.width = 600; canvas.height = 600; var ctx = canvas.getContext("2d"); var num = 0; var timer = setInterval(function() { if (num < 36) { num++; var rot = Math.random() * 30 + 10; var x = Math.cos(num * Math.PI / 180 * 10 + 90) * 180 + 200; var y = Math.sin(num * Math.PI / 180 * 10 + 90) * 180 + 300; var R = Math.ceil(Math.random() * 255); var G = Math.ceil(Math.random() * 255); var B = Math.ceil(Math.random() * 255); var color = "#" + R.toString(16) + "" + G.toString(16) + "" + B.toString(16); draw(ctx, 18, rot, x, y, { "strokeStyle": color, "lineWidth": "1", "fillStyle": color, "run": function(o) { o.fill(); } }) } else { clearInterval(timer); } }, 1000 / 60) function draw(ctx, width, rot, dy, dx, options) { if (!width) return false; var dy = dy || 0, dx = dx || 0, rot = rot || 0; ctx.beginPath(); for (var i = 0; i < 5; i++) { var deg1x = Math.cos((18 + i * 72) * Math.PI / 180 - rot) * width + dx; var deg1y = -Math.sin((18 + i * 72) * Math.PI / 180 - rot) * width + dy; var deg2x = Math.cos((54 + i * 72) * Math.PI / 180 - rot) * width / 2 + dx; var deg2y = -Math.sin((54 + i * 72) * Math.PI / 180 - rot) * width / 2 + dy; ctx.lineTo(deg1x, deg1y); ctx.lineTo(deg2x, deg2y); } if (options && typeof options == "object") { for (var i in options) { if (i != "run") { if (typeof options<i> == "string") { ctx<i> = options<i>; } } else if (typeof options<i> == "function") { options.run(ctx); } } } ctx.closePath(); ctx.stroke(); } </script> </body> </html>
相關文章
- canvas繪製扇形程式碼例項Canvas
- canvas繪製網格程式碼例項Canvas
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- css繪製圓形程式碼例項CSS
- SVG拖動繪製矩形程式碼例項SVG
- canvas刮刮樂程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型
- canvas氣泡上浮效果程式碼例項Canvas
- CSS3繪製太極圖程式碼例項詳解CSSS3
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- UML類圖繪製例項
- 例項QT程式 —— Qt自繪製小時鐘QT
- E-R圖繪製例項
- 資料流圖繪製例項
- Python例項:僅繪製圖例而不繪製實際的圖形Python
- canvas 繪製扇形Canvas
- canvas 繪製文字Canvas
- canvas 繪製矩形Canvas
- 小程式canvas居中剪裁繪製圖片Canvas
- canvas 繪製圓形Canvas
- canvas繪製流星效果Canvas
- canvas繪製直線Canvas
- canvas lineWidth 繪製原理Canvas
- canvas lineWidth繪製原理Canvas
- canvas 繪製線條Canvas
- 原生Canvas繪製餅圖,我是不是被騙程式碼了Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製矩形框Canvas
- canvas繪製風車效果Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製文字詳解Canvas
- canvas繪製笑臉表情Canvas
- Canvas 繪製雷達圖Canvas
- QT OPENGL 與 shader 繪製展示視訊程式碼例項 OPenGL直接顯示YUV資料QT