<canvas id="canvas"></canvas> <script> var WIDTH = 400, HEIGHT = 400, POINT = 40, canvas = document.getElementById('canvas'), ctx = canvas.getContext('2d'); var circleArr = []; canvas.width = WIDTH; canvas.height = HEIGHT; ctx.strokeStyle = 'rgba(0,0,0,1)'; ctx.strokeWidth = 1; ctx.fillStyle = 'rgba(0,0,180,0.2)'; function Line (x, y, _x, _y, o) { this.x = x; this.y = y; this._x = _x; this._y = _y; this.o = o; } function Circle(x, y, r, mx, my) { this.x = x; this.y = y; this.r = r; this.mx = mx; this.my = my; } var Num = (max, min) => { min |= 0; return Math.floor(Math.random()*(max - min + 1) + min); } var drawLine = (x, y, _x, _y, o) => { var line = new Line(x, y, _x, _y, o); ctx.beginPath(); ctx.strokeStyle = `rgba(0, 0, 0, ${line.o})`; ctx.moveTo(line.x, line.y); ctx.lineTo(line._x, line._y); ctx.closePath(); ctx.stroke(); } var drawCircle = (x, y, r, mx, my) => { var circle = new Circle(x, y, r, mx, my); ctx.beginPath(); ctx.arc(circle.x, circle.y, circle.r, 0, Math.PI*2); ctx.closePath(); ctx.fill(); return circle; } var init = () => { circleArr = []; for (var i = 0; i < POINT; i++) { circleArr.push(drawCircle(Num(WIDTH), Num(HEIGHT), Num(10, 1), Num(10, -10)/30, Num(10, -10)/40)); } draw() } var draw = () => { ctx.clearRect(0, 0, WIDTH, HEIGHT); for (var i = 0; i < POINT; i++) { drawCircle(circleArr[i].x, circleArr[i].y, circleArr[i].r); } for (var i = 0; i < POINT; i++) { for (var j = 0; j < POINT; j++) { if (i + j < POINT) { var A = Math.abs(circleArr[i+j].x - circleArr[i].x), B = Math.abs(circleArr[i+j].y - circleArr[i].y); var lineLength = Math.sqrt(A*A + B*B); var C = 1/lineLength*7-0.009; var lineOpacity = C > 0.03 ? 0.2 : C; if (lineOpacity > 0) { drawLine(circleArr[i].x, circleArr[i].y, circleArr[i+j].x, circleArr[i+j].y, lineOpacity); } } } } } init(); setInterval(function () { for (var i = 0; i < POINT; i++) { var cir = circleArr[i]; cir.x += cir.mx; cir.y += cir.my; if (cir.x > WIDTH) cir.mx = -cir.mx; else if (cir.x < 0) cir.mx = -cir.mx; if (cir.y > HEIGHT) cir.my = -cir.my; else if (cir.y < 0) cir.my = -cir.my; } draw(); }, 100); </script>