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" style="background-color:black">您的瀏覽器暫不支援canvas</canvas> <script> var canvas = document.getElementById("canvas"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; var ctx = canvas.getContext("2d"); var balls = []; function draw(ball) { ctx.beginPath(); ctx.arc(ball.x, ball.y, ball.r, 0, 2 * Math.PI); ctx.globalAlpha = 0.7; ctx.fillStyle = "#ffffff"; ctx.shadowColor = "#ffffff"; ctx.shadowBlur = 20; ctx.fill(); } function star() { for (var i = 0; i < 1; i++) { var ball = { x: Math.random() * canvas.width / 2 + canvas.width / 2, y: 0, r: Math.random() * 4, vx: Math.random() - 0.5, vy: Math.random() + 1, } balls.push(ball); } drawball(); } function drawball() { ctx.clearRect(0, 0, canvas.width, canvas.height); for (var i = 0; i < balls.length; i++) { balls[i].x += balls[i].vx * 5; //改變x軸位置 balls[i].y += balls[i].vy * 2; //Y軸 balls[i].index = i; if (balls[i].y > canvas.height / 1.7) { balls.splice(balls[i].index, 1); continue; } draw(balls[i]); } } setInterval(function() { star(); }, 10); </script> </body> </html>
相關文章
- canvas實現的雪花飄落效果程式碼例項Canvas
- jQuery雪花飄落效果程式碼例項jQuery
- canvas實現的雪花飄落效果Canvas
- canvas繪製圓環效果程式碼例項Canvas
- canvas繪製箭頭效果程式碼例項Canvas
- canvas繪製夜空小屋效果程式碼例項Canvas
- canvas繪製鐘錶效果程式碼例項Canvas
- canvas繪製橢圓效果程式碼例項Canvas
- canvas繪製魚吃豆效果程式碼例項Canvas
- js純程式碼為網站站點新增雪花飄落效果JS網站
- 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
- jQuery繪製網格效果程式碼例項jQuery
- canvas繪製機器貓頭像程式碼例項Canvas
- canvas繪製矩形並填充顏色程式碼例項Canvas
- canvas繪製貝濟埃曲線程式碼例項Canvas線程
- canvas繪製憤怒小鳥形象程式碼例項Canvas
- HTML5利用canvas繪製矩形程式碼例項HTMLCanvas
- canvas繪製五角星程式碼例項Canvas
- canvas火焰效果程式碼例項Canvas
- css3繪製月牙效果程式碼例項CSSS3
- canvas字母雨效果程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- canvas擺動效果程式碼例項Canvas
- canvas繪製帶有刻度的座標系程式碼例項Canvas
- canvas水位進度效果程式碼例項Canvas
- canvas原型鐘錶效果程式碼例項Canvas原型