API参考

https://www.w3school.com.cn/tags/html_ref_canvas.asp

粒子特效

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>1</title>
    <style>
        html,
        body {
            margin: 0;
            overflow: hidden;
            width: 100%;
            height: 100%;
            cursor: none;
            background: black;
        }
    </style>
</head>

<body>
    <canvas id="canvas" width="400" height="400">
    </canvas>
    <script>
        var ctx = document.getElementById('canvas'),
            content = ctx.getContext('2d'),
            WIDTH = document.documentElement.clientWidth,
            HEIGHT = document.documentElement.clientHeight
        ctx.width = WIDTH
        ctx.height = HEIGHT
        var round = []
        var initRoundPopulation = 80;

        function Round_item(index, x, y) {
            this.index = index
            this.x = x
            this.y = y
            this.r = Math.random() * 2 + 1
            var alpha = (Math.floor(Math.random() * 10) + 1) / 10 / 2;
            this.color = "rgba(255,255,255," + alpha + ")";
        }
        Round_item.prototype.draw = function () {
            content.fillStyle = this.color
            content.shadowBlur = this.r * 2
            content.beginPath()
            content.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false)
            content.closePath()
            content.fill()
        }

        function init() {
            for (var i = 0; i < initRoundPopulation; i++) {
                round[i] = new Round_item(i, Math.random() * WIDTH, Math.random() *
                    HEIGHT);
                round[i].draw();
            }
        }
        init()
    </script>
</body>

</html>

# 让粒子动起来
    Round_item.prototype.move = function () {
        this.y -= 0.15
        if (this.y <= -10) {
            this.y = HEIGHT + 10
        }
        this.draw()
    }

    function init() {
        for (var i = 0; i < initRoundPopulation; i++) {
            round[i] = new Round_item(i, Math.random() * WIDTH, Math.random() *
                HEIGHT);
            round[i].draw();
        }
        animate()
    }

    function animate() {
        content.clearRect(0, 0, WIDTH, HEIGHT)
        for (var i in round) {
            round[i].move()
        }
        requestAnimationFrame(animate)
    }

```
222