有密集恐惧的小伙伴SORRY了
小三,上代码(声明:想要和鼠标互动,留言)
<!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>
body,
p,
div,
h1,
h2,
h3,
h4,
h5,
h6,
ul,
ol,
li {
margin: 0;
padding: 0;
}
#canvas {
display: block;
margin: 0 auto;
background-color: #000;
}
#header{
position: relative;
}
#header-title{
position:absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
#header-title h1{
margin-top:90px;
color:#fff;
font-size:50px;
}
</style>
</head>
<body>
<div id="header">
<div id="header-title">
</div>
<canvas id="canvas"></canvas>
</div>
<script>
let canvasEle = document.getElementById('canvas');
let ctx = canvasEle.getContext('2d');
canvasEle.width = 1366;
canvasEle.height = 800;
function Circle(x, y, radius) {
this.x = x;
this.y = y;
this.radius = radius;
this.xDistance = Math.random() * 2 > 1 ? Math.random() * 2 : - Math.random() * 2 ;
this.yDistance = Math.random() * 2 > 1 ? Math.random() * 2 : - Math.random() * 2;
}
function draw(circle) {
if(circle.x > canvasEle.width || circle.x < 0){
circle.xDistance = - circle.xDistance;
}
if(circle.y > canvasEle.height || circle.y < 0){
circle.yDistance = - circle.yDistance;
}
circle.x += circle.xDistance;
circle.y += circle.yDistance;
ctx.fillStyle = 'rgba(255,255,255,.3)';
ctx.beginPath();
ctx.arc(circle.x, circle.y, circle.radius, 0, 2 * Math.PI);
ctx.fill();
}
let array = [];
let number = 150;
function init() {
for (let n = 0; n < number; n++) {
let x = Math.floor(Math.random() * canvasEle.width);
let y = Math.floor(Math.random() * canvasEle.height);
let radius = Math.ceil(Math.random() * 3) + 1;
array.push(new Circle(x,y,radius));
}
}
function connect(){
ctx.strokeStyle = 'rgba(255,255,255,.3)';
for(var n=0;n<array.length;n++){
for(var i=n+1;i<array.length;i++){
let xDistance=(array[i].x-array[n].x)*(array[i].x-array[n].x);
let yDistance=(array[i].y-array[n].y)*(array[i].y-array[n].y);
let distance=Math.sqrt(xDistance+yDistance);
if(distance<100){
ctx.beginPath();
ctx.moveTo(array[n].x,array[n].y);
ctx.lineTo(array[i].x,array[i].y);
ctx.stroke();
}
}
}
}
function animation(){
ctx.clearRect(0,0,canvasEle.width,canvasEle.height);
for(let n = 0;n<array.length;n++){
draw(array[n]);
}
connect();
window.requestAnimationFrame(animation);
}
init();
animation();
</script>
</body>
</html>