console
(function () {
var canvas, ctx, track_mouse, mouse = {},
draw, body, H, W, particlemotion, Particle, particles = [],
randSpeed, particlesNum;
body = document.querySelector("body");
canvas = document.querySelector("#canvas");
H = window.innerHeight;
W = window.innerWidth;
canvas.height = H;
canvas.width = W;
particlesNum = 15;
ctx = canvas.getContext("2d");
track_mouse = function (e) {
e = e || window.event;
mouse.x = e.pageX || 1;
mouse.y = e.pageY || 1;
};
body.addEventListener('mousemove', track_mouse, false);
randSpeed = function (speed) {
return Math.round(Math.random()) ? Math.round(Math.random() * 9 + 1) : -Math.round(Math.random() * 9 + 1);
};
Particle = function () {
this.x = Math.round(Math.random() * W);
this.y = Math.round(Math.random() * H);
this.r = Math.round(Math.random() * 10 + 5);
this.clr = this.r * 5.5;
this.rgb = {};
this.rgb.r = Math.round(Math.random() * 255);
this.rgb.g = Math.round(Math.random() * 255);
this.rgb.b = Math.round(Math.random() * 255);
this.speed_x = randSpeed();
this.speed_y = randSpeed();
this.speed_z = Math.random() + 0.5;
this.beginDeg = 0;
this.move = function () {
if (this.x < 0) {
this.x = 0;
this.speed_x = -this.speed_x;
}
if (this.x > W) {
this.x = W;
this.speed_x = -this.speed_x;
}
if (this.y < 0) {
this.y = 0;
this.speed_y = -this.speed_y;
}
if (this.y > H) {
this.y = H;
this.speed_y = -this.speed_y;
}
this.x += this.speed_x;
this.y += this.speed_y;
this.beginDeg = 0;
};
this.moveWithMouse = function (dt, x) {
var rad = Math.PI / 180;
this.x = mouse.x + dt * Math.cos(this.beginDeg * rad);
this.y = mouse.y + dt * Math.sin(this.beginDeg * rad);
if (this.beginDeg > 360 || this.beginDeg < -360) {
this.beginDeg = 0.5;
}
if (x < 0 && this.speed_z > 0) {
this.speed_z = -this.speed_z;
}
this.beginDeg += this.speed_z;
};
};
for (var i = 0; i < particlesNum; i++) {
particles.push(new Particle());
}
draw = function (mouse_x, mouse_y, r, rgb) {
var rgbcolor = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + "," + ")";
ctx.fillStyle = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")";
ctx.beginPath();
ctx.arc(mouse_x, mouse_y, r, 0, 2 * Math.PI);
ctx.closePath();
ctx.fill();
};
particlemotion = function () {
ctx.clearRect(0, 0, W, H);
for (i = 0; i < particlesNum; i++) {
draw(particles[i].x, particles[i].y, particles[i].r, particles[i].rgb);
var x = particles[i].x - mouse.x,
dx = Math.abs(x),
y = particles[i].y - mouse.y,
dy = Math.abs(y),
dt = Math.sqrt(dx * dx + dy * dy);
if (dt < particles[i].clr) {
if (particles[i].beginDeg === 0) {
particles[i].beginDeg = Math.acos(x / particles[i].clr);
}
particles[i].moveWithMouse(dt, x);
} else {
particles[i].move();
}
}
};
setInterval(function () {
particlemotion();
}, 5);
}).call(this);
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>canvas-test</title>
<style>
body {
margin: 0;
padding: 0;
}
#canvas {
background-color: black;
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script src="app.js"></script>
</body>
</html>