canvas繪製流星效果
分享一段程式碼,它利用canvas實現了流星劃過夜空的效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> * { margin: 0; padding: 0; } html, body { height: 100%; overflow: hidden; } #cvs { position: fixed; z-index: -1; } </style> </head> <body> <canvas id="cvs"></canvas> <script type="text/javascript"> var cvs = document.getElementById('cvs'); cvs.width = window.innerWidth; cvs.height = window.innerHeight; var ctx = cvs.getContext('2d'); var starArr = [], meteorArr = []; for (var i = 0; i < 50; i++) { starArr.push({ x: parseInt(Math.random() * cvs.width), y: parseInt(Math.random() * cvs.height), r: parseInt(Math.random() * 7) + 3, opacity: 1, opacitySpeed: 0.02 //i % 3 == 0 ? 0.01 : 0 }); } for (var i = 0; i < 50; i++) { meteorArr.push({ x: Math.random() * cvs.width * 20, y: -Math.random() * cvs.height, r: 0.1, opacity: 1 }); } setInterval(function() { ctx.fillStyle = 'rgb(20,20,20)'; ctx.fillRect(0, 0, cvs.width, cvs.height); ctx.lineCap = 'round'; for (var i = 0; i < starArr.length; i++) { var x = starArr[i].x, y = starArr[i].y, r = starArr[i].r, opacity = starArr[i].opacity; ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(x + r / 6, y + r / 3); ctx.lineTo(x + r / 2, y + r / 2); ctx.lineTo(x + r / 6, y + r / 3 * 2); ctx.lineTo(x, y + r); ctx.lineTo(x - r / 6, y + r / 3 * 2); ctx.lineTo(x - r / 2, y + r / 2); ctx.lineTo(x - r / 6, y + r / 3); ctx.lineTo(x, y); ctx.strokeStyle = 'rgba(255,255,255,' + opacity + ')'; ctx.fillStyle = 'rgba(255,255,255,' + opacity + ')'; ctx.fill(); ctx.stroke(); ctx.closePath(); starArr[i].opacity -= starArr[i].opacitySpeed; if (starArr[i].opacity <= 0.2 || starArr[i].opacity >= 1) { starArr[i].opacitySpeed = -starArr[i].opacitySpeed; } } for (var i = 0; i < meteorArr.length; i++) { var x = meteorArr[i].x, y = meteorArr[i].y, r = meteorArr[i].r; for (var j = r; j < 70; j++) { ctx.beginPath(); ctx.fillStyle = 'rgb(255,255,255)' ctx.arc(x, y, r, 0, 360 * Math.PI / 180, true); r += 0.02; x -= 2; y += 1; ctx.fill(); ctx.closePath(); } meteorArr[i].x -= 16; meteorArr[i].y += 8; if (meteorArr[i].y >= cvs.height + 50) { meteorArr[i].x = Math.random() * cvs.width * 20; meteorArr[i].y = -Math.random() * cvs.height; } } }, 20); </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 lineWidth 繪製原理Canvas
- canvas lineWidth繪製原理Canvas
- 用canvas實現流星特效Canvas特效
- Canvas 繪製雷達圖Canvas
- canvas 繪製圓角矩形Canvas
- canvas 繪製雙線技巧Canvas
- canvas繪製矩形框Canvas
- canvas 繪製矩形缺角Canvas
- canvas 繪製文字詳解Canvas
- canvas繪製笑臉表情Canvas
- Canvas(3)---繪製餅狀圖Canvas
- canvas繪製動畫的技巧Canvas動畫
- canvas系列教程之繪製矩形Canvas
- canvas 繪製立體圓環Canvas
- canvas繪製不重合的圓Canvas
- canvas繪製sin正弦曲線Canvas
- Canvas繪製一個類似老版支付寶信用分儀表盤效果Canvas
- canvas繪製圖案是重疊繪製而不是重置Canvas
- canvas 繪製圖案是重疊繪製而不是重置Canvas
- canvas實現手動繪製矩形Canvas
- 在Canvas中繪製Geojson資料CanvasJSON