Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body { margin: 0; padding: 0; background: #000000; overflow: hidden; }
JavaScript
// fun options! const PARTICLES_PER_FIREWORK = 150; // 100 - 400 or try 1000 const FIREWORK_CHANCE = 0.02; // percentage, set to 0 and click instead const BASE_PARTICLE_SPEED = 0.6; // between 0-4, controls the size of the overall fireworks const FIREWORK_LIFESPAN = 600; // ms const PARTICLE_INITIAL_SPEED = 4.5; // 2-8 // not so fun options =\ const GRAVITY = 9.8; const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let particles = []; let disableAutoFireworks = false; let resetDisable = 0; let loop = () => { if (!disableAutoFireworks && Math.random() < FIREWORK_CHANCE) { createFirework(); } ctx.clearRect(0, 0, canvas.width, canvas.height); particles.forEach((particle, i) => { particle.animate(); particle.render(); if (particle.y > canvas.height || particle.x < 0 || particle.x > canvas.width || particle.alpha <= 0 ) { particles.splice(i, 1); } }); requestAnimationFrame(loop); }; let createFirework = ( x = Math.random() * canvas.width, y = Math.random() * canvas.height ) => { let speed = (Math.random() * 2) + BASE_PARTICLE_SPEED; let maxSpeed = speed; let red = ~~(Math.random() * 255); let green = ~~(Math.random() * 255); let blue = ~~(Math.random() * 255); // use brighter colours red = (red < 150 ? red + 150 : red); green = (green < 150 ? green + 150 : green); blue = (blue < 150 ? blue + 150 : blue); // inner firework for (let i = 0; i < PARTICLES_PER_FIREWORK; i++) { let particle = new Particle(x, y, red, green, blue, speed); particles.push(particle); maxSpeed = (speed > maxSpeed ? speed : maxSpeed); } // outer edge particles to make the firework appear more full for (let i = 0; i < 40; i++) { let particle = new Particle(x, y, red, green, blue, maxSpeed, true); particles.push(particle); } }; class Particle { constructor( x = 0, y = 0, red = ~~(Math.random() * 255), green = ~~(Math.random() * 255), blue = ~~(Math.random() * 255), speed, isFixedSpeed ) { this.x = x; this.y = y; this.red = red; this.green = green; this.blue = blue; this.alpha = 0.05; this.radius = 1 + Math.random(); this.angle = Math.random() * 360; this.speed = (Math.random() * speed) + 0.1; this.velocityX = Math.cos(this.angle) * this.speed; this.velocityY = Math.sin(this.angle) * this.speed; this.startTime = (new Date()).getTime(); this.duration = Math.random() * 300 + FIREWORK_LIFESPAN; this.currentDiration = 0; this.dampening = 30; // slowing factor at the end this.colour = this.getColour(); if (isFixedSpeed) { this.speed = speed; this.velocityY = Math.sin(this.angle) * this.speed; this.velocityX = Math.cos(this.angle) * this.speed; } this.initialVelocityX = this.velocityX; this.initialVelocityY = this.velocityY; } animate() { this.currentDuration = (new Date()).getTime() - this.startTime; // initial speed kick if (this.currentDuration <= 200) { this.x += this.initialVelocityX * PARTICLE_INITIAL_SPEED; this.y += this.initialVelocityY * PARTICLE_INITIAL_SPEED; this.alpha += 0.01; this.colour = this.getColour(240, 240, 240, 0.9); } else { // normal expansion this.x += this.velocityX; this.y += this.velocityY; this.colour = this.getColour(this.red, this.green, this.blue, 0.4 + (Math.random() * 0.3)); } this.velocityY += GRAVITY / 1000; // slow down particles at the end if (this.currentDuration >= this.duration) { this.velocityX -= this.velocityX / this.dampening; this.velocityY -= this.velocityY / this.dampening; } if (this.currentDuration >= this.duration + this.duration / 1.1) { // fade out at the end this.alpha -= 0.02; this.colour = this.getColour(); } else { // fade in during expansion if (this.alpha < 1) { this.alpha += 0.03; } } } render() { ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, true); ctx.lineWidth = this.lineWidth; ctx.fillStyle = this.colour; ctx.shadowBlur = 8; ctx.shadowColor = this.getColour(this.red + 150, this.green + 150, this.blue + 150, 1); ctx.fill(); } getColour(red, green, blue, alpha) { return `rgba(${red || this.red}, ${green || this.green}, ${blue || this.blue}, ${alpha || this.alpha})`; } } let updateCanvasSize = () => { canvas.width = window.innerWidth; canvas.height = window.innerHeight; }; // run it! updateCanvasSize(); $(window).resize(updateCanvasSize); $(canvas).on('click', (e) => { createFirework(e.clientX, e.clientY); // stop fireworks when clicked, re-enable after short time disableAutoFireworks = true; clearTimeout(resetDisable); resetDisable = setTimeout(() => { disableAutoFireworks = false; }, 5000); }); loop();
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>逼真的烟花-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号