canvas實現具有粒子效果的動態進度條
分享一段程式碼例項,它實現了動態進度條效果,並且帶有粒子效果。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style> canvas { background: #181818; box-shadow: 0 0 0 1px #282828; bottom: 0; left: 0; margin: auto; position: absolute; right: 0; top: 0; } </style> </head> <body> <script> var canvas = document.createElement('canvas'), ctx = canvas.getContext('2d'), width = 400 height = 100, loaded = 0, loaderSpeed = 0.6, loaderWidth = 310, loaderHeight = 16, loaderX = width / 2 - loaderWidth / 2, loaderY = height / 2 - loaderHeight / 2, particles = [], particleLift = 180, particleRate = 4, hueStart = 0, hueEnd = 120, hue = 0, gravity = 0.12; document.body.appendChild(canvas); canvas.width = width; canvas.height = height; ctx.globalCompositeOperation = 'lighter'; function rand(rMi, rMa) { return ~~((Math.random() * (rMa - rMi + 1)) + rMi); } function updateLoader() { if (loaded < 100) { loaded += loaderSpeed; } else { loaded = 0; } } function renderLoader() { ctx.fillStyle = '#000'; ctx.fillRect(loaderX, loaderY, loaderWidth, loaderHeight); hue = hueStart + (loaded / 100) * (hueEnd - hueStart); var newWidth = (loaded / 100) * loaderWidth; ctx.fillStyle = 'hsla(' + hue + ', 100%, 40%, 1)'; ctx.fillRect(loaderX, loaderY, newWidth, loaderHeight); ctx.fillStyle = '#444'; ctx.fillRect(loaderX, loaderY, newWidth, loaderHeight / 2); } function Particle() { this.x = loaderX + ((loaded / 100) * loaderWidth) - rand(0, 1); this.y = height / 2 + rand(0, loaderHeight) - loaderHeight / 2; this.vx = (rand(0, 4) - 2) / 100; this.vy = (rand(0, particleLift) - particleLift * 2) / 100; this.width = rand(1, 4) / 2; this.height = rand(1, 4) / 2; this.hue = hue; } Particle.prototype.update = function(i) { this.vx += (rand(0, 6) - 3) / 100; this.vy += gravity; this.x += this.vx; this.y += this.vy; if (this.y > height) { particles.splice(i, 1); } }; Particle.prototype.render = function() { ctx.fillStyle = 'hsla(' + this.hue + ', 100%, ' + rand(50, 70) + '%, ' + rand(20, 100) / 100 + ')'; ctx.fillRect(this.x, this.y, this.width, this.height); }; function createParticles() { var i = particleRate; while (i--) { this.particles.push(new Particle()); } } function updateParticles() { var i = particles.length; while (i--) { var p = particles<i>; p.update(i); }; } function renderParticles() { var i = particles.length; while (i--) { var p = particles<i>; p.render(); } } function clearCanvas() { ctx.clearRect(0, 0, width, height); } function loop() { requestAnimationFrame(loop); clearCanvas(); createParticles(); updateLoader(); updateParticles(); renderLoader(); renderParticles(); } loop(); </script> </body> </html>
相關文章
- css實現螺紋動態進度條效果CSS
- Canvas 實現炫麗的粒子運動效果(粒子生成文字)Canvas
- canvas環形進度條效果Canvas
- 超炫!HTML5 粒子效果進度條HTML
- JavaScript 動態進度條效果詳解JavaScript
- HTML5超炫酷粒子效果的進度條HTML
- js實現動態進度條詳解JS
- iOS實現音訊進度條效果iOS音訊
- 實現環形進度條效果【一】
- Qt實現炫酷啟動圖-動態進度條QT
- CSS3實現原型進度條效果CSSS3原型
- canvas 畫進度條Canvas
- html5canvas: 教你實現知乎登入動態粒子背景HTMLCanvas
- canvas 中普通動效與粒子動效的實現Canvas
- canvas環形百分比進度條效果Canvas
- Javascript 及 CSS3 實現進度條效果JavaScriptCSSS3
- vue中頁面載入進度條效果的實現Vue
- js結合progress標籤實現的進度條效果JS
- canvas實現的動態心形效果程式碼例項Canvas
- jQuery實進度條效果詳解jQuery
- jquery實現的豎向動態柱狀條效果jQuery
- canvas具有漸變效果的矩形Canvas
- jQuery監聽檔案上傳實現進度條效果jQuery
- css3實現邊框具有動態環形遮罩效果CSSS3遮罩
- canvas的簡單圓形進度條Canvas
- canvas動態時鐘效果Canvas
- javascript實現的帶有百分比的進度條效果JavaScript
- 滑鼠懸浮底部具有動態延伸條效果程式碼例項
- 不可思議的純 CSS 滾動進度條效果CSS
- canvas錐形漸變進度條Canvas
- Canvas + JavaScript 製作圖片粒子效果CanvasJavaScript
- golang 進度條功能實現Golang
- clip實現圓環進度條
- 命令列進度條實現命令列
- Flutter動畫實現粒子漂浮效果Flutter動畫
- css3實現線條環形動態運動效果CSSS3
- jQuery 進度條效果程式碼jQuery
- CSS快遞進度條效果CSS