console
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var cWidth,cHeight;
resize();
window.onresize = resize;
function resize() {
cWidth = canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
cHeight = canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
var mouse = {x: null, y: null, max: 20000};
window.onmousemove = function(e) {
e = e || window.event;
mouse.x = e.clientX;
mouse.y = e.clientY;
};
window.onmouseout = function(e) {
mouse.x = null;
mouse.y = null;
};
var dots = [];
for (var i = 0; i < 300; i++) {
var x = Math.random() * cWidth;
var y = Math.random() * cHeight;
var moveX = Math.random() * 2 - 1;
var moveY = Math.random() * 2 - 1;
dots.push({
x: x,
y: y,
moveX: moveX,
moveY: moveY,
max: 6000
})
}
setTimeout(function() {
animate();
}, 100);
function animate() {
ctx.clearRect(0, 0, cWidth, cHeight);
var allDots = [mouse].concat(dots);
dots.forEach(function(dot) {
dot.x += dot.moveX;
dot.y += dot.moveY;
dot.moveX *= (dot.x > cWidth || dot.x < 0) ? -1 : 1;
dot.moveY *= (dot.y > cHeight || dot.y < 0) ? -1 : 1;
ctx.fillRect(dot.x - 0.5, dot.y - 0.5, 1, 1);
for (var i = 0; i < allDots.length; i++) {
var tempDot = allDots[i];
if (dot === tempDot || tempDot.x === null || tempDot.y === null) continue;
var _x = dot.x - tempDot.x;
var _y = dot.y - tempDot.y;
var dis = _x * _x + _y * _y;
var ratio;
if (dis < tempDot.max) {
if (tempDot === mouse && dis > (tempDot.max / 2)) {
dot.x -= _x * 0.03;
dot.y -= _y * 0.03;
}
ratio = (tempDot.max - dis) / tempDot.max;
ctx.beginPath();
ctx.lineWidth = ratio / 2;
ctx.strokeStyle = 'rgba(0,0,0,' + (ratio + 0.2) + ')';
ctx.moveTo(dot.x, dot.y);
ctx.lineTo(tempDot.x, tempDot.y);
ctx.stroke();
}
}
allDots.splice(allDots.indexOf(dot), 1);
});
setTimeout(function() {
animate();
}, 1000/60);
}
<canvas id="canvas"></canvas>