Canvas implements dynamic particle connection effect (with code)
This article will introduce you through examples how to create animations with JS Canvas and achieve dynamic particle connection effects. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
JS Canvas creates animations to achieve dynamic particle connection effects
The renderings are as follows
The idea is as follows:
Draw particles in random areas, record the x-axis, y-axis coordinates of each particle and the x-axis and The distance of each movement of the y-axis
The particles are moved through the timing function. After the movement, it is judged whether it exceeds the limit. If it exceeds the limit, the particle will be deleted and a new particle will be generated
Judge the distance between all particles and connect particles at a given distance.
The code is as follows:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Canvas动态粒子连线</title> </head> <body> <canvas id="myCanvas" style="border: 1px solid #ddd; display: block;margin: 20px auto;"></canvas> <script> var myCanvas = document.getElementById("myCanvas"); myCanvas.width = "800"; myCanvas.height = "800"; var cxt = myCanvas.getContext("2d"); cxt.fillStyle="#ddd"; var points =new Array(); //绘制60个粒子 for(var i=0;i<60;i++) { drawlizi(); } setInterval(movelizi,100); //绘制静态粒子 function drawlizi(){ var x = generate_random(3,797); var y = generate_random(3,797); var speedx = generate_random(-4,4); var speedy = generate_random(-4,4); //防止出现不移动的粒子 while(speedx==0&&speedy==0) { speedx = generate_random(-4,4); speedy = generate_random(-4,4); } var point={ x_index:x, y_index:y, x_speed:speedx, y_speed:speedy }; points.push(point); cxt.beginPath(); cxt.arc(x,y,3,0,360); cxt.closePath(); cxt.fill(); } //粒子移动 function movelizi(){ cxt.clearRect(0, 0,myCanvas.width,myCanvas.height); for(var i=0;i<points.length;i++) { points[i].x_index = points[i].x_index+points[i].x_speed; points[i].y_index = points[i].y_index+points[i].y_speed; cxt.beginPath(); cxt.arc(points[i].x_index,points[i].y_index,3,0,360); cxt.closePath(); cxt.fill(); //判断超过界限删除并再生 if((points[i].x_index<3||points[i].y_index<3)||(points[i].x_index>797||points[i].y_index<3)||(points[i].x_index<3||points[i].y_index>797)||(points[i].x_index>797||points[i].y_index>797)){ points.splice(i,1); drawlizi(); } } //相近的粒子进行连线 for (var i=0;i<points.length;i++) { for (var j=0;j<points.length;j++) { if(i!=j) { var one_x = points[i].x_index; var one_y = points[i].y_index; var two_x = points[j].x_index; var two_y = points[j].y_index; // 根据两点间的距离公式,小于界限值便进行连线 var jl = Math.sqrt(Math.pow(one_x-two_x,2)+Math.pow(one_y-two_y,2)); if(jl<100) { cxt.strokeStyle="#ddd"; cxt.moveTo(one_x,one_y); cxt.lineTo(two_x,two_y); cxt.stroke(); } } } } } //生成两个数之间的随机数 function generate_random(min,max){ return Math.floor(Math.random()*(max-min)+min); } </script> </body> </html>
For more cool page special effects, please visit: js code special effects column! !
The above is the detailed content of Canvas implements dynamic particle connection effect (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
