• H5 鼠标点击粒子扩散效果


    🧐别人的博客中有这样的效果,于是自己就尝试实现了一下。
    效果如图
    在这里插入图片描述
    源码如下

    DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="./assets/global.css">
    
        <style>
            .blastParticle {
                position: absolute;
                transform: translate(-50%, -50%);
                pointer-events: none;
            }
    
    
            body {
                overflow: hidden;
            }
    
    
    
            .hint-text {
                display: flex;
                align-items: center;
                justify-content: center;
                width: 100vw;
                height: 100vh;
                user-select: none;
            }
        style>
    head>
    
    <body>
        <div class="hint-text">
            点击鼠标左键,查看粒子效果
        div>
    
    
        <script type="module">
            import { Randoms } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/randoms.js";
            import { Animation } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/animation.js";
            import { Maths } from "https://gcore.jsdelivr.net/npm/@3r/tool/lib/maths.js";
    
    
            /**
             * 爆炸粒子效果
             */
            function blastParticle(params = {}) {
                
                let { left = 0, top = 0, width = 200, height = 200, count = 20, rr = [10, 20], duration = 1 } = params
    
                // 通过recovery拾取回收canvas 减少dom产生量
                let canvas = document.querySelector('canvas.blastParticle.recovery') || document.createElement("canvas")
                canvas.classList.remove('recovery')
    
                let ctx = canvas.getContext('2d')
                canvas.width = width;
                canvas.height = height;
    
                canvas.classList.add('blastParticle')
                canvas.style.left = left + 'px'
                canvas.style.top = top + 'px'
    
    
                let cx = width / 2
                let cy = height / 2
    
                class Particle {
                    constructor({ x, y, r, tx, ty, ts, color, duration }) {
                        this.x = x;
                        this.y = y;
                        this.r = r;
                        this.tx = tx;
                        this.ty = ty;
                        this.ts = ts;
                        this.color = color
    
                        this.timestemp = +new Date()
    
                        this.update = () => {
                            if ((+new Date() - this.timestemp) > 1000 * duration) return;
    
                            let timeProportion = (+new Date() - this.timestemp) / (1000 * duration)
                            let proportion = Animation.easeOut(timeProportion)
    
                            this.x = x + tx * proportion
                            this.y = y + ty * proportion
                            this.r = r * (1 - timeProportion)
    
                            ctx.fillStyle = color
                            ctx.beginPath()
                            ctx.arc(this.x, this.y, this.r, 0, Math.PI * 2)
                            ctx.closePath();
                            ctx.fill()
                        }
                    }
                }
    
    
    
                // 记录开始时间 用于停止渲染标记
                let startTimestemp = +new Date()
    
                function renderer(particles) {
                    ctx.clearRect(0, 0, width, height)
                    for (const p of particles) {
                        p.update()
                    }
    
                    if (+new Date() - startTimestemp < duration * 1000) {
                        console.log("渲染中");
                        requestAnimationFrame(() => { renderer(particles) })
                    }
                    else {
                        console.log('渲染结束');
                        canvas.classList.add('recovery');
                    }
                }
    
                // 粒子列表
                let particleList = []
                for (let i = 0; i < count; i++) {
                    // 随机方向
                    let tx = 2 * Math.random() - 1
                    let ty = 2 * Math.random() - 1
                    // 乘以半径
                    tx *= cx;
                    ty *= cy;
                    let particle = new Particle({ x: cx, y: cy, r: Randoms.getRandomInt(...rr), tx, ty, ts: 0, duration, color: Randoms.getRandomColor() })
                    particleList.push(particle)
                }
                // 执行渲染
                renderer(particleList)
                document.body.appendChild(canvas)
            }
    
            document.addEventListener("click", (ev) => {
                blastParticle({ left: ev.x, top: ev.y, duration: .5 })
            })
        script>
    body>
    
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    在线预览
    https://linyisonger.github.io/H5.Examples/
    源码仓库
    https://github.com/linyisonger/H5.Examples.git

  • 相关阅读:
    SettingsView/设置页 的实现
    docker 已经配置了国内镜像源,但是拉取镜像速度还是很慢(gcr.io、quay.io、ghcr.io)
    TDengine 与煤科院五大系统实现兼容性互认,助力煤矿智能化安全体系搭建
    vue3中的provide 与 inject
    C语言之指针进阶篇(3)
    Ubuntu22.04安装MySql
    「学习笔记」字符串基础:Hash,KMP与Trie
    单代号网络图
    【网络安全 --- xss-labs靶场通关(11-20关)】详细的xss-labs靶场通关思路及技巧讲解,让你对xss漏洞的理解更深刻
    【基于机械臂触觉伺服的物体操控】论文研读《A control framework for tactile servoing》
  • 原文地址:https://blog.csdn.net/linyisonger/article/details/138663549