Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body,html{ background-color: #000000; overflow: hidde; } #canvas { display: block; width: 100%; height:100%; }
JavaScript
function random(low, high) { return Math.random() * (high - low) + low; } class Visual { constructor() { this.canvas = document.querySelector('#canvas'); this.context = this.canvas.getContext('2d'); this.canvasWidth = 0; this.canvasHeight = 0; this.particleLength = 150; this.particles = []; this.particleMaxRadius = 8; this.handleMouseMoveBind = this.handleMouseMove.bind(this); this.handleClickBind = this.handleClick.bind(this); this.handleResizeBind = this.handleResize.bind(this); this.initialize(); this.render(); } initialize() { this.resizeCanvas(); for (let i = 0; i < this.particleLength; i++) { this.particles.push(this.createParticle(i)); } this.bind(); } bind() { document.body.addEventListener('mousemove', this.handleMouseMoveBind, false); document.body.addEventListener('click', this.handleClickBind, false); window.addEventListener('resize', this.handleResizeBind, false); } unbind() { document.body.removeEventListener('mousemove', this.handleMouseMoveBind, false); document.body.removeEventListener('click', this.handleClickBind, false); window.removeEventListener('resize', this.handleResizeBind, false); } handleMouseMove(e) { this.enlargeParticle(e.clientX, e.clientY); } handleClick(e) { this.burstParticle(e.clientX, e.clientY); } handleResize() { this.resizeCanvas(); } resizeCanvas() { this.canvasWidth = document.body.offsetWidth; this.canvasHeight = document.body.offsetHeight; this.canvas.width = this.canvasWidth * window.devicePixelRatio; this.canvas.height = this.canvasHeight * window.devicePixelRatio; this.context = this.canvas.getContext('2d'); this.context.scale(window.devicePixelRatio, window.devicePixelRatio); } createParticle(id, isRecreate) { const radius = random(1, this.particleMaxRadius); const x = isRecreate ? -radius - random(this.particleMaxRadius * 2, this.canvasWidth) : random(0, this.canvasWidth); let y = random(this.canvasHeight / 2 - 150, this.canvasHeight / 2 + 150); y += random(-100, 100); const alpha = random(0.05, 1); return { id: id, x: x, y: y, startY: y, radius: radius, defaultRadius: radius, startAngle: 0, endAngle: Math.PI * 2, alpha: alpha, color: { r: random(0, 100), g: random(0, 100), b: 255 }, speed: alpha + 1, amplitude: random(50, 200), isBurst: false }; } drawParticles() { this.particles.forEach(particle => { // 位置情?更新 this.moveParticle(particle); // particle描画 this.context.beginPath(); this.context.fillStyle = `rgba(${particle.color.r}, ${particle.color.g}, ${particle.color.b}, ${particle.alpha})`; this.context.arc(particle.x, particle.y, particle.radius, particle.startAngle, particle.endAngle); this.context.fill(); }); } moveParticle(particle) { particle.x += particle.speed; particle.y = particle.startY + particle.amplitude * Math.sin(((particle.x / 5) * Math.PI) / 180); } enlargeParticle(clientX, clientY) { this.particles.forEach(particle => { if (particle.isBurst) return; const distance = Math.hypot(particle.x - clientX, particle.y - clientY); if (distance <= 100) { const scaling = (100 - distance) / 1.5; TweenMax.to(particle, 0.5, { radius: particle.defaultRadius + scaling, ease: Power2.easeOut }); } else { TweenMax.to(particle, 0.5, { radius: particle.defaultRadius, ease: Power2.easeOut }); } }); } burstParticle(clientX, clientY) { this.particles.forEach(particle => { const distance = Math.hypot(particle.x - clientX, particle.y - clientY); if (distance <= 100) { particle.isBurst = true; TweenMax.to(particle, 0.5, { radius: particle.defaultRadius + 200, alpha: 0, ease: Power2.easeOut, onComplete: () => { this.particles[particle.id] = this.createParticle(particle.id, true); } }); } }); } render() { // canvas初期化 this.context.clearRect(0, 0, this.canvasWidth + this.particleMaxRadius * 2, this.canvasHeight); // 绘制particle this.drawParticles(); // 从画面消失后换成新的particle this.particles.forEach(particle => { if (particle.x - particle.radius >= this.canvasWidth) { this.particles[particle.id] = this.createParticle(particle.id, true); } }); requestAnimationFrame(this.render.bind(this)); } } new Visual();
粒子
时间
文字
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号