-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepulsiveElements.js
170 lines (158 loc) · 4.62 KB
/
RepulsiveElements.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
/***
*
* Make elements avoid your mouse.
*
* Cody Smith 2020
*
*
*
* */
export class RepulsiveElements {
constructor(element, opts = {}) {
const config = {
textSplitter: "",
friction: 0.09,
springK: 1,
mouseK: 0.1,
scrollK: 0.3,
...opts,
};
this.friction = config.friction;
this.springK = config.springK;
this.mouseK = config.mouseK;
this.scrollK = config.scrollK;
this.mouseDecay = 0;
this.mouseX = 0;
this.mouseY = 0;
// Seperate words iinto spererate elements
const bees1 = elementizeStrings(element, config.textSplitter);
this.hive = bees1.map((b) => new Bee(b, element));
element.addEventListener("mousemove", (ev) => {
var rect = element.getBoundingClientRect();
var x = ev.clientX - rect.left; //x position within the element.
var y = ev.clientY - rect.top; //y position within the element.
this.moveMouse(x, y);
});
this.lastScrollPosition = 0;
window.addEventListener("scroll", (e) => {
const y = window.scrollY;
const change = this.lastScrollPosition - y;
this.hive.forEach((b) => {
b.y = b.y - this.scrollK * change;
});
this.lastScrollPosition = y;
});
this.animate();
}
animate() {
this.step(this.friction, this.springK, this.mouseK);
this.moveElements();
requestAnimationFrame(this.animate.bind(this));
}
moveMouse(mouseX, mouseY) {
this.mouseDecay = 100;
this.mouseX = mouseX;
this.mouseY = mouseY;
}
step(friction, springForce = 1, mouseForceK = 0.01) {
this.hive.forEach((b) => {
const dispX = b.x; //- b.originX;
const dispY = b.y; //- b.originY;
b.vx += -1 * springForce * friction * dispX; //elastic return
b.vy += -1 * springForce * friction * dispY;
const mdx = b.originX + b.w / 2 + b.x - this.mouseX; //mouse force
const mdy = b.originY + b.h / 2 + b.y - this.mouseY;
const md = Math.max(0.0001, Math.hypot(mdx, mdy)); //displacement from mouse
const mouseForce = mouseForceK / Math.pow(md / this.mouseDecay, 2);
b.vx += (mdx / md) * mouseForce;
b.vy += (mdy / md) * mouseForce;
b.vx = b.vx * (1 - friction); //friction
b.vy = b.vy * (1 - friction);
b.x += b.vx;
b.y += b.vy;
if (isNaN(b.vx)) {
console.log(b);
}
});
this.mouseDecay = Math.max(0, this.mouseDecay - 1);
}
moveElements() {
this.hive.forEach((b) => {
// dont update if not moving for energy saving
if (Math.hypot(b.x, b.y) > 0.01 || Math.hypot(b.vx, b.vy) > 0.01) {
const x = Math.round(b.x);
const y = Math.round(b.y);
b.element.style.transform = `translate(${x}px,${y}px)`; //`matrix3d(1, 0, 0, ${b.x}, 0, 1, 0, ${b.y}, 0, 0, 1, 0, 0, 0, 0, 1);`;
}
});
}
}
class Bee {
constructor(element, boundingElement) {
const pp = boundingElement.getBoundingClientRect();
const pe = element.getBoundingClientRect();
this.w = pe.width;
this.h = pe.height;
this.originX = pe.left - pp.left;
this.originY = pe.top - pp.top;
this.x = 0;
this.y = 0;
this.vx = 0;
this.vy = 0;
this.offsetX = 0;
this.offsetY = 0;
this.element = element;
}
}
function elementizeStrings(element, textSplitter, recursiveDepth = 6) {
const childElements = Array.from(element.childNodes);
element.textContent = undefined;
let elems = [];
childElements.forEach((a) => {
if (a.nodeType == 3) {
//text Node
const spans = textToSpans(a.wholeText, textSplitter);
elems = elems.concat(spans);
spans.forEach((b) => {
element.appendChild(b);
});
} else {
//regular node
if (recursiveDepth > 0) {
const subElements = elementizeStrings(
a,
textSplitter,
recursiveDepth - 1
);
elems = elems.concat(subElements);
element.appendChild(a);
} else {
elems.push(a);
element.appendChild(a);
}
}
});
return elems;
}
function textToSpans(text, splitter) {
let text1 = text.replace(/\s{2,}/g, " "); // remove extra whitespace
if (!splitter) splitter = "";
if (splitter.length == 0) text1 = Array.from(text1);
else {
text1 = text1.split(splitter);
text1 = text1.reduce((acc, cur) => {
const split2 = splitter;
return acc.concat([cur, split2]);
}, []);
// text1 = text1.replace(/\s/g, " ");
}
const result = [];
text1.forEach((v) => {
const elm = document.createElement("span");
elm.style.display = "inline-block";
elm.style["white-space"] = "pre";
elm.textContent = v;
result.push(elm);
});
return result;
}