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

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

How to improve MySQL Limit query performance

In MySQL database operations, we always hope to a...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

Analysis of the locking mechanism of MySQL database

In the case of concurrent access, non-repeatable ...

Tutorial on binary compilation and installation of MySql centos7 under Linux

// It took me a whole afternoon to install this, ...

How can MySQL effectively prevent database deletion and running away?

Table of contents Safe Mode Settings test 1. Upda...

Analysis of Docker's method for creating local images

The so-called container actually creates a readab...

Test and solution for MySQL's large memory usage and high CPU usage

After the changes: innodb_buffer_pool_size=576M -...

Solution to VMware virtual machine no network

Table of contents 1. Problem Description 2. Probl...

A brief discussion on the correct posture of Tomcat memory configuration

1. Background Although I have read many blogs or ...

JS realizes the effect of Baidu News navigation bar

This article shares the specific code of JS to ac...

Analysis of the ideas of implementing vertical tables in two ways in Vue project

Problem Description In our projects, horizontal t...

Detailed explanation of the use of Vue image drag and drop zoom component

The specific usage of the Vue image drag and drop...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

CocosCreator ScrollView optimization series: frame loading

Table of contents 1. Introduction 2. Analysis of ...