JavaScript canvas to achieve raindrop effect

JavaScript canvas to achieve raindrop effect

This article example shares the specific code for JavaScript canvas to achieve raindrop effects for your reference. The specific content is as follows

See the effect first

It looks very cool, but it actually realizes the falling of raindrops and the final circle.

Or look at the source code

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            background-color: #000;
        }
    </style>
</head>

<body>
    <canvas></canvas>
    <script>
        // Get the canvas var canvas = document.querySelector('canvas')
        // Get the brush var ctx = canvas.getContext('2d')
        // Cannot change canvas size using CSS var ch = canvas.height = window.innerHeight
        var cw = canvas.width = window.innerWidth
        // Release raindrops var arrRain = []
        // Monitor the screen size, and change the size of the canvas accordingly window.onresize = function () {
            ch = canvas.height = window.innerHeight
            cw = canvas.width = window.innerWidth
        }
        // Get a random number in order to generate random raindrops function randow(min, max) {
            return Math.random() * (max - min) + min
        }
        // Constructor function Rain() {

        }
        // Add properties and methods to rain Rain.prototype = {
            // Initialize the position and radius of the circle where the raindrops fall init: function () {
                this.x = randow(0, cw)
                this.y = 0
                // The distance that the raindrops finally land on the ground cannot exceed the screen this.h = randow(0.8 * ch, 0.9 * ch)
                this.r = 1 // Starting radius of the circle this.vr = 1 // Speed ​​at which the radius grows this.vy = randow(4, 5)

            },
            // Drawing method draw: function () {
                // When it is less than h, draw raindrops and rectangles if (this.y < this.h) {
                    ctx.beginPath()
                    ctx.fillStyle = 'white'
                    ctx.fillRect(this.x, this.y, 4, 10)
                } else {
                    // Draw a circle ctx.beginPath()
                    ctx.strokeStyle = 'white'
                    ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI)
                    ctx.stroke()
                }
            },
            //Raindrop movement move: function () {
                // When it is less than h, add y to make the raindrops move if (this.y < this.h) {
                    this.y += this.vy
                } else {
                    // Achieve the effect of splashing water if (this.r < 70) {
                        this.r += this.vr
                    } else {
                        // Initialize after the effect ends and generate raindrops from the sky, so call the init function this.init()
                    }
                }
                this.draw()
            }
        }
        // Generate raindrops function createRain(num) {
            for (var i = 0; i < num; i++) {
                // Generate raindrops randomly, not simultaneously setTimeout(function () {
                    var rain = new Rain()
                    rain.init()
                    rain.draw()
                    arrRain.push(rain)
                }, 300 * i)
            }
        }
        createRain(60)
        setInterval(function () {
            ctx.beginPath()
            ctx.fillStyle = 'rgba(0,0,0,0.05)'
            ctx.fillRect(0, 0, cw, ch)
            for (var k of arrRain) {
                k.move()
            }
        }, 1000 / 80)
    </script>
</body>

</html>

These are the source codes implemented by Raindrops, for reference only.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JavaScript canvas to achieve raindrop effects

<<:  Mysql join query principle knowledge points

>>:  Three ways to forward linux ssh port

Recommend

How to use squid to build a proxy server for http and https

When we introduced nginx, we also used nginx to s...

MySQL transaction details

Table of contents Introduction Four characteristi...

Analysis of MySQL concurrency issues and solutions

Table of contents 1. Background 2. Slow query cau...

How to connect idea to docker to achieve one-click deployment

1. Modify the docker configuration file and open ...

Installation method of mysql-8.0.17-winx64 under windows 10

1. Download from the official website and unzip h...

Ubuntu 16.04 image complete installation tutorial under VMware

This article shares with you the installation tut...

Docker installation and deployment example on Linux

After reading the following article, you can depl...

select the best presets to create full compatibility with all browsersselect

We know that the properties of the select tag in e...

Introduction to 10 Hooks in React

Table of contents What is ReactHook? React curren...

Setting the engine MyISAM/InnoDB when creating a data table in MySQL

When I configured mysql, I set the default storag...

Practical method of deleting files from Linux command line

rm Command The rm command is a command that most ...

Solution to win10 without Hyper-V

Are you still looking for a way to enable Hyper-v...