跳转至内容
0
  • 版块
  • 讨论
  • 最新
  • 标签
  • 热门
  • 顶端
  • 用户
  • 版块
  • 讨论
  • 最新
  • 标签
  • 热门
  • 顶端
  • 用户
皮肤
  • 浅色
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • 深色
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(不使用皮肤)
  • 不使用皮肤
折叠
JIKE社区
  1. 首页
  2. 讨论区
  3. 技术专区
  4. 在网站上展现春节烟花效果,仿百度首页

在网站上展现春节烟花效果,仿百度首页

已定时 已固定 已锁定 已移动 技术专区
1 帖子 1 发布者 306 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • 我是小马甲~M
    我是小马甲~M
    我是小马甲~
    🤫
    发表于 最后由 编辑
    #1

    播放5秒自己停止!

    (function() {
        // 1. 设置画布与透明度
        const canvas = document.createElement('canvas');
        const ctx = canvas.getContext('2d');
        canvas.style.position = 'fixed';
        canvas.style.left = '0';
        canvas.style.top = '0';
        canvas.style.width = '100%';
        canvas.style.height = '100%';
        canvas.style.zIndex = '9999';
        canvas.style.pointerEvents = 'none'; 
        document.body.appendChild(canvas);
    
        let width, height;
        let particles = [];
        let isRunning = true; // 控制运行开关
        const colors = ['#FF1744', '#D500F9', '#3D5AFE', '#00E5FF', '#00E676', '#FFEA00', '#FF3D00'];
    
        function resize() {
            width = canvas.width = window.innerWidth;
            height = canvas.height = window.innerHeight;
        }
        window.addEventListener('resize', resize);
        resize();
    
        class Particle {
            constructor(x, y, color) {
                this.x = x;
                this.y = y;
                this.color = color;
                
                const angle = Math.random() * Math.PI * 2;
                const force = Math.pow(Math.random(), 2) * 10 + 2; 
                
                this.vx = Math.cos(angle) * force;
                this.vy = Math.sin(angle) * force;
    
                this.radius = Math.random() * 2 + 1;
                this.alpha = 1;
                this.decay = Math.random() * 0.015 + 0.015; 
                this.gravity = 0.07;
                this.friction = 0.95;
                
                // 轨迹点:用于在透明背景下模拟流光
                this.history = [];
                for(let i=0; i<6; i++) this.history.push({x: x, y: y});
            }
    
            update() {
                this.history.shift();
                this.history.push({x: this.x, y: this.y});
    
                this.vx *= this.friction;
                this.vy *= this.friction;
                this.vy += this.gravity;
                this.x += this.vx;
                this.y += this.vy;
                this.alpha -= this.decay;
            }
    
            draw() {
                ctx.beginPath();
                ctx.moveTo(this.history[0].x, this.history[0].y);
                for(let i=1; i<this.history.length; i++) {
                    ctx.lineTo(this.history[i].x, this.history[i].y);
                }
                ctx.strokeStyle = this.color;
                ctx.lineWidth = this.radius;
                ctx.lineCap = 'round';
                ctx.globalAlpha = this.alpha;
                ctx.stroke();
            }
        }
    
        function createFirework(x, y) {
            if (!isRunning && particles.length > 500) return; 
            const color = colors[Math.floor(Math.random() * colors.length)];
            for (let i = 0; i < 60; i++) {
                particles.push(new Particle(x, y, color));
            }
        }
    
        // 核心动画循环
        function loop() {
            // 使用 clearRect 保持背景透明
            ctx.clearRect(0, 0, width, height);
    
            for (let i = particles.length - 1; i >= 0; i--) {
                const p = particles[i];
                p.update();
                if (p.alpha <= 0) {
                    particles.splice(i, 1);
                } else {
                    p.draw();
                }
            }
    
            // 如果停止运行且粒子已排空,则彻底停止循环节省资源
            if (!isRunning && particles.length === 0) {
                canvas.remove(); // 移除画布
                return;
            }
            requestAnimationFrame(loop);
        }
    
        // 5秒后停止自动燃放
        setTimeout(() => {
            isRunning = false;
        }, 5000);
    
        // 自动燃放逻辑
        function autoFire() {
            if (isRunning) {
                createFirework(Math.random() * width, Math.random() * (height * 0.5));
                setTimeout(autoFire, Math.random() * 800 + 400);
            }
        }
    
        // 依然保留点击手动燃放(5秒内有效)
        window.addEventListener('mousedown', (e) => {
            if (isRunning) createFirework(e.clientX, e.clientY);
        });
    
        loop();
        autoFire();
    })();
    

    唐宋元明清,连排小工兵!

    1 条回复 最后回复
    1

    你好!看起来您对这段对话很感兴趣,但您还没有一个账号。

    厌倦了每次访问都刷到同样的帖子?您注册账号后,您每次返回时都能精准定位到您上次浏览的位置,并可选择接收新回复通知(通过邮件或推送通知)。您还能收藏书签、为帖子顶,向社区成员表达您的欣赏。

    有了你的建议,这篇帖子会更精彩哦 💗

    注册 登录
    回复
    • 在新帖中回复
    登录后回复
    • 从旧到新
    • 从新到旧
    • 最多赞同


    Popular Topics - 热门主题
    • 阿里云杯垫水杯领取
      ‍H
      ‍
      0
      6
      453

    • 11月12日中国移动灵犀口令是:黄叶落金喜
      我是小马甲~M
      我是小马甲~
      0
      2
      56

    • 浙江农行2元购8元立减金,限购2次净赚12元,11月30日截止(需受邀)
      W
      wythe
      0
      2
      78

    • 支付宝充话费建行卡立减:1元档只付0.5元,广东实测16笔都还有
      C
      chen1026241686
      0
      2
      123

    • 国剧/杀人真相/自首真相 (2026)[剧情 犯罪][许魏洲 郭俊辰 肖燕] 4K
      爱之梦梦爱
      爱之梦梦
      0
      2
      3

    • 想开京东Plus的别错过,手柄页面能刷出50元红包
      W
      wxy
      0
      2
      282


    • 登录

    • 没有帐号? 注册

    • 登录或注册以进行搜索。
    夸克网盘
    5,758 个主题
    百度网盘
    5,215 个主题
    连续剧
    1,823 个主题
    国产剧
    1,162 个主题
    美剧
    915 个主题
    动作片
    859 个主题
    恐怖片
    631 个主题
    jike早报
    471 个主题

    114

    在线

    45.5k

    用户

    52.6k

    主题

    129.0k

    帖子
    • 第一个帖子
      最后一个帖子