• 烟花效果,H5+C3+JS实现


    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">
        <title>111title>
        <style>
            body {
                background-color: #000000;
                margin: 0;
                overflow: hidden;
            }
    
            html {
                height: 100%;
                overflow: hidden;
            }
    
            body {
                height: 100%;
                margin: 0;
            }
    
            #test {
                background: #000000;
            }
        style>
        <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js">script>
    head>
    
    <body>
    <canvas id="test">canvas>
    <script>
        const cvs = document.getElementById('test');
        //canvas充满窗口
        cvs.width = window.innerWidth;
        cvs.height = window.innerHeight / 2.8;
        //画笔
        const ctx = cvs.getContext('2d');
    
        //颜色数组
        const colors = ['red', 'yellow'];
    
        /*文字内容*/
        const text = 'cqf';
        const text2 = '哈哈哈';
    
        /*文字位置*/
        const [x, y] = [window.innerWidth / 2.5, window.innerHeight / 5];
        const [a, b] = [window.innerWidth / 4, window.innerHeight / 3];
    
        //字体属性
        ctx.font = 'bold 80px arial';
    
        function draw() {
            //保存上下文对象的状态
            ctx.save();
            //设置描边样式
            ctx.strokeStyle = colors[0];
            //设置描边宽度
            ctx.lineWidth = 1;
            //虚线
            ctx.setLineDash([8]);
            //以描边的方式显示路径
            ctx.strokeText(text, x, y);
            ctx.strokeText(text2, a, b);
    
            //第二部分虚线
            ctx.lineDashOffset = 8;
            ctx.strokeStyle = colors[1];
            //光晕
            ctx.shadowColor = 'orange';
            //多画几遍光晕
            for (let i = 25; i > 3; i -= 2) {
                ctx.shadowBlur = i;
                ctx.strokeText(text, x, y);
            }
            for (let i = 25; i > 3; i -= 2) {
                ctx.shadowBlur = i;
                ctx.strokeText(text2, a, b);
            }
            //将上下文对象的状态恢复到上一次保存时的状态
            ctx.restore();
        }
    
        draw();
    
        setInterval(function () {
            ctx.clearRect(0, 0, cvs.width, cvs.height);
            draw();
            colors.reverse();
        }, 500)
    
    
        var SCREEN_WIDTH = window.innerWidth,
            SCREEN_HEIGHT = window.innerHeight,
            mousePos = {
                x: 50,
                y: 300
            },
    
    // create canvas
            canvas = document.createElement('canvas'),
            context = canvas.getContext('2d'),
            particles = [],
            rockets = [],
            MAX_PARTICLES = 400,
            colorCode = 0;
    
        // init
        $(document).ready(function () {
            document.body.appendChild(canvas);
            canvas.width = SCREEN_WIDTH;
            canvas.height = SCREEN_HEIGHT;
            setInterval(launch, 600);
            setInterval(loop, 1000 / 50);
        });
    
        // update mouse position
        $(document).mousemove(function (e) {
            e.preventDefault();
            mousePos = {
                x: e.clientX,
                y: e.clientY
            };
        });
    
        // launch more rockets!!!
        $(document).mousedown(function (e) {
            for (var i = 0; i < 5; i++) {
                // 底下发射的横坐标位置
                launchFrom(SCREEN_WIDTH);
            }
        });
    
        function launch() {
            // 底下发射的横坐标位置
            launchFrom(650);
        }
    
        function launchFrom(x) {
            if (rockets.length < 100) {
                var rocket = new Rocket(x);
                rocket.explosionColor = Math.floor(Math.random() * 360 / 10) * 10;
                rocket.vel.y = Math.random() * -3 - 4;
                rocket.vel.x = Math.random() * 8 - 3;
                rocket.size = 8;
                rocket.shrink = 0.999;
                rocket.gravity = 0.0000001;
                rockets.push(rocket);
            }
        }
    
        function loop() {
            // update screen size
            if (SCREEN_WIDTH !== window.innerWidth) {
                canvas.width = SCREEN_WIDTH = window.innerWidth;
            }
            if (SCREEN_HEIGHT !== window.innerHeight) {
                canvas.height = SCREEN_HEIGHT = window.innerHeight;
            }
    
            // clear canvas
            context.fillStyle = "rgba(0, 0, 0, 0.05)";
            context.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
            var existingRockets = [];
    
            for (var i = 0; i < rockets.length; i++) {
                // update and render
                rockets[i].update();
                rockets[i].render(context);
    
                // calculate distance with Pythagoras
                var distance = Math.sqrt(Math.pow(mousePos.x - rockets[i].pos.x, 2) + Math.pow(mousePos.y - rockets[i].pos.y, 2));
    
                // random chance of 1% if rockets is above the middle
                var randomChance = rockets[i].pos.y < (SCREEN_HEIGHT * 2 / 3) ? (Math.random() * 100 <= 1) : false;
    
                /* Explosion rules
                 - 80% of screen
                 - going down
                 - close to the mouse
                 - 1% chance of random explosion
                 */
                if (rockets[i].pos.y < SCREEN_HEIGHT / 5 || rockets[i].vel.y >= 0 || distance < 50 || randomChance) {
                    rockets[i].explode();
                } else {
                    existingRockets.push(rockets[i]);
                }
            }
    
            rockets = existingRockets;
    
            var existingParticles = [];
    
            for (var i = 0; i < particles.length; i++) {
                particles[i].update();
    
                // render and save particles that can be rendered
                if (particles[i].exists()) {
                    particles[i].render(context);
                    existingParticles.push(particles[i]);
                }
            }
    
            // update array with existing particles - old particles should be garbage collected
            particles = existingParticles;
    
            while (particles.length > MAX_PARTICLES) {
                particles.shift();
            }
        }
    
        function Particle(pos) {
            this.pos = {
                x: pos ? pos.x : 0,
                y: pos ? pos.y : 0
            };
            this.vel = {
                x: 0,
                y: 0
            };
            this.shrink = .97;
            this.size = 2;
    
            this.resistance = 1;
            this.gravity = 0;
    
            this.flick = false;
    
            this.alpha = 1;
            this.fade = 0;
            this.color = 0;
        }
    
        Particle.prototype.update = function () {
            // apply resistance
            this.vel.x *= this.resistance;
            this.vel.y *= this.resistance;
    
            // gravity down
            this.vel.y += this.gravity;
    
            // update position based on speed
            this.pos.x += this.vel.x;
            this.pos.y += this.vel.y;
    
            // shrink
            this.size *= this.shrink;
    
            // fade out
            this.alpha -= this.fade;
        };
    
        Particle.prototype.render = function (c) {
            if (!this.exists()) {
                return;
            }
    
            c.save();
    
            c.globalCompositeOperation = 'lighter';
    
            var x = this.pos.x,
                y = this.pos.y,
                r = this.size / 2;
    
            var gradient = c.createRadialGradient(x, y, 0.1, x, y, r);
            gradient.addColorStop(0.1, "rgba(255,255,255," + this.alpha + ")");
            gradient.addColorStop(0.8, "hsla(" + this.color + ", 100%, 50%, " + this.alpha + ")");
            gradient.addColorStop(1, "hsla(" + this.color + ", 100%, 50%, 0.1)");
    
            c.fillStyle = gradient;
    
            c.beginPath();
            c.arc(this.pos.x, this.pos.y, this.flick ? Math.random() * this.size : this.size, 0, Math.PI * 2, true);
    
            c.closePath();
            c.fill();
    
            c.restore();
        };
    
        Particle.prototype.exists = function () {
            return this.alpha >= 0.1 && this.size >= 1;
        };
    
        function Rocket(x) {
            Particle.apply(this, [{
                x: x,
                y: SCREEN_HEIGHT
            }]);
    
            this.explosionColor = 0;
        }
    
        Rocket.prototype = new Particle();
        Rocket.prototype.constructor = Rocket;
    
        Rocket.prototype.explode = function () {
            var count = Math.random() * 10 + 80;
    
            for (var i = 0; i < count; i++) {
                var particle = new Particle(this.pos);
                var angle = Math.random() * Math.PI * 2;
    
                // emulate 3D effect by using cosine and put more particles in the middle
                var speed = Math.cos(Math.random() * Math.PI / 2) * 15;
    
                particle.vel.x = Math.cos(angle) * speed;
                particle.vel.y = Math.sin(angle) * speed;
    
                particle.size = 10;
    
                particle.gravity = 0.2;
                particle.resistance = 0.92;
                particle.shrink = Math.random() * 0.05 + 0.93;
    
                particle.flick = true;
                particle.color = this.explosionColor;
    
                particles.push(particle);
            }
        };
    
        Rocket.prototype.render = function (c) {
            if (!this.exists()) {
                return;
            }
    
            c.save();
    
            c.globalCompositeOperation = 'lighter';
    
            var x = this.pos.x,
                y = this.pos.y,
                r = this.size / 2;
    
            var gradient = c.createRadialGradient(x, y, 0.1, x, y, r);
            gradient.addColorStop(0.1, "rgba(255, 255, 255 ," + this.alpha + ")");
            gradient.addColorStop(1, "rgba(0, 0, 0, " + this.alpha + ")");
    
            c.fillStyle = gradient;
    
            c.beginPath();
            c.arc(this.pos.x, this.pos.y, this.flick ? Math.random() * this.size / 2 + this.size / 2 : this.size, 0, Math.PI * 2, true);
            c.closePath();
            c.fill();
    
            c.restore();
        };
    
    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
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
  • 相关阅读:
    mysql-MVCC
    第十节:继承【java】
    java-net-php-python-ssh电动车销售系统计算机毕业设计程序
    BF算法与KMP模式匹配算法(画图详解,C语言实现)
    OSI七层模型&TCP/IP四层&面试高频考点
    【漏洞复现-solr-命令执行】vulfocus/solr-cve_2019_17558
    图像领域-深度学习网络结构(从浅入深)——基础到对比到改进
    ArcGIS教程(02):创建多模式网络数据集
    24 DRF详细学习篇章一|Requests|Responses|View|Routers
    linux下sqlplus登录oracle显示问号处理办法
  • 原文地址:https://blog.csdn.net/qq_40306697/article/details/133699004