HTML+CSS+JS realizes canvas follows the mouse small circle special effect source code

HTML+CSS+JS realizes canvas follows the mouse small circle special effect source code

Effect (source code at the end):

insert image description here

accomplish:

1. Define tags:

 <h1>Northern Lights Night</h1>
 <canvas id="draw" style=" position: fixed; display: block;">  
			The current browser does not support Canvas, please change the browser and try again</canvas>

2. Basic text styles:

h1{
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%);
   font-size: 5em;
   font-family: 'fangsong';
   color: rgb(38, 205, 247);
  }

top: 50%;
left: 50%;
transform: translate(-50%,-50%); center alignment
3. js part, see the comments for details:

<script>
  /* First get the canvas */
  var canvas = document.querySelector("#draw");
  var yuan = canvas.getContext("2d");  
  /* Bind the window size change event to allow the canvas to fill the browser's visible area at any time*/
   window.onresize=resizeCanvas;
  function resizeCanvas(){
   canvas.width=window.innerWidth;
   canvas.height=window.innerHeight;
  }
  resizeCanvas(); 

  /* Define an array to store the small circles generated when the move event is triggered below*/
  var arr = [];
  
  /* How to draw a small circle, x and y are the initial positions, r is the radius of the circle*/
  function circle (x,y,r){
   this.x=x;
   this.y=y;
   this.r=r;
   /* Get a random color */
   this.color = `rgb(${255*Math.random()},${255*Math.random()},${255*Math.random()})`
   /* The direction of the circle's movement. The random function returns a random number between 0.0 and 1.0. x can be a random positive or negative number, and y is a random positive number*/
   this.xZou = parseInt(Math.random()*10-5);
   this.yZou = parseInt(Math.random()*10);  
   /* Add this element to the end of the arr array */ 
   arr.push(this);
  }

  /* Method to update the circle */
   circle.prototype.updated = function() {
    /* x and y increase, forming a circular shape*/
   this.x = this.x + this.xZou;
   this.y = this.y + this.yZou;
   /* The radius slowly decreases*/
   this.r = this.r - 0.1 ;
   /* Clear the circle when the radius is less than 1 */
   if(this.r<0){
    this.remove();
   }
   }
   /* Delete the function of the small circle*/
   circle.prototype.remove = function (){
    /* Traverse the array, find the same circle as the one that called this function, and then use the splice function to delete it*/
   for(let i=0;i<arr.length;i++){
     if(this==arr[i])
     {
      arr.splice(i,1);
     }
   }
  }
   /* Render a small circle */
   circle.prototype.render = function(){

   yuan.beginPath();
   yuan.arc(this.x,this.y,this.r,0,2*3.14,false);
   yuan.fillStyle = this.color;
   yuan.fill();
   }
   /* Bind mouse over events to the canvas */  
   canvas.addEventListener('mousemove',function(e){
    /* Pass in x, y, r. offsetX distance from the left side, .., */
   new circle(e.offsetX,e.offsetY,Math.random()*15); 
   })

    /* Timer renders the small circle and starts the animation, once every 30 milliseconds*/
   setInterval(function(){
     /* Clear screen */
    yuan.clearRect(0,0,canvas.width,canvas.height);
    /* Loop through the array to update and render each circle*/
    for(let i=0;i<arr.length;i++){
     /* renew*/
     arr[i].updated();
     /* Render if the browser supports it */
     if (arr[i].render()) {
      arr[i].render();
     }
     
    }

   },30)

 </script>

Canvas Links
splice() Method Chaining
random() Method Chaining
push() Method Chaining
resize event link

Complete 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;
   box-sizing: border-box;
  }
  
  body{
   background-color: rgb(72, 75, 122);
  }
  
  h1{
   position: absolute;
   top: 50%;
   left: 50%;
   transform: translate(-50%,-50%);
   font-size: 5em;
   font-family: 'fangsong';
   color: rgb(38, 205, 247);
  }
  
 </style>
</head>
<body>
 
  <h1>Northern Lights Night</h1>

 <canvas id="draw" style=" position: fixed; display: block;">  
			The current browser does not support Canvas, please change the browser and try again</canvas>

 <script>
  /* First get the canvas */
  var canvas = document.querySelector("#draw");
  var yuan = canvas.getContext("2d");  
  /* Bind the window size change event to allow the canvas to fill the browser's visible area at any time*/
   window.onresize=resizeCanvas;
  function resizeCanvas(){
   canvas.width=window.innerWidth;
   canvas.height=window.innerHeight;
  }
  resizeCanvas(); 

  /* Define an array to store the small circles generated when the move event is triggered below*/
  var arr = [];
  
  /* How to draw a small circle, x and y are the initial positions, r is the radius of the circle*/
  function circle (x,y,r){
   this.x=x;
   this.y=y;
   this.r=r;
   /* Get a random color */
   this.color = `rgb(${255*Math.random()},${255*Math.random()},${255*Math.random()})`
   /* The direction of the circle's movement. The random function returns a random number between 0.0 and 1.0. x can be a random positive or negative number, and y is a random positive number*/
   this.xZou = parseInt(Math.random()*10-5);
   this.yZou = parseInt(Math.random()*10);  
   /* Add this element to the end of the arr array */ 
   arr.push(this);
  }

  /* Method to update the circle */
   circle.prototype.updated = function() {
    /* x and y increase, forming a circular shape*/
   this.x = this.x + this.xZou;
   this.y = this.y + this.yZou;
   /* The radius slowly decreases*/
   this.r = this.r - 0.1 ;
   /* Clear the circle when the radius is less than 1 */
   if(this.r<0){
    this.remove();
   }
   }
   /* Delete the function of the small circle*/
   circle.prototype.remove = function (){
    /* Traverse the array, find the same circle as the one that called this function, and then use the splice function to delete it*/
   for(let i=0;i<arr.length;i++){
     if(this==arr[i])
     {
      arr.splice(i,1);
     }
   }
  }
   /* Render a small circle */
   circle.prototype.render = function(){

   yuan.beginPath();
   yuan.arc(this.x,this.y,this.r,0,2*3.14,false);
   yuan.fillStyle = this.color;
   yuan.fill();
   }
   /* Bind mouse over events to the canvas */  
   canvas.addEventListener('mousemove',function(e){
    /* Pass in x, y, r. offsetX distance from the left side, .., */
   new circle(e.offsetX,e.offsetY,Math.random()*15); 
   })

    /* Timer renders the small circle and starts the animation, once every 30 milliseconds*/
   setInterval(function(){
     /* Clear screen */
    yuan.clearRect(0,0,canvas.width,canvas.height);
    /* Loop through the array to update and render each circle*/
    for(let i=0;i<arr.length;i++){
     /* renew*/
     arr[i].updated();
     /* Render if the browser supports it */
     if (arr[i].render()) {
      arr[i].render();
     }
     
    }

   },30)

 </script>
</body>
</html>

other:

Three self-examinations today: A comfortable life is boring; life full of challenges and achieving victories is the true meaning of life.

This concludes this article about the source code for the small circle special effects of canvas following the mouse using html+css+js. For more content about the small circle special effects of canvas following the mouse, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Remove HTML tags and delete HTML sample code
  • Using front-end HTML+CSS+JS to develop a simple TODOLIST function (notepad)
  • HTML5 and jQuery to implement preview code examples before uploading local pictures
  • js+html+css to achieve manual and automatic carousel
  • Two ways to use JavaScript in HTML
  • How to learn various tags of html

<<:  How to bind Docker container to external IP and port

>>:  Solution to the Mysql ERROR 1045 (28000): Access denied for user root@localhost problem in Ubuntu system

Recommend

element-ui Mark the coordinate points after uploading the picture

What is element-ui element-ui is a desktop compon...

Example code for implementing auto-increment sequence in mysql

1. Create a sequence table CREATE TABLE `sequence...

Detailed explanation of where the images pulled by docker are stored

The commands pulled by docker are stored in the /...

...

Tutorial on installing Tomcat server under Windows

1 Download and prepare First, we need to download...

How to use xshell to connect to Linux in VMware (2 methods)

【Foreword】 Recently I want to stress test ITOO...

Summary of all HTML interview questions

1. The role of doctype, the difference between st...

JavaScript removes unnecessary properties of an object

Table of contents Example Method 1: delete Method...

Sending emails in html is easy with Mailto

Recently, I added a click-to-send email function t...

How to make a List in CocosCreator

CocosCreator version: 2.3.4 Cocos does not have a...

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

Docker builds jenkins+maven code building and deployment platform

Table of contents Docker Basic Concepts Docker in...

In-depth understanding of the life cycle comparison between Vue2 and Vue3

Table of contents Cycle comparison usage Summariz...