-
Notifications
You must be signed in to change notification settings - Fork 0
/
letter-particles.js
219 lines (193 loc) · 5.28 KB
/
letter-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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
class Vector {
constructor(x = 0, y = 0) {
this.x = x;
this.y = y;
}
// return the angle of the vector in radians
getDirection() {
return Math.atan2(this.y, this.x);
}
// set the direction of the vector in radians
setDirection(angle) {
const magnitude = this.getMagnitude();
this.x = Math.cos(angle) * magnitude;
this.y = Math.sin(angle) * magnitude;
}
// get the magnitude of the vector
getMagnitude() {
// use pythagoras theorem to work out the magnitude of the vector
return Math.sqrt(this.x * this.x + this.y * this.y);
}
// set the magnitude of the vector
setMagnitude(magnitude) {
const direction = this.getDirection();
this.x = Math.cos(direction) * magnitude;
this.y = Math.sin(direction) * magnitude;
}
// add two vectors together and return a new one
add(v2) {
return new Vector(this.x + v2.x, this.y + v2.y);
}
// add a vector to this one
addTo(v2, scalar = 1) {
this.x += v2.x * scalar;
this.y += v2.y * scalar;
}
// subtract two vectors and return a new one
subtract(v2) {
return new Vector(this.x - v2.x, this.y - v2.y);
}
// subtract a vector from this one
subtractFrom(v2) {
this.x -= v2.x;
this.y -= v2.y;
}
// multiply this vector by a scalar and return a new one
multiply(scalar) {
return new Vector(this.x * scalar, this.y * scalar);
}
// multiply this vector by the scalar
multiplyBy(scalar) {
this.x *= scalar;
this.y *= scalar;
}
// scale this vector by scalar and return a new vector
divide(scalar) {
return new Vector(this.x / scalar, this.y / scalar);
}
// scale this vector by scalar
divideBy(scalar) {
this.x /= scalar;
this.y /= scalar;
}
// dot product of two vectors
dotProduct(v2) {
return this.x * v2.x + this.y * v2.y;
}
// normalize a given vector
normalize() {
return new Vector(
this.x / Math.sqrt(this.x * this.x + this.y * this.y),
this.y / Math.sqrt(this.x * this.x + this.y * this.y),
);
}
limit(max, factor) {
if (Math.abs(this.x) > max) {
this.x *= factor;
}
if (Math.abs(this.y) > max) {
this.y *= factor;
}
return this;
}
}
class Letter {
constructor(element, mouse) {
this.element = element;
this.mouse = mouse;
this.elementPos = new Vector();
this.opacity = 1;
this.target = new Vector();
this.pos = new Vector();
this.vel = new Vector();
this.acc = new Vector();
// tick, tick
const MS_PER_UPDATE = 4;
let [rAF, elapsed, previous, lag] = [null, 0, 0, 0];
const tick = (current) => {
//console.log(elapsed);
elapsed = current - previous;
previous = current;
lag += elapsed;
// Update or simulate
while (lag > MS_PER_UPDATE) {
this.update(MS_PER_UPDATE / 1000);
lag -= MS_PER_UPDATE;
}
// Render
this.draw(lag / MS_PER_UPDATE);
rAF = requestAnimationFrame(tick);
};
rAF = requestAnimationFrame(tick);
}
setRandomPosition() {
this.pos = new Vector(
(Math.random() - 0.5) * window.innerWidth,
(Math.random() - 0.5) * window.innerHeight,
);
this.vel = new Vector(
(Math.random() - 0.5) * 5000,
(Math.random() - 0.5) * 5000,
);
}
applyForce(force) {
this.acc.addTo(force);
}
behavoir() {
const arrive = this.target.subtract(this.pos);
// const distance = arrive.getMagnitude();
arrive.setMagnitude(60);
this.applyForce(arrive);
this.elementPos = new Vector(
this.element.getBoundingClientRect().x +
this.element.getBoundingClientRect().width / 2,
this.element.getBoundingClientRect().y +
this.element.getBoundingClientRect().height / 2,
);
const avoidMouse = this.pos.subtract(this.mouse.subtract(this.elementPos));
if (
avoidMouse.getMagnitude() < this.element.getBoundingClientRect().height
) {
avoidMouse.multiplyBy(1);
} else {
avoidMouse.multiplyBy(0);
}
this.applyForce(avoidMouse);
}
update(dt) {
this.behavoir();
this.vel.addTo(this.acc);
this.vel.limit(0.92, 0.92);
this.pos.addTo(this.vel, dt);
this.acc.multiplyBy(0);
}
draw() {
this.element.style.transform = `
translate3d(${this.pos.x}px,${this.pos.y}px,0)
`;
this.element.style.opacity = this.opacity;
}
}
class LetterParticles extends HTMLElement {
constructor() {
super();
this.letters = new Set();
}
connectedCallback() {
const mouse = new Vector();
document.addEventListener('mousemove', (event) => {
mouse.x = event.clientX;
mouse.y = event.clientY;
});
const letters = this.textContent.split('');
this.textContent = '';
letters.forEach((letter) => {
const element = document.createElement('span');
element.innerHTML = letter !== ' ' ? letter : ' ';
this.appendChild(element);
this.letters.add(new Letter(element, mouse));
});
const callback = (entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
this.letters.forEach((letter) => {
letter.setRandomPosition();
});
}
});
};
const observer = new IntersectionObserver(callback);
observer.observe(this);
}
}
customElements.define('letter-particles', LetterParticles);