-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathparticles.js
125 lines (111 loc) · 2.96 KB
/
particles.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const canvas = document.querySelector("#canvas");
const ctx = canvas.getContext("2d");
let w, h, particles;
let particleDistance = 40;
let mouse = {
x: undefined,
y: undefined,
radius: 70
}
function init() {
resizeReset();
animationLoop();
}
function resizeReset() {
w = canvas.width = window.innerWidth;
h = canvas.height = window.innerHeight;
particles = [];
for (let y = (((h - particleDistance) % particleDistance) + particleDistance) / 2; y < h; y += particleDistance) {
for (let x = (((w - particleDistance) % particleDistance) + particleDistance) / 2; x < w; x += particleDistance) {
particles.push(new Particle(x, y));
}
}
}
function animationLoop() {
ctx.clearRect(0, 0, w, h);
drawScene();
requestAnimationFrame(animationLoop);
}
function drawScene() {
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].draw();
}
drawLine();
}
function drawLine() {
for (let a = 0; a < particles.length; a++) {
for (let b = a; b < particles.length; b++) {
let dx = particles[a].x - particles[b].x;
let dy = particles[a].y - particles[b].y;
let distance = Math.sqrt(dx * dx + dy * dy);
if (distance < particleDistance * 1.5) {
opacity = 1 - (distance / (particleDistance * 1.5));
ctx.strokeStyle = "rgba(100,100,180," + opacity + ")";
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(particles[a].x, particles[a].y);
ctx.lineTo(particles[b].x, particles[b].y);
ctx.stroke();
}
}
}
}
function mousemove(e) {
mouse.x = e.x;
mouse.y = e.y;
}
function mouseout() {
mouse.x = undefined;
mouse.y = undefined;
}
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.size = 100;
this.baseX = this.x;
this.baseY = this.y;
this.speed = (Math.random() * 25) + 99;
}
draw() {
ctx.fillStyle = "rgba(200,200,255,0.0001)";
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
update() {
let dx = mouse.x - this.x;
let dy = mouse.y - this.y;
let distance = Math.sqrt(dx * dx + dy * dy);
let maxDistance = mouse.radius;
let force = (maxDistance - distance) / maxDistance; // 0 ~ 1
let forceDirectionX = dx / distance;
let forceDirectionY = dy / distance;
let directionX = forceDirectionX * force * this.speed;
let directionY = forceDirectionY * force * this.speed;
if (distance < mouse.radius) {
this.x -= directionX;
this.y -= directionY;
} else {
if (this.x !== this.baseX) {
let dx = this.x - this.baseX;
this.x -= dx / 10;
}
if (this.y !== this.baseY) {
let dy = this.y - this.baseY;
this.y -= dy / 10;
}
}
}
}
init();
window.addEventListener("resize", resizeReset);
window.addEventListener("mousemove", mousemove);
window.addEventListener("mouseout", mouseout);
window.addEventListener('scroll', function() {
var canvas = document.querySelector('#canvas');
var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
canvas.style.top = scrollTop + 'px';
});