Canvas碎纸屑闪烁动画特效

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas碎纸屑闪烁动画特效</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            overflow: hidden;
            background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
            display: flex;
            justify-content: center;
            align-items: center;
            font-family: Arial, sans-serif;
        }

        canvas {
            display: block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
        }

        .content {
            position: relative;
            z-index: 2;
            text-align: center;
            color: white;
            padding: 30px;
            background: rgba(0, 0, 0, 0.3);
            border-radius: 15px;
            max-width: 80%;
        }

        h1 {
            font-size: 3rem;
            margin-bottom: 20px;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
        }

        p {
            font-size: 1.2rem;
            margin-bottom: 30px;
        }

        .btn {
            padding: 12px 30px;
            background: linear-gradient(45deg, #ff0a54, #ff477e);
            color: white;
            border: none;
            border-radius: 50px;
            font-size: 1.1rem;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 5px 15px rgba(255, 10, 84, 0.4);
        }

        .btn:hover {
            transform: translateY(-3px);
            box-shadow: 0 8px 20px rgba(255, 10, 84, 0.6);
        }

        .btn:active {
            transform: translateY(0);
        }

        /* 版权信息 */
        .footer {
            position: absolute;
            bottom: 20px;
            right: 20px;
            color: rgba(255, 255, 255, 0.5);
            font-size: 12px;
            z-index: 3;
        }

        .footer a {
            color: #00f5d4;
            text-decoration: none;
        }

        .footer a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <canvas id="confetti"></canvas>
    
    <div class="content">
        <h1>碎纸屑特效</h1>
        <p>点击按钮触发炫酷的碎纸屑动画效果</p>
        <button class="btn" id="triggerBtn">触发动画</button>
    </div>
    
      <script>
        document.addEventListener('DOMContentLoaded', function() {
            const canvas = document.getElementById('confetti');
            const ctx = canvas.getContext('2d');
            const triggerBtn = document.getElementById('triggerBtn');
            
            // 设置画布大小为窗口大小
            function resizeCanvas() {
                canvas.width = window.innerWidth;
                canvas.height = window.innerHeight;
            }
            
            window.addEventListener('resize', resizeCanvas);
            resizeCanvas();
            
            // 碎纸屑粒子类
            class Particle {
                constructor(x, y) {
                    this.x = x;
                    this.y = y;
                    this.size = Math.random() * 8 + 3;
                    this.density = Math.random() * 30 + 1;
                    this.color = this.getRandomColor();
                    this.velocityX = Math.random() * 5 - 2.5;
                    this.velocityY = Math.random() * 5 - 2.5;
                    this.rotation = Math.random() * 360;
                    this.rotationSpeed = Math.random() * 2 - 1;
                    this.opacity = 1;
                    this.fadeOut = false;
                    this.shape = Math.random() > 0.5 ? 'rect' : 'circle';
                }
                
                getRandomColor() {
                    const colors = [
                        '#ff0a54', '#ff477e', '#ff5c8a', '#ff7096', 
                        '#ff85a1', '#f991b7', '#fbb1bd', '#f9bec7',
                        '#00f5d4', '#00bbf9', '#7bf1a8', '#9b5de5'
                    ];
                    return colors[Math.floor(Math.random() * colors.length)];
                }
                
                update() {
                    this.velocityY += 0.1; // 重力
                    this.velocityX *= 0.99; // 空气阻力
                    this.velocityY *= 0.99;
                    
                    this.x += this.velocityX;
                    this.y += this.velocityY;
                    this.rotation += this.rotationSpeed;
                    
                    // 边界检测
                    if (this.y > canvas.height) {
                        this.velocityY *= -0.6; // 反弹
                        this.y = canvas.height;
                    }
                    
                    if (this.fadeOut && this.opacity > 0) {
                        this.opacity -= 0.01;
                    }
                }
                
                draw() {
                    ctx.save();
                    ctx.globalAlpha = this.opacity;
                    ctx.translate(this.x, this.y);
                    ctx.rotate(this.rotation * Math.PI / 180);
                    
                    if (this.shape === 'rect') {
                        ctx.fillStyle = this.color;
                        ctx.fillRect(-this.size/2, -this.size/2, this.size, this.size);
                    } else {
                        ctx.beginPath();
                        ctx.arc(0, 0, this.size/2, 0, Math.PI * 2);
                        ctx.fillStyle = this.color;
                        ctx.fill();
                    }
                    
                    ctx.restore();
                }
            }
            
            let particles = [];
            let animationId = null;
            let isAnimating = false;
            
            // 创建碎纸屑
            function createConfetti(x, y, count = 100) {
                for (let i = 0; i < count; i++) {
                    particles.push(new Particle(x, y));
                }
            }
            
            // 动画循环
            function animate() {
                ctx.clearRect(0, 0, canvas.width, canvas.height);
                
                // 更新和绘制所有粒子
                for (let i = 0; i < particles.length; i++) {
                    particles[i].update();
                    particles[i].draw();
                }
                
                // 移除透明度为0的粒子
                particles = particles.filter(p => p.opacity > 0);
                
                // 如果还有粒子,继续动画
                if (particles.length > 0) {
                    animationId = requestAnimationFrame(animate);
                } else {
                    isAnimating = false;
                    triggerBtn.textContent = '触发动画';
                }
            }
            
            // 触发动画
            triggerBtn.addEventListener('click', function() {
                if (isAnimating) {
                    // 如果正在动画,让所有粒子淡出
                    particles.forEach(p => p.fadeOut = true);
                    triggerBtn.textContent = '动画结束中...';
                } else {
                    // 创建新粒子
                    const centerX = canvas.width / 2;
                    const centerY = canvas.height / 2;
                    createConfetti(centerX, centerY, 150);
                    
                    isAnimating = true;
                    triggerBtn.textContent = '停止动画';
                    
                    // 开始动画
                    if (!animationId) {
                        animate();
                    }
                }
            });
            
            // 初始加载时自动触发一次
            setTimeout(() => {
                triggerBtn.click();
            }, 500);
        });
    </script>
</body>
</html>

js代码 [removed] ;(function() { &#39;use strict&#39;; var c = document.getElementById(&#39;c&#39;); var ctx = c.getContext(&#39;2d&#39;); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var cx = w / 2; var cy = h / 2; var fl = 1000; function prj(obj) { var cz = obj.z fl; if(cz === 0) return; var scl = fl / cz; obj.p.x = cx obj.x * scl; obj.p.y = cy obj.y * scl; obj.s = scl; } var P = function(x, y, z) { this.x = x; this.y = y; this.z = z; this.s = 1; this.cl = 0; this.p = { x: 0, y: 0 }; }; P.prototype = { constructor: P, update: function() { this.z -= 30; }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.scale(this.s, this.s); ctx.fillStyle = &#39;hsla(&#39; this.cl &#39;, 100%, 50%, 0.5)&#39;; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; var M = function(x, y, z) { this.list = []; this.max = 100; this.x = x; this.y = y; this.z = z; this.s = 1; this.p = { x: 0, y: 0 }; this.ax = Math.random() * (Math.PI * 2); this.ay = Math.random() * (Math.PI * 2); this.rx = Math.random() * 100; this.ry = Math.random() * 100; this.cl = Math.random() * 360; this.cls = Math.random(); }; M.prototype = { constructor: M, update: function() { this.cl = this.cls; this.ax = Math.random() * 0.1 - 0.02; this.ay = Math.random() * 0.1 - 0.02; this.x = Math.cos(this.ax) * 100; this.y = Math.sin(this.ay) * 100; this.z = 10; if(this.z > fl) this.z = fl; if(this.list.length < this.max) { if(Math.random() * 100 < 50) { var pp = new P(this.x, this.y, this.z); pp.cl = this.cl; this.list.push(pp); } } else { var pp = this.list.shift(); pp.x = this.x; pp.y = this.y; pp.z = this.z; pp.cl = this.cl; this.list.push(pp); } }, render: function(ctx) { if(this.z <= -fl) return; ctx.save(); ctx.translate(this.p.x, this.p.y); ctx.fillStyle = &#39;green&#39;; ctx.beginPath(); ctx.arc(0, 0, 2, 0, Math.PI * 2); ctx.fill(); ctx.restore(); } }; function update(mv, list) { for(var i = 0; i < list.length; i ) { var p = list[i]; p.update(); prj(p); p.render(ctx); } for(var i = list.length-1; i >= 0; i--) { var p = list[i]; if(p.z <= -fl) continue; if(i === list.length - 1) { ctx.lineWidth = Math.random(); ctx.strokeStyle = &#39;hsl(&#39; mv.cl &#39;, 100%, 50%)&#39;; ctx.beginPath(); ctx.moveTo(p.p.x, p.p.y); } else { ctx.lineTo(p.p.x, p.p.y); } } ctx.stroke(); } var ms = []; for(var i = 0; i < 10; i ) { ms.push(new M( Math.random() * 400 - 200, Math.random() * 400 - 200, Math.random() * 400 - 200)); } requestAnimationFrame(function loop() { requestAnimationFrame(loop); ctx.clearRect(0, 0, w, h); for(var i = 0; i < ms.length; i ) { var m = ms[i]; m.update(); prj(m); update(m, m.list); } }); })(); [removed] 这是一款基于HTML5 Canvas绘制的3D线条延伸动画特效,多彩颜色变幻,非常漂亮!
前端动画素材在网页开发中扮演着重要角色,能够吸引用户注意、提升用户体验。以下是一些常见的前端动画素材的技术实现方式: CSS 动画:使用 CSS 属性(如@keyframes、transition、transform等)来实现动画效果。这种方式简单易用,适合实现简单的动画效果,如过渡、旋转、缩放等。 JavaScript 动画:通过 JavaScript 操作 DOM 元素的样式属性,实现更复杂、交互性更强的动画效果。常见的库包括 jQuery、Anime.js、GreenSock(GSAP)等,它们提供了丰富的动画函数和效果,使动画开发更加高效。 SVG 动画:使用 SVG(可缩放矢量图形)和 SMIL(同步多媒体集成语言)技术创建矢量图形动画,可以实现复杂的矢量图形动画效果,如路径动画、填充动画等。 Canvas 动画:通过 HTML5 Canvas 元素绘制图形,利用 JavaScript 控制绘制过程,实现高度可定制化的动画效果,适用于需要实时渲染的复杂动画场景。 WebGL 动画:基于 WebGL(Web图形库)的 3D 图形渲染技术,可以实现高性能的复杂动画效果,适合开发需要展示 3D 动画的网页。 React 动画库:如果你在使用 React 框架,可以考虑使用像 React Spring、Framer Motion 等专门为 React 设计的动画库,简化动画开发流程。 CSS 预处理器:使用像 Sass、Less 等 CSS 预处理器可以简化 CSS 编写过程,提高样式代码的可维护性,进而对动画效果的实现有所帮助。 综上所述,前端动画素材的技术实现方式多种多样,开发者可以根据项目需求和个人技术偏好选择合适的方式来实现动画效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值