-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
125 lines (91 loc) · 2.52 KB
/
script.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
var NUM_PARTICLES = ((ROWS = 100) * (COLS = 300)),
THICKNESS = Math.pow(80, 2),
SPACING = 3,
MARGIN = 100,
COLOR = 220,
DRAG = 0.95,
EASE = 0.25,
container,
particle,
canvas,
mouse,
stats,
list,
ctx,
tog,
man,
dx, dy,
mx, my,
d, t, f,
a, b,
i, n,
w, h,
p, s,
r, c;
particle = {
vx: 0,
vy: 0,
x: 0,
y: 0
};
function init() {
container = document.getElementById('container');
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
man = false;
tog = true;
list = [];
w = canvas.width = COLS * SPACING + MARGIN * 2;
h = canvas.height = ROWS * SPACING + MARGIN * 2;
container.style.marginLeft = Math.round(w * -0.5) + 'px';
container.style.marginTop = Math.round(h * -0.5) + 'px';
for (i = 0; i < NUM_PARTICLES; i++) {
p = Object.create(particle);
p.x = p.ox = MARGIN + SPACING * (i % COLS);
p.y = p.oy = MARGIN + SPACING * Math.floor(i / COLS);
list[i] = p;
}
container.addEventListener('mousemove', function(e) {
bounds = container.getBoundingClientRect();
mx = e.clientX - bounds.left;
my = e.clientY - bounds.top;
man = true;
});
if (typeof Stats === 'function') {
document.body.appendChild((stats = new Stats()).domElement);
}
container.appendChild(canvas);
}
function step() {
if (stats) stats.begin();
if (tog = !tog) {
if (!man) {
t = +new Date() * 0.001;
mx = w * 0.5 + (Math.cos(t * 2.1) * Math.cos(t * 0.9) * w * 0.45);
my = h * 0.5 + (Math.sin(t * 3.2) * Math.tan(Math.sin(t * 0.8)) * h * 0.45);
}
for (i = 0; i < NUM_PARTICLES; i++) {
p = list[i];
d = (dx = mx - p.x) * dx + (dy = my - p.y) * dy;
f = -THICKNESS / d;
if (d < THICKNESS) {
t = Math.atan2(dy, dx);
p.vx += f * Math.cos(t);
p.vy += f * Math.sin(t);
}
p.x += (p.vx *= DRAG) + (p.ox - p.x) * EASE;
p.y += (p.vy *= DRAG) + (p.oy - p.y) * EASE;
}
} else {
b = (a = ctx.createImageData(w, h)).data;
for (i = 0; i < NUM_PARTICLES; i++) {
p = list[i];
b[n = (~~p.x + (~~p.y * w)) * 4] = b[n + 1] = b[n + 2] = COLOR, b[n + 3] = 255;
}
ctx.putImageData(a, 0, 0);
}
if (stats) stats.end();
requestAnimationFrame(step);
}
init();
step();