• html--彩虹马


    html

    DOCTYPE html>
    <html lang="en" >
    <head>
    <meta charset="UTF-8">
    <title>Rainbow Space Unicorntitle>
    <link rel="stylesheet" href="css/style.css">
    head>
    <body>
    <img src="img/unicorn.png" alt="unicorn" id="unicorn" hidden>
    <script  src="js/index.js">script>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    js

    var __extends = (this && this.__extends) || (function () {
        var extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return function (d, b) {
            extendStatics(d, b);
            function __() { this.constructor = d; }
            d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
        };
    })();
    window.addEventListener('DOMContentLoaded', init);
    //#region classes
    var Vector = /** @class */ (function () {
        function Vector() {
            this.x = 0;
            this.y = 0;
            this.vx = 0;
            this.vy = 0;
            this.angle = 0;
            this.va = 0.05;
        }
        return Vector;
    }());
    var Particle = /** @class */ (function (_super) {
        __extends(Particle, _super);
        function Particle() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.id = Math.random().toString();
            _this.color = randomColor();
            _this.radius = 8;
            _this.opacity = 1;
            return _this;
        }
        return Particle;
    }(Vector));
    var Unicorn = /** @class */ (function (_super) {
        __extends(Unicorn, _super);
        function Unicorn(image) {
            var _this = _super.call(this) || this;
            _this.image = image;
            return _this;
        }
        return Unicorn;
    }(Vector));
    var Tile = /** @class */ (function (_super) {
        __extends(Tile, _super);
        function Tile() {
            var _this = _super !== null && _super.apply(this, arguments) || this;
            _this.color = randomColor();
            _this.width = 30;
            _this.height = 10;
            _this.opacity = 1;
            return _this;
        }
        return Tile;
    }(Vector));
    var ParticleFactory = /** @class */ (function () {
        function ParticleFactory() {
        }
        ParticleFactory.archive = function (p) {
            ParticleFactory.particleArchive[p.id] = Object.assign({}, p);
        };
        ParticleFactory.retrieve = function (p) {
            if (p.id in ParticleFactory.particleArchive) {
                return ParticleFactory.particleArchive[p.id];
            }
            return null;
        };
        ParticleFactory.create = function (options) {
            var p = new Particle();
            Object.assign(p, options);
            ParticleFactory.archive(p);
            return p;
        };
        ParticleFactory.reset = function (p) {
            var archivedVersion = ParticleFactory.retrieve(p);
            if (archivedVersion) {
                ;
                Object.assign(p, archivedVersion);
            }
        };
        ParticleFactory.particleArchive = {};
        return ParticleFactory;
    }());
    var Renderer = /** @class */ (function () {
        function Renderer($) {
            var _this = this;
            this.$ = $;
            this.renderParticle = function (p) {
                var $ = _this.$;
                var x = p.x, y = p.y, radius = p.radius, color = p.color, opacity = p.opacity;
                $.save();
                $.globalAlpha = opacity;
                $.fillStyle = color;
                $.translate(x, y);
                $.beginPath();
                $.arc(0, 0, radius, 0, PI2);
                $.fill();
                $.stroke();
                $.restore();
            };
            this.renderStar = function (p) {
                var $ = _this.$;
                var radius = p.radius, color = p.color, x = p.x, y = p.y, opacity = p.opacity;
                $.save();
                $.translate(x, y);
                $.fillStyle = color;
                $.globalAlpha = opacity;
                $.beginPath();
                for (var i = 5; i--;) {
                    $.lineTo(0, radius);
                    $.translate(0, radius);
                    $.rotate(PI2 / 10);
                    $.lineTo(0, -radius);
                    $.translate(0, -radius);
                    $.rotate(-((Math.PI * 6) / 10));
                }
                $.lineTo(0, radius);
                $.fill();
                $.stroke();
                $.restore();
            };
            this.renderTile = function (t) {
                var $ = _this.$;
                var x = t.x, y = t.y, width = t.width, height = t.height, color = t.color, opacity = t.opacity;
                $.save();
                $.globalAlpha = opacity;
                $.fillStyle = color;
                $.translate(x, y);
                $.beginPath();
                $.rect(0, 0, width, height);
                $.fill();
                $.stroke();
                $.restore();
            };
        }
        Renderer.prototype.clearScreen = function () {
            var $ = this.$;
            $.clearRect(0, 0, $.canvas.width, $.canvas.height);
        };
        Renderer.prototype.renderImage = function (img, x, y) {
            var $ = this.$;
            $.save();
            $.drawImage(img, x, y);
            $.restore();
        };
        Renderer.prototype.renderUnicorn = function (u) {
            this.renderImage(u.image, u.x, u.y);
        };
        return Renderer;
    }());
    //#endregion
    //#region globals
    var CANVAS = document.createElement('canvas');
    var CTX = CANVAS.getContext('2d');
    var WIDTH = (CANVAS.width = window.innerWidth);
    var HEIGHT = (CANVAS.height = window.innerHeight);
    var PI2 = 2 * Math.PI;
    var GRAVITY = 0.125;
    var COLOR_FADE = 0.01;
    var COLORS = [
        '#9400D3',
        '#4B0082',
        '#0000FF',
        '#00FF00',
        '#FFFF00',
        '#FF7F00',
        '#FF0000'
    ];
    var RENDERER = new Renderer(CTX);
    var TILES = [];
    var PARTICLES = [];
    var PARTICLE_COUNT = 40;
    var UNICORN_IMAGE = document.getElementById('unicorn');
    var UNICORN = new Unicorn(UNICORN_IMAGE);
    //#endregion
    //#region utils
    function randomColor() {
        return sample(COLORS);
    }
    function randomNumber(min, max) {
        return Math.random() * (max - min) + min;
    }
    function randomBoolean() {
        return Math.random() > 0.5;
    }
    function sample(a) {
        return a[Math.floor(Math.random() * a.length)];
    }
    function outsideScreen(p) {
        var diameter = p.radius * 2;
        var yExceeded = p.y - diameter > HEIGHT || p.y + diameter < 0;
        var xExceeded = p.x - diameter > WIDTH || p.x + diameter < 0;
        return yExceeded || xExceeded;
    }
    //#endregion
    //#region animation
    function updateParticle(p) {
        p.vy += GRAVITY;
        p.y += p.vy;
        p.x += p.vx;
        if (p.opacity > COLOR_FADE) {
            p.opacity -= COLOR_FADE;
        }
        if (outsideScreen(p)) {
            ParticleFactory.reset(p);
        }
    }
    function updateUnicorn(unicorn) {
        var image = unicorn.image;
        var centerX = WIDTH / 2 - image.width / 2;
        var centerY = HEIGHT / 2 - image.height / 2 - 50;
        var radiusX = 20;
        var radiusY = 8;
        unicorn.x = centerX + Math.cos(unicorn.angle) * radiusX;
        unicorn.y = centerY + Math.sin(unicorn.angle) * radiusY;
        unicorn.angle += unicorn.va;
    }
    function animation() {
        requestAnimationFrame(animation);
        RENDERER.clearScreen();
        TILES.forEach(updateTile);
        TILES.forEach(RENDERER.renderTile);
        PARTICLES.forEach(updateParticle);
        PARTICLES.forEach(RENDERER.renderStar);
        if (UNICORN_IMAGE.complete) {
            updateUnicorn(UNICORN);
            RENDERER.renderUnicorn(UNICORN);
        }
    }
    function createParticles() {
        for (var i = PARTICLE_COUNT; i > 0; --i) {
            var p = ParticleFactory.create({
                x: WIDTH / 2,
                y: HEIGHT / 2,
                vx: randomNumber(-14, -3),
                vy: randomNumber(-8, 2)
            });
            var i_1 = Math.floor(randomNumber(0, 60)) + 1;
            while (i_1--) {
                updateParticle(p);
            }
            PARTICLES.push(p);
        }
    }
    function updateTile(t) {
        t.vx -= GRAVITY;
        t.x += t.vx;
        t.y += t.vy;
        if (t.x + t.width < 0) {
            t.x = randomNumber(0, WIDTH) + WIDTH;
            t.vx = 0;
        }
    }
    function createTiles() {
        var tileCount = HEIGHT / 10;
        for (var i = tileCount; i > 0; --i) {
            var tileWidth = randomNumber(60, 120);
            var t = new Tile();
            t.opacity = randomNumber(0.1, 0.6);
            t.width = tileWidth;
            t.x = tileWidth * (i - 1);
            t.y = t.height * (i - 1);
            var ii = Math.floor(randomNumber(0, 260)) + 1;
            while (ii--) {
                updateTile(t);
            }
            TILES.push(t);
        }
    }
    function setup() {
        document.body.appendChild(CANVAS);
        var unicornX = WIDTH / 2 - UNICORN.image.width / 2;
        var unicornY = HEIGHT / 2 - UNICORN.image.height / 2;
        UNICORN.x = unicornX;
        UNICORN.y = unicornY;
        createTiles();
        createParticles();
    }
    function init() {
        setup();
        animation();
    }
    //#endregion
    
    • 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

    css

    body {
        background: linear-gradient(180deg, #FF7F0022, white, #9400D322);
        box-sizing: border-box;
        margin: 0;
        overflow: hidden;
        text-align: center;
    }
    
    .made-by {
        position: absolute;
        bottom: 16px;
        right: 16px;
        padding: 8px 16px;
        background: white;
        color: hotpink;
        font-family: monospace;
        font-size: 12px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    效果

    在这里插入图片描述

  • 相关阅读:
    Python自动化测试框架之unittest使用详解!
    Vue基础(七)——数据交互(Ajax)
    应急响应.windows
    Redis无法正常连接
    【学懂数据结构】算法及其复杂度分析
    window10下安装docker教程
    Vue项目大屏可视化适配方法记录
    【C#】BindingFlags
    程序员转正述职报告/总结
    04-学生选课(课程记录、订单创建、微信支付)
  • 原文地址:https://blog.csdn.net/stqer/article/details/136597021