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"); var context = canvas.getContext("2d"); //使用context繪製 canvas.width = window.innerWidth; canvas.height = window.innerHeight; //context.fillStyle = "black"; /*線性漸變*/ var linearGrad = context.createLinearGradient(0, 0, 0, 700); linearGrad.addColorStop(0.0, "#444"); linearGrad.addColorStop(1.0, "#000"); context.fillStyle = linearGrad; context.fillRect(0, 0, canvas.width, canvas.height); /*迴圈顯示多少個星星*/ function run() { for (var i = 0; i < 123; i++) { var r = Math.random() * 5 + 1; drawStar(context, r, 2 * r, Math.random() * canvas.width, Math.random() * canvas.height); } } run(); /*繪製五角星*/ function drawStar(cxt, r, R, x, y) { cxt.beginPath(); var str = ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).slice(-6); //隨機生成 for (var i = 0; i < 5; i++) { cxt.lineTo(Math.cos((18 + i * 72) / 180 * Math.PI) * R + x, -Math.sin((18 + i * 72) / 180 * Math.PI) * R + y); cxt.lineTo(Math.cos((54 + i * 72) / 180 * Math.PI) * r + x, -Math.sin((54 + i * 72) / 180 * Math.PI) * r + y); } cxt.fillStyle = "#" + str; cxt.strokeStyle = "#" + str; cxt.closePath(); cxt.fill(); cxt.stroke(); } </script> </body> </html>
相關文章
- canvas繪製扇形程式碼例項Canvas
- canvas 繪製雞蛋程式碼例項Canvas
- canvas繪製網格程式碼例項Canvas
- canvas繪製笑臉程式碼例項Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas繪製米字旗程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製拋物線程式碼例項Canvas線程
- canvas繪製機器貓程式碼例項Canvas
- canvas繪製夜空小屋效果程式碼例項Canvas
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas繪製圓角矩形程式碼例項Canvas
- canvas繪製橢圓效果程式碼例項Canvas
- canvas繪製熊貓頭像程式碼例項Canvas
- canvas繪製魚吃豆效果程式碼例項Canvas
- canvas繪製雪花飄落效果程式碼例項Canvas
- canvas繪製圓形鐘錶程式碼例項Canvas
- canvas繪製實心圓形程式碼例項Canvas
- canvas繪製網狀圓圈程式碼例項Canvas
- canvas繪製機器貓頭像程式碼例項Canvas
- canvas繪製矩形並填充顏色程式碼例項Canvas
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- canvas繪製憤怒小鳥形象程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- canvas繪製五角星程式碼例項Canvas
- canvas繪製帶有刻度的座標系程式碼例項Canvas
- css繪製圓形程式碼例項CSS
- svg繪製半圓程式碼例項SVG
- SVG拖動繪製矩形程式碼例項SVG
- jQuery繪製網格效果程式碼例項jQuery
- CSS3繪製菱形程式碼例項CSSS3
- canvas刮刮樂程式碼例項Canvas
- css3繪製月牙效果程式碼例項CSSS3
- js按住滑鼠繪製線條程式碼例項JS
- javascript繪製心形圖案程式碼例項JavaScript
- canvas畫圖程式碼例項Canvas
- canvas小遊戲程式碼例項Canvas遊戲
- canvas火焰效果程式碼例項Canvas