js+canvas realizes code rain effect

js+canvas realizes code rain effect

This article shares the specific code of js+canvas code rain effect for your reference. The specific content is as follows

Code:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <style type="text/css">
   *{
    margin: 0px;
    padding: 0px;
   }
   html,body{
    height: 100%;
    width: 100%;
   }
   #canvas{
    display: block;
   }
  </style>
 </head>
 <body>
  <canvas id="canvas"></canvas>
  <script type="text/javascript">
   var canvas = document.getElementById('canvas')
   var c = canvas.getContext('2d')
   var cw = canvas.width = window.innerWidth
   var ch = canvas.height = window.innerHeight 
   
   var str = [1,2,3,4,5,6,7,8,9,0,'q','w','e','r','t','y','u','i','a','c','d','f','g','h','j','l']
   var init = function(){
    this.x = Math.random()*cw
    this.y = 0
    this.content1 =Math.random()*15 
    this.speed = Math.random()*5+20
    this.add = function(){
     this.y+=this.speed
    }
    this.reset1 = function(){
     this.x = Math.random()*cw
     this.y = 0
    }
   }
   //Define a random color var gl = c.createLinearGradient(0, 0, cw, ch);
       gl.addColorStop(0, 'red');
       
       gl.addColorStop(.5, 'yellow');
       
       gl.addColorStop(1, 'cyan');
   
   var arr = []
   for(var i=0;i<20;i++){
    arr.push(new init())
   }
   setInterval(function(){
    
    c.fillStyle = 'rgba(0,0,0,0.05)'
    c.fillRect(0,0,cw,ch)
    //The above two sentences are to give a background, put in the timer so that each time it runs, it will be repainted once//used to clear the canvas//1, yong rgba() to indicate the color is to give a transparency, the newly drawn canvas does not completely cover the previous canvas, so there is a gradient effectfor(var i=0;i<arr.length;i++){
     c.fillStyle = gl
     c.font = '30px Microsoft YaHei'
     
     c.fillText(str[i],arr[i].x,arr[i].y)
     
     //Let him fall to the bottom and come back if (arr[i].y>ch-20) {
      arr[i].reset1()
     }
     arr[i].add()
    }
    
    
    //
   },1000/60)
  </script>
 </body>
</html>

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 to achieve code rain effect
  • JavaScript canvas to achieve code rain effect
  • JS+CSS+HTML realizes "code rain" similar to the falling effect of text in The Matrix
  • HTML+JS realizes the source code of "code rain" effect (the Matrix text falling effect)
  • JS realizes code rain special effects

<<:  MySQL Binlog Data Recovery: Detailed Explanation of Accidentally Deleting a Database

>>:  Installation steps of docker-ce on Raspberry Pi 4b ubuntu19 server

Recommend

Analysis of the process of deploying pure HTML files in Tomcat and WebLogic

1. First, the pure HTML file must have an entry i...

Detailed explanation of mysql.user user table in Mysql

MySQL is a multi-user managed database that can a...

Various methods to restart Mysql under CentOS (recommended)

1. MySQL installed via rpm package service mysqld...

MySQL cleverly uses sum, case and when to optimize statistical queries

I was recently working on a project at the compan...

Mac+IDEA+Tomcat configuration steps

Table of contents 1. Download 2. Installation and...

How to correctly create MySQL indexes

Indexing is similar to building bibliographic ind...

js returns to the previous page and refreshes the code

1. Javascript returns to the previous page history...

A simple way to put HTML footer at the bottom of the page

Requirement: Sometimes, when the page content is ...

Detailed Tutorial on Using xargs Command on Linux

Hello everyone, I am Liang Xu. When using Linux, ...

How to introduce Excel table plug-in into Vue

This article shares the specific code of Vue intr...

Summary of common Mysql DDL operations

Library Management Create a library create databa...

How to add and delete unique indexes for fields in MySQL

1. Add PRIMARY KEY (primary key index) mysql>A...