canvas實現的雪花飄落效果程式碼例項
分享一段程式碼例項,它利用canvas實現了雪花飄落效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> * { padding: 0; margin: 0; } #box { margin: 0 auto; width: 600px; position: relative; } #fcan { position: absolute; top: 0; z-index: 100; } #bg { position: absolute; top: 0; } #bg img { width: 600px; height: 400px; } </style> </head> <body> <div id="box"> <canvas id="fcan" width="600" height="400" ></canvas> </div> <script> var rains = []; var can = document.getElementById('fcan'); var ct = can.getContext('2d'); init(); setInterval(function() { rainsNum(); ct.clearRect(0, 0, 600, 400); init(); dropDown(); }, 50); //初始化 function init() { ct.rect(0, 0, 600, 400); ct.fillStyle = '#000'; ct.fill(); } //產生隨機雨滴 function createRain() { var ix = Math.random() * 600; var iy = Math.random() * 50; var ir = Math.random() * 4; var ivy = Math.random() * 5 + 2; var rain = { x: ix, y: iy, r: ir, vy: ivy, g: 8 }; rains.push(rain); } function rainsNum() { createRain(); } //落下 function dropDown() { var len = rains.length; for (var i = 0; i < len; i++) { drawRain(rains<i>.x, rains<i>.y, rains<i>.r, ct); rains<i>.v += rains<i>.g; rains<i>.y += rains<i>.vy; } } //畫雪花 function drawRain(x, y, r, obj) { obj.fillStyle = '#fff'; obj.beginPath(); obj.arc(x, y, r, 0, Math.PI * 2, true); obj.closePath(); obj.fill(); } </script> </body> </html>
相關文章
- canvas繪製雪花飄落效果程式碼例項Canvas
- canvas實現的雪花飄落效果Canvas
- jQuery雪花飄落效果程式碼例項jQuery
- js純程式碼為網站站點新增雪花飄落效果JS網站
- canvas實現的鋸齒效果程式碼例項Canvas
- canvas實現的鐘表效果程式碼例項Canvas
- canvas實現的驗證碼效果程式碼例項Canvas
- Android自定義View——從零開始實現雪花飄落效果AndroidView
- 利用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
- canvas繪製圓環效果程式碼例項Canvas
- canvas螞蟻線效果程式碼例項Canvas
- canvas實現的變幻線程式碼例項Canvas線程
- canvas實現的多邊形程式碼例項Canvas
- canvas實現的千輪眼程式碼例項Canvas
- JavaScript 雪花飄舞效果詳解JavaScript
- jquery實現的選項卡效果例項程式碼jQuery