@elementalfx/gravity 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +125 -2
- package/dist/body.d.ts +52 -0
- package/dist/body.d.ts.map +1 -0
- package/dist/chunks/chunk-VP6BXQKB.js +1430 -0
- package/dist/chunks/chunk-VP6BXQKB.js.map +7 -0
- package/dist/collide.d.ts +18 -0
- package/dist/collide.d.ts.map +1 -0
- package/dist/element.d.ts +73 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +38 -0
- package/dist/element.js.map +7 -0
- package/dist/global.d.ts +3 -0
- package/dist/global.d.ts.map +1 -0
- package/dist/grab.d.ts +37 -0
- package/dist/grab.d.ts.map +1 -0
- package/dist/gravity.d.ts +35 -0
- package/dist/gravity.d.ts.map +1 -0
- package/dist/gravity.global.js +2 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +7 -0
- package/dist/options.d.ts +62 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/pick.d.ts +18 -0
- package/dist/pick.d.ts.map +1 -0
- package/dist/world.d.ts +61 -0
- package/dist/world.d.ts.map +1 -0
- package/package.json +71 -3
|
@@ -0,0 +1,1430 @@
|
|
|
1
|
+
// packages/gravity/src/body.ts
|
|
2
|
+
var _Body = class _Body {
|
|
3
|
+
constructor(options) {
|
|
4
|
+
this.id = _Body.nextId++;
|
|
5
|
+
this.vx = 0;
|
|
6
|
+
this.vy = 0;
|
|
7
|
+
this.w = 0;
|
|
8
|
+
this.sleeping = false;
|
|
9
|
+
this.sleepTime = 0;
|
|
10
|
+
/** @internal The bodies that fell asleep together with this one; they wake together too. */
|
|
11
|
+
this.island = null;
|
|
12
|
+
/** @internal Union-find parent while grouping bodies into islands. */
|
|
13
|
+
this.root = this;
|
|
14
|
+
/** Arbitrary data, e.g. the DOM element this body moves. */
|
|
15
|
+
this.data = null;
|
|
16
|
+
this.x = options.x;
|
|
17
|
+
this.y = options.y;
|
|
18
|
+
this.angle = options.angle ?? 0;
|
|
19
|
+
this.shape = options.shape ?? "box";
|
|
20
|
+
this.hw = options.width / 2;
|
|
21
|
+
this.hh = options.height / 2;
|
|
22
|
+
this.radius = Math.min(this.hw, this.hh);
|
|
23
|
+
this.friction = options.friction ?? 0.5;
|
|
24
|
+
this.restitution = options.restitution ?? 0.2;
|
|
25
|
+
const density = options.density ?? 1;
|
|
26
|
+
if (density <= 0) {
|
|
27
|
+
this.mass = Infinity;
|
|
28
|
+
this.invMass = 0;
|
|
29
|
+
this.invInertia = 0;
|
|
30
|
+
} else if (this.shape === "circle") {
|
|
31
|
+
this.mass = Math.PI * this.radius * this.radius * density;
|
|
32
|
+
this.invMass = 1 / this.mass;
|
|
33
|
+
this.invInertia = 1 / (0.5 * this.mass * this.radius * this.radius);
|
|
34
|
+
} else {
|
|
35
|
+
this.mass = 4 * this.hw * this.hh * density;
|
|
36
|
+
this.invMass = 1 / this.mass;
|
|
37
|
+
this.invInertia = 1 / (this.mass * (4 * this.hw * this.hw + 4 * this.hh * this.hh) / 12);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
get isStatic() {
|
|
41
|
+
return this.invMass === 0;
|
|
42
|
+
}
|
|
43
|
+
/** Wake this body and everything that fell asleep in contact with it. */
|
|
44
|
+
wake() {
|
|
45
|
+
this.sleepTime = 0;
|
|
46
|
+
if (!this.sleeping) return;
|
|
47
|
+
const island = this.island ?? [this];
|
|
48
|
+
for (const body of island) {
|
|
49
|
+
body.sleeping = false;
|
|
50
|
+
body.sleepTime = 0;
|
|
51
|
+
body.island = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/** Axis-aligned bounds: [minX, minY, maxX, maxY]. */
|
|
55
|
+
bounds() {
|
|
56
|
+
if (this.shape === "circle") {
|
|
57
|
+
return [this.x - this.radius, this.y - this.radius, this.x + this.radius, this.y + this.radius];
|
|
58
|
+
}
|
|
59
|
+
const c = Math.abs(Math.cos(this.angle));
|
|
60
|
+
const s = Math.abs(Math.sin(this.angle));
|
|
61
|
+
const ex = this.hw * c + this.hh * s;
|
|
62
|
+
const ey = this.hw * s + this.hh * c;
|
|
63
|
+
return [this.x - ex, this.y - ey, this.x + ex, this.y + ey];
|
|
64
|
+
}
|
|
65
|
+
/** Applies an impulse (px·mass/s) at a world point. */
|
|
66
|
+
applyImpulse(px, py, atX = this.x, atY = this.y) {
|
|
67
|
+
if (this.isStatic) return;
|
|
68
|
+
this.wake();
|
|
69
|
+
this.vx += px * this.invMass;
|
|
70
|
+
this.vy += py * this.invMass;
|
|
71
|
+
this.w += this.invInertia * ((atX - this.x) * py - (atY - this.y) * px);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
_Body.nextId = 1;
|
|
75
|
+
var Body = _Body;
|
|
76
|
+
|
|
77
|
+
// packages/gravity/src/collide.ts
|
|
78
|
+
var EDGE1 = 1;
|
|
79
|
+
var EDGE2 = 2;
|
|
80
|
+
var EDGE3 = 3;
|
|
81
|
+
var EDGE4 = 4;
|
|
82
|
+
var feature = (v) => v.in1 | v.out1 << 4 | v.in2 << 8 | v.out2 << 12;
|
|
83
|
+
var flippedFeature = (v) => v.in2 | v.out2 << 4 | v.in1 << 8 | v.out1 << 12;
|
|
84
|
+
function clipSegment(input, nx, ny, offset, clipEdge) {
|
|
85
|
+
const out = [];
|
|
86
|
+
const [v0, v1] = input;
|
|
87
|
+
const d0 = nx * v0.x + ny * v0.y - offset;
|
|
88
|
+
const d1 = nx * v1.x + ny * v1.y - offset;
|
|
89
|
+
if (d0 <= 0) out.push(v0);
|
|
90
|
+
if (d1 <= 0) out.push(v1);
|
|
91
|
+
if (d0 * d1 < 0) {
|
|
92
|
+
const t = d0 / (d0 - d1);
|
|
93
|
+
const x = v0.x + t * (v1.x - v0.x);
|
|
94
|
+
const y = v0.y + t * (v1.y - v0.y);
|
|
95
|
+
if (d0 > 0) out.push({ ...v0, x, y, in1: clipEdge, in2: 0 });
|
|
96
|
+
else out.push({ ...v1, x, y, out1: clipEdge, out2: 0 });
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
function incidentEdge(hx, hy, px, py, c, s, nx, ny) {
|
|
101
|
+
const lx = -(c * nx + s * ny);
|
|
102
|
+
const ly = -(-s * nx + c * ny);
|
|
103
|
+
let v;
|
|
104
|
+
if (Math.abs(lx) > Math.abs(ly)) {
|
|
105
|
+
v = lx > 0 ? [
|
|
106
|
+
{ x: hx, y: -hy, in1: 0, out1: 0, in2: EDGE3, out2: EDGE4 },
|
|
107
|
+
{ x: hx, y: hy, in1: 0, out1: 0, in2: EDGE4, out2: EDGE1 }
|
|
108
|
+
] : [
|
|
109
|
+
{ x: -hx, y: hy, in1: 0, out1: 0, in2: EDGE1, out2: EDGE2 },
|
|
110
|
+
{ x: -hx, y: -hy, in1: 0, out1: 0, in2: EDGE2, out2: EDGE3 }
|
|
111
|
+
];
|
|
112
|
+
} else {
|
|
113
|
+
v = ly > 0 ? [
|
|
114
|
+
{ x: hx, y: hy, in1: 0, out1: 0, in2: EDGE4, out2: EDGE1 },
|
|
115
|
+
{ x: -hx, y: hy, in1: 0, out1: 0, in2: EDGE1, out2: EDGE2 }
|
|
116
|
+
] : [
|
|
117
|
+
{ x: -hx, y: -hy, in1: 0, out1: 0, in2: EDGE2, out2: EDGE3 },
|
|
118
|
+
{ x: hx, y: -hy, in1: 0, out1: 0, in2: EDGE3, out2: EDGE4 }
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
for (const p of v) {
|
|
122
|
+
const x = px + c * p.x - s * p.y;
|
|
123
|
+
const y = py + s * p.x + c * p.y;
|
|
124
|
+
p.x = x;
|
|
125
|
+
p.y = y;
|
|
126
|
+
}
|
|
127
|
+
return v;
|
|
128
|
+
}
|
|
129
|
+
function contact(x, y, nx, ny, separation, feature2) {
|
|
130
|
+
return { x, y, nx, ny, separation, feature: feature2, pn: 0, pt: 0, massNormal: 0, massTangent: 0, bias: 0 };
|
|
131
|
+
}
|
|
132
|
+
function collideBoxes(A, B) {
|
|
133
|
+
const ca = Math.cos(A.angle);
|
|
134
|
+
const sa = Math.sin(A.angle);
|
|
135
|
+
const cb = Math.cos(B.angle);
|
|
136
|
+
const sb = Math.sin(B.angle);
|
|
137
|
+
const dpx = B.x - A.x;
|
|
138
|
+
const dpy = B.y - A.y;
|
|
139
|
+
const dAx = ca * dpx + sa * dpy;
|
|
140
|
+
const dAy = -sa * dpx + ca * dpy;
|
|
141
|
+
const dBx = cb * dpx + sb * dpy;
|
|
142
|
+
const dBy = -sb * dpx + cb * dpy;
|
|
143
|
+
const c11 = ca * cb + sa * sb;
|
|
144
|
+
const c12 = -ca * sb + sa * cb;
|
|
145
|
+
const c21 = -sa * cb + ca * sb;
|
|
146
|
+
const c22 = sa * sb + ca * cb;
|
|
147
|
+
const a11 = Math.abs(c11);
|
|
148
|
+
const a12 = Math.abs(c12);
|
|
149
|
+
const a21 = Math.abs(c21);
|
|
150
|
+
const a22 = Math.abs(c22);
|
|
151
|
+
const faceAx = Math.abs(dAx) - A.hw - (a11 * B.hw + a12 * B.hh);
|
|
152
|
+
const faceAy = Math.abs(dAy) - A.hh - (a21 * B.hw + a22 * B.hh);
|
|
153
|
+
if (faceAx > 0 || faceAy > 0) return [];
|
|
154
|
+
const faceBx = Math.abs(dBx) - (a11 * A.hw + a21 * A.hh) - B.hw;
|
|
155
|
+
const faceBy = Math.abs(dBy) - (a12 * A.hw + a22 * A.hh) - B.hh;
|
|
156
|
+
if (faceBx > 0 || faceBy > 0) return [];
|
|
157
|
+
const relativeTol = 0.95;
|
|
158
|
+
const absoluteTol = 0.01;
|
|
159
|
+
let axis = 0;
|
|
160
|
+
let separation = faceAx;
|
|
161
|
+
let nx = dAx > 0 ? ca : -ca;
|
|
162
|
+
let ny = dAx > 0 ? sa : -sa;
|
|
163
|
+
if (faceAy > relativeTol * separation + absoluteTol * A.hh) {
|
|
164
|
+
axis = 1;
|
|
165
|
+
separation = faceAy;
|
|
166
|
+
nx = dAy > 0 ? -sa : sa;
|
|
167
|
+
ny = dAy > 0 ? ca : -ca;
|
|
168
|
+
}
|
|
169
|
+
if (faceBx > relativeTol * separation + absoluteTol * B.hw) {
|
|
170
|
+
axis = 2;
|
|
171
|
+
separation = faceBx;
|
|
172
|
+
nx = dBx > 0 ? cb : -cb;
|
|
173
|
+
ny = dBx > 0 ? sb : -sb;
|
|
174
|
+
}
|
|
175
|
+
if (faceBy > relativeTol * separation + absoluteTol * B.hh) {
|
|
176
|
+
axis = 3;
|
|
177
|
+
separation = faceBy;
|
|
178
|
+
nx = dBy > 0 ? -sb : sb;
|
|
179
|
+
ny = dBy > 0 ? cb : -cb;
|
|
180
|
+
}
|
|
181
|
+
let fnx, fny, front, snx, sny;
|
|
182
|
+
let negSide, posSide, negEdge, posEdge;
|
|
183
|
+
let incident;
|
|
184
|
+
if (axis === 0) {
|
|
185
|
+
fnx = nx;
|
|
186
|
+
fny = ny;
|
|
187
|
+
front = A.x * fnx + A.y * fny + A.hw;
|
|
188
|
+
snx = -sa;
|
|
189
|
+
sny = ca;
|
|
190
|
+
const side = A.x * snx + A.y * sny;
|
|
191
|
+
negSide = -side + A.hh;
|
|
192
|
+
posSide = side + A.hh;
|
|
193
|
+
negEdge = EDGE3;
|
|
194
|
+
posEdge = EDGE1;
|
|
195
|
+
incident = incidentEdge(B.hw, B.hh, B.x, B.y, cb, sb, fnx, fny);
|
|
196
|
+
} else if (axis === 1) {
|
|
197
|
+
fnx = nx;
|
|
198
|
+
fny = ny;
|
|
199
|
+
front = A.x * fnx + A.y * fny + A.hh;
|
|
200
|
+
snx = ca;
|
|
201
|
+
sny = sa;
|
|
202
|
+
const side = A.x * snx + A.y * sny;
|
|
203
|
+
negSide = -side + A.hw;
|
|
204
|
+
posSide = side + A.hw;
|
|
205
|
+
negEdge = EDGE2;
|
|
206
|
+
posEdge = EDGE4;
|
|
207
|
+
incident = incidentEdge(B.hw, B.hh, B.x, B.y, cb, sb, fnx, fny);
|
|
208
|
+
} else if (axis === 2) {
|
|
209
|
+
fnx = -nx;
|
|
210
|
+
fny = -ny;
|
|
211
|
+
front = B.x * fnx + B.y * fny + B.hw;
|
|
212
|
+
snx = -sb;
|
|
213
|
+
sny = cb;
|
|
214
|
+
const side = B.x * snx + B.y * sny;
|
|
215
|
+
negSide = -side + B.hh;
|
|
216
|
+
posSide = side + B.hh;
|
|
217
|
+
negEdge = EDGE3;
|
|
218
|
+
posEdge = EDGE1;
|
|
219
|
+
incident = incidentEdge(A.hw, A.hh, A.x, A.y, ca, sa, fnx, fny);
|
|
220
|
+
} else {
|
|
221
|
+
fnx = -nx;
|
|
222
|
+
fny = -ny;
|
|
223
|
+
front = B.x * fnx + B.y * fny + B.hh;
|
|
224
|
+
snx = cb;
|
|
225
|
+
sny = sb;
|
|
226
|
+
const side = B.x * snx + B.y * sny;
|
|
227
|
+
negSide = -side + B.hw;
|
|
228
|
+
posSide = side + B.hw;
|
|
229
|
+
negEdge = EDGE2;
|
|
230
|
+
posEdge = EDGE4;
|
|
231
|
+
incident = incidentEdge(A.hw, A.hh, A.x, A.y, ca, sa, fnx, fny);
|
|
232
|
+
}
|
|
233
|
+
const clip1 = clipSegment(incident, -snx, -sny, negSide, negEdge);
|
|
234
|
+
if (clip1.length < 2) return [];
|
|
235
|
+
const clip2 = clipSegment(clip1, snx, sny, posSide, posEdge);
|
|
236
|
+
if (clip2.length < 2) return [];
|
|
237
|
+
const contacts = [];
|
|
238
|
+
for (const v of clip2) {
|
|
239
|
+
const sep = fnx * v.x + fny * v.y - front;
|
|
240
|
+
if (sep <= 0) {
|
|
241
|
+
contacts.push(
|
|
242
|
+
contact(v.x - sep * fnx, v.y - sep * fny, nx, ny, sep, axis >= 2 ? flippedFeature(v) : feature(v))
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return contacts;
|
|
247
|
+
}
|
|
248
|
+
function collideBoxCircle(A, B) {
|
|
249
|
+
const c = Math.cos(A.angle);
|
|
250
|
+
const s = Math.sin(A.angle);
|
|
251
|
+
const dx = B.x - A.x;
|
|
252
|
+
const dy = B.y - A.y;
|
|
253
|
+
const lx = c * dx + s * dy;
|
|
254
|
+
const ly = -s * dx + c * dy;
|
|
255
|
+
const qx = Math.max(-A.hw, Math.min(A.hw, lx));
|
|
256
|
+
const qy = Math.max(-A.hh, Math.min(A.hh, ly));
|
|
257
|
+
let nlx, nly, separation, px, py;
|
|
258
|
+
if (qx === lx && qy === ly) {
|
|
259
|
+
const ex = A.hw - Math.abs(lx);
|
|
260
|
+
const ey = A.hh - Math.abs(ly);
|
|
261
|
+
if (ex < ey) {
|
|
262
|
+
nlx = lx >= 0 ? 1 : -1;
|
|
263
|
+
nly = 0;
|
|
264
|
+
separation = -ex - B.radius;
|
|
265
|
+
px = nlx * A.hw;
|
|
266
|
+
py = ly;
|
|
267
|
+
} else {
|
|
268
|
+
nlx = 0;
|
|
269
|
+
nly = ly >= 0 ? 1 : -1;
|
|
270
|
+
separation = -ey - B.radius;
|
|
271
|
+
px = lx;
|
|
272
|
+
py = nly * A.hh;
|
|
273
|
+
}
|
|
274
|
+
} else {
|
|
275
|
+
const ox = lx - qx;
|
|
276
|
+
const oy = ly - qy;
|
|
277
|
+
const distance = Math.hypot(ox, oy);
|
|
278
|
+
separation = distance - B.radius;
|
|
279
|
+
if (separation > 0) return [];
|
|
280
|
+
nlx = ox / distance;
|
|
281
|
+
nly = oy / distance;
|
|
282
|
+
px = qx;
|
|
283
|
+
py = qy;
|
|
284
|
+
}
|
|
285
|
+
const nx = c * nlx - s * nly;
|
|
286
|
+
const ny = s * nlx + c * nly;
|
|
287
|
+
const wx = A.x + c * px - s * py;
|
|
288
|
+
const wy = A.y + s * px + c * py;
|
|
289
|
+
return [contact(wx, wy, nx, ny, separation, 0)];
|
|
290
|
+
}
|
|
291
|
+
function collideCircles(A, B) {
|
|
292
|
+
const dx = B.x - A.x;
|
|
293
|
+
const dy = B.y - A.y;
|
|
294
|
+
const distance = Math.hypot(dx, dy);
|
|
295
|
+
const separation = distance - A.radius - B.radius;
|
|
296
|
+
if (separation > 0) return [];
|
|
297
|
+
const nx = distance > 1e-6 ? dx / distance : 0;
|
|
298
|
+
const ny = distance > 1e-6 ? dy / distance : 1;
|
|
299
|
+
return [contact(A.x + nx * (A.radius + separation / 2), A.y + ny * (A.radius + separation / 2), nx, ny, separation, 0)];
|
|
300
|
+
}
|
|
301
|
+
function collide(a, b) {
|
|
302
|
+
if (a.shape === "box" && b.shape === "box") return collideBoxes(a, b);
|
|
303
|
+
if (a.shape === "circle" && b.shape === "circle") return collideCircles(a, b);
|
|
304
|
+
if (a.shape === "box") return collideBoxCircle(a, b);
|
|
305
|
+
return collideBoxCircle(b, a).map((c) => ({ ...c, nx: -c.nx, ny: -c.ny }));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// packages/gravity/src/grab.ts
|
|
309
|
+
var Grab = class {
|
|
310
|
+
constructor(body, x, y, frequency = 6, damping = 0.8) {
|
|
311
|
+
this.body = body;
|
|
312
|
+
this.frequency = frequency;
|
|
313
|
+
this.damping = damping;
|
|
314
|
+
this.ix = 0;
|
|
315
|
+
this.iy = 0;
|
|
316
|
+
this.rx = 0;
|
|
317
|
+
this.ry = 0;
|
|
318
|
+
this.m11 = 0;
|
|
319
|
+
this.m12 = 0;
|
|
320
|
+
this.m22 = 0;
|
|
321
|
+
this.cx = 0;
|
|
322
|
+
this.cy = 0;
|
|
323
|
+
this.gamma = 0;
|
|
324
|
+
this.maxImpulse = 0;
|
|
325
|
+
const c = Math.cos(body.angle);
|
|
326
|
+
const s = Math.sin(body.angle);
|
|
327
|
+
const dx = x - body.x;
|
|
328
|
+
const dy = y - body.y;
|
|
329
|
+
this.localX = c * dx + s * dy;
|
|
330
|
+
this.localY = -s * dx + c * dy;
|
|
331
|
+
this.targetX = x;
|
|
332
|
+
this.targetY = y;
|
|
333
|
+
}
|
|
334
|
+
moveTo(x, y) {
|
|
335
|
+
this.targetX = x;
|
|
336
|
+
this.targetY = y;
|
|
337
|
+
this.body.wake();
|
|
338
|
+
}
|
|
339
|
+
/** @internal */
|
|
340
|
+
prepare(dt) {
|
|
341
|
+
const b = this.body;
|
|
342
|
+
const c = Math.cos(b.angle);
|
|
343
|
+
const s = Math.sin(b.angle);
|
|
344
|
+
this.rx = c * this.localX - s * this.localY;
|
|
345
|
+
this.ry = s * this.localX + c * this.localY;
|
|
346
|
+
const omega = 2 * Math.PI * this.frequency;
|
|
347
|
+
const d = 2 * b.mass * this.damping * omega;
|
|
348
|
+
const k = b.mass * omega * omega;
|
|
349
|
+
this.gamma = 1 / (dt * (d + dt * k));
|
|
350
|
+
const beta = dt * k * this.gamma;
|
|
351
|
+
const im = b.invMass;
|
|
352
|
+
const ii = b.invInertia;
|
|
353
|
+
const k11 = im + ii * this.ry * this.ry + this.gamma;
|
|
354
|
+
const k12 = -ii * this.rx * this.ry;
|
|
355
|
+
const k22 = im + ii * this.rx * this.rx + this.gamma;
|
|
356
|
+
const det = k11 * k22 - k12 * k12;
|
|
357
|
+
const inv = det !== 0 ? 1 / det : 0;
|
|
358
|
+
this.m11 = inv * k22;
|
|
359
|
+
this.m12 = -inv * k12;
|
|
360
|
+
this.m22 = inv * k11;
|
|
361
|
+
this.cx = (b.x + this.rx - this.targetX) * beta;
|
|
362
|
+
this.cy = (b.y + this.ry - this.targetY) * beta;
|
|
363
|
+
this.maxImpulse = b.mass * 2e5 * dt;
|
|
364
|
+
b.w *= 0.98;
|
|
365
|
+
this.apply(this.ix, this.iy);
|
|
366
|
+
}
|
|
367
|
+
/** @internal */
|
|
368
|
+
solve() {
|
|
369
|
+
const b = this.body;
|
|
370
|
+
const vx = b.vx - b.w * this.ry;
|
|
371
|
+
const vy = b.vy + b.w * this.rx;
|
|
372
|
+
const ex = -(vx + this.cx + this.gamma * this.ix);
|
|
373
|
+
const ey = -(vy + this.cy + this.gamma * this.iy);
|
|
374
|
+
let dx = this.m11 * ex + this.m12 * ey;
|
|
375
|
+
let dy = this.m12 * ex + this.m22 * ey;
|
|
376
|
+
const ox = this.ix;
|
|
377
|
+
const oy = this.iy;
|
|
378
|
+
this.ix += dx;
|
|
379
|
+
this.iy += dy;
|
|
380
|
+
const length = Math.hypot(this.ix, this.iy);
|
|
381
|
+
if (length > this.maxImpulse) {
|
|
382
|
+
this.ix *= this.maxImpulse / length;
|
|
383
|
+
this.iy *= this.maxImpulse / length;
|
|
384
|
+
}
|
|
385
|
+
dx = this.ix - ox;
|
|
386
|
+
dy = this.iy - oy;
|
|
387
|
+
this.apply(dx, dy);
|
|
388
|
+
}
|
|
389
|
+
apply(px, py) {
|
|
390
|
+
const b = this.body;
|
|
391
|
+
b.vx += b.invMass * px;
|
|
392
|
+
b.vy += b.invMass * py;
|
|
393
|
+
b.w += b.invInertia * (this.rx * py - this.ry * px);
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
// packages/gravity/src/world.ts
|
|
398
|
+
var Arbiter = class {
|
|
399
|
+
constructor(a, b) {
|
|
400
|
+
this.a = a;
|
|
401
|
+
this.b = b;
|
|
402
|
+
this.contacts = [];
|
|
403
|
+
this.friction = Math.sqrt(a.friction * b.friction);
|
|
404
|
+
this.restitution = Math.max(a.restitution, b.restitution);
|
|
405
|
+
}
|
|
406
|
+
/** Replaces contacts, keeping accumulated impulses for matching features (warm starting). */
|
|
407
|
+
update(next) {
|
|
408
|
+
for (const contact2 of next) {
|
|
409
|
+
const previous = this.contacts.find((c) => c.feature === contact2.feature);
|
|
410
|
+
if (previous) {
|
|
411
|
+
contact2.pn = previous.pn;
|
|
412
|
+
contact2.pt = previous.pt;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
this.contacts = next;
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
var ALLOWED_PENETRATION = 0.75;
|
|
419
|
+
var BIAS_FACTOR = 0.2;
|
|
420
|
+
var RESTITUTION_THRESHOLD = 60;
|
|
421
|
+
var SLEEP_SPEED = 6;
|
|
422
|
+
var SLEEP_TIME = 0.5;
|
|
423
|
+
var MAX_FRAME = 1 / 15;
|
|
424
|
+
var MAX_SPEED = 5e3;
|
|
425
|
+
var World = class {
|
|
426
|
+
constructor(options = {}) {
|
|
427
|
+
this.bodies = [];
|
|
428
|
+
this.arbiters = /* @__PURE__ */ new Map();
|
|
429
|
+
this.grabs = [];
|
|
430
|
+
this.pending = 0;
|
|
431
|
+
this.gravity = options.gravity ?? 2e3;
|
|
432
|
+
this.iterations = options.iterations ?? 6;
|
|
433
|
+
this.rate = options.rate ?? 240;
|
|
434
|
+
}
|
|
435
|
+
add(body) {
|
|
436
|
+
this.bodies.push(body);
|
|
437
|
+
return body;
|
|
438
|
+
}
|
|
439
|
+
remove(body) {
|
|
440
|
+
this.bodies = this.bodies.filter((b) => b !== body);
|
|
441
|
+
this.grabs = this.grabs.filter((g) => g.body !== body);
|
|
442
|
+
this.wakeTouching(body);
|
|
443
|
+
for (const [key, arbiter] of this.arbiters) {
|
|
444
|
+
if (arbiter.a === body || arbiter.b === body) this.arbiters.delete(key);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
clear() {
|
|
448
|
+
this.bodies = [];
|
|
449
|
+
this.grabs = [];
|
|
450
|
+
this.arbiters.clear();
|
|
451
|
+
}
|
|
452
|
+
/** Start dragging `body` by the world point (x, y). Move it with `grab.moveTo`. */
|
|
453
|
+
grab(body, x, y) {
|
|
454
|
+
const grab = new Grab(body, x, y);
|
|
455
|
+
this.grabs.push(grab);
|
|
456
|
+
this.wakeTouching(body);
|
|
457
|
+
body.wake();
|
|
458
|
+
return grab;
|
|
459
|
+
}
|
|
460
|
+
release(grab) {
|
|
461
|
+
this.grabs = this.grabs.filter((g) => g !== grab);
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Wake every body touching `body`, e.g. before pulling it out of a pile.
|
|
465
|
+
* Goes by bounds rather than contacts, since two sleeping bodies keep none.
|
|
466
|
+
*/
|
|
467
|
+
wakeTouching(body) {
|
|
468
|
+
const [x0, y0, x1, y1] = body.bounds();
|
|
469
|
+
const slack = 2;
|
|
470
|
+
for (const other of this.bodies) {
|
|
471
|
+
if (other === body || !other.sleeping) continue;
|
|
472
|
+
const [u0, v0, u1, v1] = other.bounds();
|
|
473
|
+
if (u0 <= x1 + slack && u1 >= x0 - slack && v0 <= y1 + slack && v1 >= y0 - slack) other.wake();
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/** Put every body to sleep at once, as one island, e.g. to stop a pile that keeps fidgeting. */
|
|
477
|
+
sleepAll() {
|
|
478
|
+
const island = this.bodies.filter((b) => !b.isStatic && this.grabs.every((g) => g.body !== b));
|
|
479
|
+
for (const b of island) {
|
|
480
|
+
b.sleeping = true;
|
|
481
|
+
b.island = island;
|
|
482
|
+
b.vx = b.vy = b.w = 0;
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
/** True when every dynamic body is asleep. */
|
|
486
|
+
get settled() {
|
|
487
|
+
return this.bodies.every((b) => b.isStatic || b.sleeping);
|
|
488
|
+
}
|
|
489
|
+
/** Advance by `dt` seconds in fixed substeps. Returns the number of substeps run. */
|
|
490
|
+
step(dt) {
|
|
491
|
+
if (!(dt > 0)) return 0;
|
|
492
|
+
const h = 1 / this.rate;
|
|
493
|
+
this.pending = Math.min(this.pending + dt, MAX_FRAME);
|
|
494
|
+
let count = 0;
|
|
495
|
+
while (this.pending >= h * 0.999) {
|
|
496
|
+
this.substep(h);
|
|
497
|
+
this.pending = Math.max(0, this.pending - h);
|
|
498
|
+
count++;
|
|
499
|
+
}
|
|
500
|
+
return count;
|
|
501
|
+
}
|
|
502
|
+
substep(dt) {
|
|
503
|
+
const invDt = 1 / dt;
|
|
504
|
+
this.broadphase();
|
|
505
|
+
for (const b of this.bodies) {
|
|
506
|
+
if (b.isStatic || b.sleeping) continue;
|
|
507
|
+
b.vy += this.gravity * dt;
|
|
508
|
+
const drag = 1 / (1 + dt * 0.05);
|
|
509
|
+
b.vx *= drag;
|
|
510
|
+
b.vy *= drag;
|
|
511
|
+
b.w *= 1 / (1 + dt * 0.4);
|
|
512
|
+
}
|
|
513
|
+
for (const arbiter of this.arbiters.values()) this.preStep(arbiter, invDt);
|
|
514
|
+
for (const arbiter of this.arbiters.values()) this.warmStart(arbiter);
|
|
515
|
+
for (const grab of this.grabs) grab.prepare(dt);
|
|
516
|
+
for (let i = 0; i < this.iterations; i++) {
|
|
517
|
+
for (const grab of this.grabs) grab.solve();
|
|
518
|
+
for (const arbiter of this.arbiters.values()) this.applyImpulses(arbiter);
|
|
519
|
+
}
|
|
520
|
+
for (const b of this.bodies) {
|
|
521
|
+
if (b.isStatic || b.sleeping) continue;
|
|
522
|
+
const speed = Math.hypot(b.vx, b.vy);
|
|
523
|
+
if (speed > MAX_SPEED) {
|
|
524
|
+
b.vx *= MAX_SPEED / speed;
|
|
525
|
+
b.vy *= MAX_SPEED / speed;
|
|
526
|
+
}
|
|
527
|
+
b.x += b.vx * dt;
|
|
528
|
+
b.y += b.vy * dt;
|
|
529
|
+
b.angle += b.w * dt;
|
|
530
|
+
const edge = Math.abs(b.w) * Math.hypot(b.hw, b.hh);
|
|
531
|
+
const still = Math.hypot(b.vx, b.vy) + edge < SLEEP_SPEED;
|
|
532
|
+
b.sleepTime = still ? b.sleepTime + dt : 0;
|
|
533
|
+
}
|
|
534
|
+
for (const grab of this.grabs) grab.body.sleepTime = 0;
|
|
535
|
+
this.sleepIslands();
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Groups awake bodies that touch into islands and puts an island to sleep
|
|
539
|
+
* once all of it has been still for SLEEP_TIME. A body resting on another
|
|
540
|
+
* can't sleep alone, so nothing is ever pinned under a sleeping neighbour.
|
|
541
|
+
*/
|
|
542
|
+
sleepIslands() {
|
|
543
|
+
const find = (body) => {
|
|
544
|
+
while (body.root !== body) {
|
|
545
|
+
body.root = body.root.root;
|
|
546
|
+
body = body.root;
|
|
547
|
+
}
|
|
548
|
+
return body;
|
|
549
|
+
};
|
|
550
|
+
const awake = this.bodies.filter((b) => !b.isStatic && !b.sleeping);
|
|
551
|
+
for (const b of awake) b.root = b;
|
|
552
|
+
for (const { a, b } of this.arbiters.values()) {
|
|
553
|
+
if (a.isStatic || b.isStatic || a.sleeping || b.sleeping) continue;
|
|
554
|
+
const ra = find(a);
|
|
555
|
+
const rb = find(b);
|
|
556
|
+
if (ra !== rb) ra.root = rb;
|
|
557
|
+
}
|
|
558
|
+
const islands = /* @__PURE__ */ new Map();
|
|
559
|
+
const restless = /* @__PURE__ */ new Set();
|
|
560
|
+
for (const b of awake) {
|
|
561
|
+
const root = find(b);
|
|
562
|
+
let island = islands.get(root);
|
|
563
|
+
if (!island) islands.set(root, island = []);
|
|
564
|
+
island.push(b);
|
|
565
|
+
if (b.sleepTime < SLEEP_TIME) restless.add(root);
|
|
566
|
+
}
|
|
567
|
+
for (const [root, island] of islands) {
|
|
568
|
+
if (restless.has(root)) continue;
|
|
569
|
+
for (const b of island) {
|
|
570
|
+
b.sleeping = true;
|
|
571
|
+
b.island = island;
|
|
572
|
+
b.vx = b.vy = b.w = 0;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/** Sort-and-sweep on x to find overlapping pairs, then narrow-phase contacts. */
|
|
577
|
+
broadphase() {
|
|
578
|
+
const entries = this.bodies.map((body) => ({ body, box: body.bounds() }));
|
|
579
|
+
entries.sort((p, q) => p.box[0] - q.box[0]);
|
|
580
|
+
const seen = /* @__PURE__ */ new Set();
|
|
581
|
+
for (let i = 0; i < entries.length; i++) {
|
|
582
|
+
const p = entries[i];
|
|
583
|
+
for (let j = i + 1; j < entries.length; j++) {
|
|
584
|
+
const q = entries[j];
|
|
585
|
+
if (q.box[0] > p.box[2]) break;
|
|
586
|
+
if (q.box[1] > p.box[3] || q.box[3] < p.box[1]) continue;
|
|
587
|
+
let a = p.body;
|
|
588
|
+
let b = q.body;
|
|
589
|
+
if ((a.isStatic || a.sleeping) && (b.isStatic || b.sleeping)) continue;
|
|
590
|
+
if (a.id > b.id) [a, b] = [b, a];
|
|
591
|
+
const key = `${a.id}:${b.id}`;
|
|
592
|
+
const contacts = collide(a, b);
|
|
593
|
+
if (contacts.length === 0) continue;
|
|
594
|
+
if (a.sleeping && !b.isStatic) a.wake();
|
|
595
|
+
if (b.sleeping && !a.isStatic) b.wake();
|
|
596
|
+
seen.add(key);
|
|
597
|
+
let arbiter = this.arbiters.get(key);
|
|
598
|
+
if (!arbiter) {
|
|
599
|
+
arbiter = new Arbiter(a, b);
|
|
600
|
+
this.arbiters.set(key, arbiter);
|
|
601
|
+
}
|
|
602
|
+
arbiter.update(contacts);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
for (const key of this.arbiters.keys()) if (!seen.has(key)) this.arbiters.delete(key);
|
|
606
|
+
}
|
|
607
|
+
preStep(arbiter, invDt) {
|
|
608
|
+
const { a, b } = arbiter;
|
|
609
|
+
const ima = a.sleeping ? 0 : a.invMass;
|
|
610
|
+
const imb = b.sleeping ? 0 : b.invMass;
|
|
611
|
+
const iia = a.sleeping ? 0 : a.invInertia;
|
|
612
|
+
const iib = b.sleeping ? 0 : b.invInertia;
|
|
613
|
+
for (const c of arbiter.contacts) {
|
|
614
|
+
const r1x = c.x - a.x;
|
|
615
|
+
const r1y = c.y - a.y;
|
|
616
|
+
const r2x = c.x - b.x;
|
|
617
|
+
const r2y = c.y - b.y;
|
|
618
|
+
const rn1 = r1x * c.nx + r1y * c.ny;
|
|
619
|
+
const rn2 = r2x * c.nx + r2y * c.ny;
|
|
620
|
+
const kNormal = ima + imb + iia * (r1x * r1x + r1y * r1y - rn1 * rn1) + iib * (r2x * r2x + r2y * r2y - rn2 * rn2);
|
|
621
|
+
c.massNormal = kNormal > 0 ? 1 / kNormal : 0;
|
|
622
|
+
const tx = c.ny;
|
|
623
|
+
const ty = -c.nx;
|
|
624
|
+
const rt1 = r1x * tx + r1y * ty;
|
|
625
|
+
const rt2 = r2x * tx + r2y * ty;
|
|
626
|
+
const kTangent = ima + imb + iia * (r1x * r1x + r1y * r1y - rt1 * rt1) + iib * (r2x * r2x + r2y * r2y - rt2 * rt2);
|
|
627
|
+
c.massTangent = kTangent > 0 ? 1 / kTangent : 0;
|
|
628
|
+
c.bias = -BIAS_FACTOR * invDt * Math.min(0, c.separation + ALLOWED_PENETRATION);
|
|
629
|
+
const dvx = b.vx - b.w * r2y - a.vx + a.w * r1y;
|
|
630
|
+
const dvy = b.vy + b.w * r2x - a.vy - a.w * r1x;
|
|
631
|
+
const vn = dvx * c.nx + dvy * c.ny;
|
|
632
|
+
if (vn < -RESTITUTION_THRESHOLD) c.bias = Math.max(c.bias, -arbiter.restitution * vn);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
/** Re-apply last step's impulses so the solver starts close to the answer. */
|
|
636
|
+
warmStart(arbiter) {
|
|
637
|
+
const { a, b } = arbiter;
|
|
638
|
+
const ima = a.sleeping ? 0 : a.invMass;
|
|
639
|
+
const imb = b.sleeping ? 0 : b.invMass;
|
|
640
|
+
const iia = a.sleeping ? 0 : a.invInertia;
|
|
641
|
+
const iib = b.sleeping ? 0 : b.invInertia;
|
|
642
|
+
for (const c of arbiter.contacts) {
|
|
643
|
+
const r1x = c.x - a.x;
|
|
644
|
+
const r1y = c.y - a.y;
|
|
645
|
+
const r2x = c.x - b.x;
|
|
646
|
+
const r2y = c.y - b.y;
|
|
647
|
+
const tx = c.ny;
|
|
648
|
+
const ty = -c.nx;
|
|
649
|
+
const px = c.pn * c.nx + c.pt * tx;
|
|
650
|
+
const py = c.pn * c.ny + c.pt * ty;
|
|
651
|
+
a.vx -= ima * px;
|
|
652
|
+
a.vy -= ima * py;
|
|
653
|
+
a.w -= iia * (r1x * py - r1y * px);
|
|
654
|
+
b.vx += imb * px;
|
|
655
|
+
b.vy += imb * py;
|
|
656
|
+
b.w += iib * (r2x * py - r2y * px);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
applyImpulses(arbiter) {
|
|
660
|
+
const { a, b } = arbiter;
|
|
661
|
+
const ima = a.sleeping ? 0 : a.invMass;
|
|
662
|
+
const imb = b.sleeping ? 0 : b.invMass;
|
|
663
|
+
const iia = a.sleeping ? 0 : a.invInertia;
|
|
664
|
+
const iib = b.sleeping ? 0 : b.invInertia;
|
|
665
|
+
for (const c of arbiter.contacts) {
|
|
666
|
+
const r1x = c.x - a.x;
|
|
667
|
+
const r1y = c.y - a.y;
|
|
668
|
+
const r2x = c.x - b.x;
|
|
669
|
+
const r2y = c.y - b.y;
|
|
670
|
+
let dvx = b.vx - b.w * r2y - a.vx + a.w * r1y;
|
|
671
|
+
let dvy = b.vy + b.w * r2x - a.vy - a.w * r1x;
|
|
672
|
+
const vn = dvx * c.nx + dvy * c.ny;
|
|
673
|
+
let dPn = c.massNormal * (-vn + c.bias);
|
|
674
|
+
const pn0 = c.pn;
|
|
675
|
+
c.pn = Math.max(pn0 + dPn, 0);
|
|
676
|
+
dPn = c.pn - pn0;
|
|
677
|
+
let px = dPn * c.nx;
|
|
678
|
+
let py = dPn * c.ny;
|
|
679
|
+
a.vx -= ima * px;
|
|
680
|
+
a.vy -= ima * py;
|
|
681
|
+
a.w -= iia * (r1x * py - r1y * px);
|
|
682
|
+
b.vx += imb * px;
|
|
683
|
+
b.vy += imb * py;
|
|
684
|
+
b.w += iib * (r2x * py - r2y * px);
|
|
685
|
+
dvx = b.vx - b.w * r2y - a.vx + a.w * r1y;
|
|
686
|
+
dvy = b.vy + b.w * r2x - a.vy - a.w * r1x;
|
|
687
|
+
const tx = c.ny;
|
|
688
|
+
const ty = -c.nx;
|
|
689
|
+
const vt = dvx * tx + dvy * ty;
|
|
690
|
+
let dPt = c.massTangent * -vt;
|
|
691
|
+
const maxPt = arbiter.friction * c.pn;
|
|
692
|
+
const pt0 = c.pt;
|
|
693
|
+
c.pt = Math.max(-maxPt, Math.min(maxPt, pt0 + dPt));
|
|
694
|
+
dPt = c.pt - pt0;
|
|
695
|
+
px = dPt * tx;
|
|
696
|
+
py = dPt * ty;
|
|
697
|
+
a.vx -= ima * px;
|
|
698
|
+
a.vy -= ima * py;
|
|
699
|
+
a.w -= iia * (r1x * py - r1y * px);
|
|
700
|
+
b.vx += imb * px;
|
|
701
|
+
b.vy += imb * py;
|
|
702
|
+
b.w += iib * (r2x * py - r2y * px);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
// packages/gravity/src/gravity.ts
|
|
708
|
+
import { EffectRuntime, isBrowser, prefersReducedMotion, resolveElement } from "@elementalfx/core";
|
|
709
|
+
|
|
710
|
+
// packages/gravity/src/options.ts
|
|
711
|
+
var defaults = {
|
|
712
|
+
selector: null,
|
|
713
|
+
splitText: false,
|
|
714
|
+
gravity: 2e3,
|
|
715
|
+
bounce: 0.25,
|
|
716
|
+
friction: 0.5,
|
|
717
|
+
spin: 0.6,
|
|
718
|
+
walls: null,
|
|
719
|
+
onlyVisible: true,
|
|
720
|
+
maxBodies: 400,
|
|
721
|
+
interactive: true,
|
|
722
|
+
trigger: "manual",
|
|
723
|
+
restoreOnEscape: true,
|
|
724
|
+
restoreDuration: 0.9,
|
|
725
|
+
respectReducedMotion: true
|
|
726
|
+
};
|
|
727
|
+
function resolveOptions(options, base = defaults) {
|
|
728
|
+
const merged = { ...base };
|
|
729
|
+
for (const [key, value] of Object.entries(options)) if (value !== void 0) merged[key] = value;
|
|
730
|
+
if (typeof merged.trigger === "string") merged.trigger = parseTrigger(merged.trigger);
|
|
731
|
+
if (merged.splitText !== "words" && merged.splitText !== "letters") merged.splitText = false;
|
|
732
|
+
return merged;
|
|
733
|
+
}
|
|
734
|
+
function parseTrigger(value) {
|
|
735
|
+
const [kind = "manual", ...rest] = value.trim().split(/\s+/);
|
|
736
|
+
const argument = rest.join(" ");
|
|
737
|
+
if (kind === "idle") {
|
|
738
|
+
const seconds = Number(argument);
|
|
739
|
+
return { idle: Number.isFinite(seconds) && seconds > 0 ? seconds : 30 };
|
|
740
|
+
}
|
|
741
|
+
if (kind === "click" && argument) return { click: argument };
|
|
742
|
+
if (kind === "load" || kind === "konami") return kind;
|
|
743
|
+
return "manual";
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
// packages/gravity/src/pick.ts
|
|
747
|
+
var SKIP = /* @__PURE__ */ new Set([
|
|
748
|
+
"SCRIPT",
|
|
749
|
+
"STYLE",
|
|
750
|
+
"TEMPLATE",
|
|
751
|
+
"NOSCRIPT",
|
|
752
|
+
"HEAD",
|
|
753
|
+
"META",
|
|
754
|
+
"LINK",
|
|
755
|
+
"TITLE",
|
|
756
|
+
"BR",
|
|
757
|
+
"WBR",
|
|
758
|
+
"SOURCE",
|
|
759
|
+
"TRACK",
|
|
760
|
+
"PARAM",
|
|
761
|
+
"AREA",
|
|
762
|
+
"MAP",
|
|
763
|
+
"OPTION",
|
|
764
|
+
"OPTGROUP",
|
|
765
|
+
"DATALIST",
|
|
766
|
+
"DIALOG"
|
|
767
|
+
]);
|
|
768
|
+
var ATOMIC = /* @__PURE__ */ new Set([
|
|
769
|
+
"IMG",
|
|
770
|
+
"SVG",
|
|
771
|
+
"VIDEO",
|
|
772
|
+
"AUDIO",
|
|
773
|
+
"CANVAS",
|
|
774
|
+
"PICTURE",
|
|
775
|
+
"IFRAME",
|
|
776
|
+
"OBJECT",
|
|
777
|
+
"EMBED",
|
|
778
|
+
"BUTTON",
|
|
779
|
+
"INPUT",
|
|
780
|
+
"SELECT",
|
|
781
|
+
"TEXTAREA",
|
|
782
|
+
"HR",
|
|
783
|
+
"METER",
|
|
784
|
+
"PROGRESS",
|
|
785
|
+
"MATH"
|
|
786
|
+
]);
|
|
787
|
+
var ATOMIC_SELECTOR = "img,svg,video,audio,canvas,picture,iframe,object,embed,button,input,select,textarea,hr,meter,progress,math";
|
|
788
|
+
var tag = (element) => element.tagName.toUpperCase();
|
|
789
|
+
var marked = (element, value) => element.getAttribute("data-gravity") === value;
|
|
790
|
+
function hasDirectText(element) {
|
|
791
|
+
for (const node of element.childNodes) {
|
|
792
|
+
if (node.nodeType === 3 && /\S/.test(node.data)) return true;
|
|
793
|
+
}
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
796
|
+
function paints(style) {
|
|
797
|
+
return style.backgroundColor !== "rgba(0, 0, 0, 0)" && style.backgroundColor !== "transparent" || style.backgroundImage !== "none" || parseFloat(style.borderTopWidth) + parseFloat(style.borderLeftWidth) > 0 || style.boxShadow !== "none";
|
|
798
|
+
}
|
|
799
|
+
function pickElements(root, selector, onlyVisible, max) {
|
|
800
|
+
const viewportW = document.documentElement.clientWidth;
|
|
801
|
+
const viewportH = document.documentElement.clientHeight;
|
|
802
|
+
const big = viewportW * viewportH * 0.6;
|
|
803
|
+
const picked = [];
|
|
804
|
+
const visible = (rect) => !onlyVisible || rect.bottom > 0 && rect.right > 0 && rect.top < viewportH && rect.left < viewportW;
|
|
805
|
+
if (selector) {
|
|
806
|
+
const candidates = [...root.querySelectorAll(selector)].filter((element) => {
|
|
807
|
+
if (element.closest('[data-gravity="ignore"]') || element.closest("[data-fx]")) return false;
|
|
808
|
+
const rect = element.getBoundingClientRect();
|
|
809
|
+
return rect.width >= 1 && rect.height >= 1 && visible(rect);
|
|
810
|
+
});
|
|
811
|
+
const inner = candidates.filter((element) => !candidates.some((other) => other !== element && element.contains(other)));
|
|
812
|
+
return inner.slice(0, max);
|
|
813
|
+
}
|
|
814
|
+
const walk = (element) => {
|
|
815
|
+
if (picked.length >= max) return;
|
|
816
|
+
if (SKIP.has(tag(element)) || marked(element, "ignore") || element.hasAttribute("data-fx")) return;
|
|
817
|
+
const style = getComputedStyle(element);
|
|
818
|
+
if (style.display === "none") return;
|
|
819
|
+
const children = () => {
|
|
820
|
+
for (const child of element.children) walk(child);
|
|
821
|
+
};
|
|
822
|
+
if (style.display === "contents") return children();
|
|
823
|
+
const rect = element.getBoundingClientRect();
|
|
824
|
+
if (rect.width < 1 || rect.height < 1 || style.visibility !== "visible") return children();
|
|
825
|
+
if (!visible(rect)) return;
|
|
826
|
+
if (marked(element, "body") || ATOMIC.has(tag(element))) {
|
|
827
|
+
picked.push(element);
|
|
828
|
+
return;
|
|
829
|
+
}
|
|
830
|
+
if (rect.width < viewportW * 0.6 && rect.width * rect.height < big * 0.4 && paints(style) && element.textContent?.trim()) {
|
|
831
|
+
picked.push(element);
|
|
832
|
+
return;
|
|
833
|
+
}
|
|
834
|
+
if (hasDirectText(element)) {
|
|
835
|
+
if (rect.width * rect.height > big && element.children.length > 0) return children();
|
|
836
|
+
picked.push(element);
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (element.children.length === 0) {
|
|
840
|
+
if (paints(style)) picked.push(element);
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
children();
|
|
844
|
+
};
|
|
845
|
+
for (const child of root.children) walk(child);
|
|
846
|
+
return picked;
|
|
847
|
+
}
|
|
848
|
+
var segmenter = typeof Intl !== "undefined" && "Segmenter" in Intl ? new Intl.Segmenter(void 0, { granularity: "grapheme" }) : null;
|
|
849
|
+
function graphemes(text) {
|
|
850
|
+
return segmenter ? Array.from(segmenter.segment(text), (part) => part.segment) : Array.from(text);
|
|
851
|
+
}
|
|
852
|
+
function piece(text, nowrap = false) {
|
|
853
|
+
const span = document.createElement("span");
|
|
854
|
+
span.setAttribute("data-gravity-piece", "");
|
|
855
|
+
span.style.display = "inline-block";
|
|
856
|
+
if (nowrap) span.style.whiteSpace = "nowrap";
|
|
857
|
+
span.textContent = text;
|
|
858
|
+
return span;
|
|
859
|
+
}
|
|
860
|
+
function splitText(elements, mode, max) {
|
|
861
|
+
const replaced = [];
|
|
862
|
+
const bodies = [];
|
|
863
|
+
for (const element of elements) {
|
|
864
|
+
if (bodies.length >= max) break;
|
|
865
|
+
if (ATOMIC.has(tag(element)) || marked(element, "body")) {
|
|
866
|
+
bodies.push(element);
|
|
867
|
+
continue;
|
|
868
|
+
}
|
|
869
|
+
const texts = [];
|
|
870
|
+
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, {
|
|
871
|
+
acceptNode(node) {
|
|
872
|
+
if (!/\S/.test(node.data)) return NodeFilter.FILTER_REJECT;
|
|
873
|
+
for (let parent = node.parentElement; parent && parent !== element; parent = parent.parentElement) {
|
|
874
|
+
if (SKIP.has(tag(parent)) || ATOMIC.has(tag(parent)) || marked(parent, "ignore") || marked(parent, "body")) {
|
|
875
|
+
return NodeFilter.FILTER_REJECT;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return NodeFilter.FILTER_ACCEPT;
|
|
879
|
+
}
|
|
880
|
+
});
|
|
881
|
+
for (let node = walker.nextNode(); node; node = walker.nextNode()) texts.push(node);
|
|
882
|
+
const before = bodies.length;
|
|
883
|
+
for (const text of texts) {
|
|
884
|
+
const nodes = [];
|
|
885
|
+
for (const token of text.data.split(/(\s+)/)) {
|
|
886
|
+
if (!token) continue;
|
|
887
|
+
if (/^\s+$/.test(token) || bodies.length >= max) {
|
|
888
|
+
nodes.push(document.createTextNode(token));
|
|
889
|
+
} else if (mode === "words") {
|
|
890
|
+
const span = piece(token);
|
|
891
|
+
nodes.push(span);
|
|
892
|
+
bodies.push(span);
|
|
893
|
+
} else {
|
|
894
|
+
const word = document.createElement("span");
|
|
895
|
+
word.style.display = "inline-block";
|
|
896
|
+
word.style.whiteSpace = "nowrap";
|
|
897
|
+
for (const letter of graphemes(token)) {
|
|
898
|
+
const span = piece(letter);
|
|
899
|
+
word.append(span);
|
|
900
|
+
bodies.push(span);
|
|
901
|
+
}
|
|
902
|
+
nodes.push(word);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
text.replaceWith(...nodes);
|
|
906
|
+
replaced.push({ text, nodes });
|
|
907
|
+
}
|
|
908
|
+
for (const inner of element.querySelectorAll(ATOMIC_SELECTOR)) {
|
|
909
|
+
const outer = inner.parentElement?.closest(ATOMIC_SELECTOR);
|
|
910
|
+
if ((!outer || !element.contains(outer)) && !inner.closest('[data-gravity="ignore"]')) bodies.push(inner);
|
|
911
|
+
}
|
|
912
|
+
if (bodies.length === before) bodies.push(element);
|
|
913
|
+
}
|
|
914
|
+
return {
|
|
915
|
+
bodies: bodies.slice(0, max),
|
|
916
|
+
undo() {
|
|
917
|
+
for (const { text, nodes } of replaced.reverse()) {
|
|
918
|
+
const first = nodes[0];
|
|
919
|
+
if (first?.parentNode) first.parentNode.insertBefore(text, first);
|
|
920
|
+
for (const node of nodes) node.parentNode?.removeChild(node);
|
|
921
|
+
}
|
|
922
|
+
replaced.length = 0;
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
// packages/gravity/src/gravity.ts
|
|
928
|
+
var WALL = 1e3;
|
|
929
|
+
var MAX_RESTLESS = 15;
|
|
930
|
+
var INSET = 1.5;
|
|
931
|
+
function createGravity(target = "body", options = {}) {
|
|
932
|
+
let element = isBrowser ? resolveElement(target) : target;
|
|
933
|
+
if (isBrowser && element === document.documentElement) element = document.body;
|
|
934
|
+
return new GravityEffect(element, options);
|
|
935
|
+
}
|
|
936
|
+
var GravityEffect = class {
|
|
937
|
+
constructor(element, options) {
|
|
938
|
+
this.element = element;
|
|
939
|
+
this.state = "idle";
|
|
940
|
+
this.world = null;
|
|
941
|
+
this.items = [];
|
|
942
|
+
this.walls = [];
|
|
943
|
+
this.split = null;
|
|
944
|
+
this.runtime = null;
|
|
945
|
+
this.scrollX = 0;
|
|
946
|
+
this.scrollY = 0;
|
|
947
|
+
this.restoreTime = 0;
|
|
948
|
+
this.restless = 0;
|
|
949
|
+
this.settleWaiters = [];
|
|
950
|
+
this.restoreWaiters = [];
|
|
951
|
+
this.cleanups = [];
|
|
952
|
+
this.destroyed = false;
|
|
953
|
+
this.byElement = /* @__PURE__ */ new WeakMap();
|
|
954
|
+
this.drag = null;
|
|
955
|
+
this.disarm = () => {
|
|
956
|
+
};
|
|
957
|
+
this.options = resolveOptions(options);
|
|
958
|
+
if (!isBrowser) return;
|
|
959
|
+
this.runtime = new EffectRuntime({
|
|
960
|
+
element: this.element,
|
|
961
|
+
frame: (dt) => this.frame(dt),
|
|
962
|
+
pauseOffscreen: false,
|
|
963
|
+
respectReducedMotion: this.options.respectReducedMotion,
|
|
964
|
+
onReducedMotionChange: (reduce) => {
|
|
965
|
+
if (reduce) this.reset();
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
this.runtime.sleep();
|
|
969
|
+
this.listen(window, "resize", () => this.fitWalls());
|
|
970
|
+
this.listen(window, "scroll", () => this.renderFixed(), { passive: true });
|
|
971
|
+
this.listen(this.element, "pointerdown", (event) => this.grabAt(event));
|
|
972
|
+
this.listen(window, "pointermove", (event) => this.dragTo(event));
|
|
973
|
+
this.listen(window, "pointerup", (event) => this.letGo(event));
|
|
974
|
+
this.listen(window, "pointercancel", (event) => this.letGo(event));
|
|
975
|
+
this.listen(this.element, "dragstart", (event) => {
|
|
976
|
+
if (this.state !== "idle") event.preventDefault();
|
|
977
|
+
});
|
|
978
|
+
this.listen(window, "keydown", (event) => {
|
|
979
|
+
if (event.key === "Escape" && this.options.restoreOnEscape && this.state !== "idle") void this.restore();
|
|
980
|
+
});
|
|
981
|
+
this.armTrigger();
|
|
982
|
+
}
|
|
983
|
+
get bodies() {
|
|
984
|
+
return this.items.length;
|
|
985
|
+
}
|
|
986
|
+
drop() {
|
|
987
|
+
if (!isBrowser || this.destroyed || this.reduced()) return Promise.resolve();
|
|
988
|
+
if (this.state === "restoring") this.resumeFromRestore();
|
|
989
|
+
else if (this.state === "idle") this.start();
|
|
990
|
+
const settled = new Promise((resolve) => this.settleWaiters.push(resolve));
|
|
991
|
+
this.wakeUp();
|
|
992
|
+
return settled;
|
|
993
|
+
}
|
|
994
|
+
restore() {
|
|
995
|
+
if (this.state === "idle") return Promise.resolve();
|
|
996
|
+
const done = new Promise((resolve) => this.restoreWaiters.push(resolve));
|
|
997
|
+
if (this.state === "falling") {
|
|
998
|
+
if (this.reduced() || this.options.restoreDuration <= 0) {
|
|
999
|
+
this.reset();
|
|
1000
|
+
return done;
|
|
1001
|
+
}
|
|
1002
|
+
this.letGo(null);
|
|
1003
|
+
this.state = "restoring";
|
|
1004
|
+
this.restoreTime = 0;
|
|
1005
|
+
for (const item of this.items) {
|
|
1006
|
+
const [dx, dy] = this.offset(item);
|
|
1007
|
+
const angle = Math.atan2(Math.sin(item.body.angle), Math.cos(item.body.angle));
|
|
1008
|
+
item.from = { x: dx, y: dy, angle };
|
|
1009
|
+
}
|
|
1010
|
+
this.settle();
|
|
1011
|
+
this.wakeUp();
|
|
1012
|
+
}
|
|
1013
|
+
return done;
|
|
1014
|
+
}
|
|
1015
|
+
toggle() {
|
|
1016
|
+
return this.state === "falling" ? this.restore() : this.drop();
|
|
1017
|
+
}
|
|
1018
|
+
explode(at, strength = 1) {
|
|
1019
|
+
const settled = this.drop();
|
|
1020
|
+
if (this.state !== "falling") return settled;
|
|
1021
|
+
let x = at?.x;
|
|
1022
|
+
let y = at?.y;
|
|
1023
|
+
if (x === void 0 || y === void 0) {
|
|
1024
|
+
const rect = this.element.getBoundingClientRect();
|
|
1025
|
+
const top = Math.max(0, rect.top);
|
|
1026
|
+
const bottom = Math.min(document.documentElement.clientHeight, rect.bottom);
|
|
1027
|
+
x = (Math.max(0, rect.left) + Math.min(document.documentElement.clientWidth, rect.right)) / 2;
|
|
1028
|
+
y = bottom > top ? (top + bottom) / 2 : rect.top + rect.height / 2;
|
|
1029
|
+
}
|
|
1030
|
+
x += window.scrollX;
|
|
1031
|
+
y += window.scrollY;
|
|
1032
|
+
for (const { body } of this.items) {
|
|
1033
|
+
const dx = body.x - x;
|
|
1034
|
+
const dy = body.y - y;
|
|
1035
|
+
const distance = Math.hypot(dx, dy) || 1;
|
|
1036
|
+
const speed = strength * 2400 / (1 + distance / 400);
|
|
1037
|
+
body.vx += dx / distance * speed;
|
|
1038
|
+
body.vy += dy / distance * speed - strength * 300;
|
|
1039
|
+
body.w += (Math.random() * 2 - 1) * 8 * strength;
|
|
1040
|
+
body.wake();
|
|
1041
|
+
}
|
|
1042
|
+
this.wakeUp();
|
|
1043
|
+
return settled;
|
|
1044
|
+
}
|
|
1045
|
+
set(options) {
|
|
1046
|
+
const trigger = this.options.trigger;
|
|
1047
|
+
this.options = resolveOptions(options, this.options);
|
|
1048
|
+
this.runtime?.setRespectReducedMotion(this.options.respectReducedMotion);
|
|
1049
|
+
if (isBrowser && this.options.trigger !== trigger) this.armTrigger();
|
|
1050
|
+
if (!this.world) return;
|
|
1051
|
+
this.world.gravity = this.options.gravity;
|
|
1052
|
+
for (const { body } of this.items) {
|
|
1053
|
+
body.restitution = this.options.bounce;
|
|
1054
|
+
body.friction = this.options.friction;
|
|
1055
|
+
body.wake();
|
|
1056
|
+
}
|
|
1057
|
+
this.wakeUp();
|
|
1058
|
+
}
|
|
1059
|
+
destroy() {
|
|
1060
|
+
if (this.destroyed) return;
|
|
1061
|
+
this.destroyed = true;
|
|
1062
|
+
this.reset();
|
|
1063
|
+
this.disarm();
|
|
1064
|
+
for (const cleanup of this.cleanups) cleanup();
|
|
1065
|
+
this.cleanups = [];
|
|
1066
|
+
this.runtime?.destroy();
|
|
1067
|
+
}
|
|
1068
|
+
/** Sets up whatever makes the page fall by itself: load, the Konami code, idleness or a click. */
|
|
1069
|
+
armTrigger() {
|
|
1070
|
+
this.disarm();
|
|
1071
|
+
const stops = [];
|
|
1072
|
+
this.disarm = () => {
|
|
1073
|
+
for (const stop of stops) stop();
|
|
1074
|
+
this.disarm = () => {
|
|
1075
|
+
};
|
|
1076
|
+
};
|
|
1077
|
+
const on = (target, type, handler) => {
|
|
1078
|
+
const listener = handler;
|
|
1079
|
+
target.addEventListener(type, listener, { passive: true });
|
|
1080
|
+
stops.push(() => target.removeEventListener(type, listener));
|
|
1081
|
+
};
|
|
1082
|
+
const trigger = this.options.trigger;
|
|
1083
|
+
if (trigger === "load") {
|
|
1084
|
+
const go = () => {
|
|
1085
|
+
const frame = requestAnimationFrame(() => void this.drop());
|
|
1086
|
+
stops.push(() => cancelAnimationFrame(frame));
|
|
1087
|
+
};
|
|
1088
|
+
if (document.readyState === "complete") go();
|
|
1089
|
+
else on(window, "load", go);
|
|
1090
|
+
} else if (trigger === "konami") {
|
|
1091
|
+
const code = ["arrowup", "arrowup", "arrowdown", "arrowdown", "arrowleft", "arrowright", "arrowleft", "arrowright", "b", "a"];
|
|
1092
|
+
let progress = 0;
|
|
1093
|
+
on(window, "keydown", (event) => {
|
|
1094
|
+
const key = event.key.toLowerCase();
|
|
1095
|
+
progress = key === code[progress] ? progress + 1 : key === code[0] ? 1 : 0;
|
|
1096
|
+
if (progress === code.length) {
|
|
1097
|
+
progress = 0;
|
|
1098
|
+
void this.toggle();
|
|
1099
|
+
}
|
|
1100
|
+
});
|
|
1101
|
+
} else if (typeof trigger === "object" && "idle" in trigger) {
|
|
1102
|
+
let timer = 0;
|
|
1103
|
+
const restart = () => {
|
|
1104
|
+
clearTimeout(timer);
|
|
1105
|
+
if (this.state === "idle") timer = window.setTimeout(() => void this.drop(), trigger.idle * 1e3);
|
|
1106
|
+
};
|
|
1107
|
+
for (const type of ["pointermove", "pointerdown", "keydown", "wheel", "scroll", "touchstart"]) on(window, type, restart);
|
|
1108
|
+
stops.push(() => clearTimeout(timer));
|
|
1109
|
+
restart();
|
|
1110
|
+
} else if (typeof trigger === "object" && "click" in trigger) {
|
|
1111
|
+
on(document, "click", (event) => {
|
|
1112
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
1113
|
+
if (!target || event.defaultPrevented) return;
|
|
1114
|
+
const hit = typeof trigger.click === "string" ? target.closest(trigger.click) : trigger.click.contains(target);
|
|
1115
|
+
if (hit) void this.toggle();
|
|
1116
|
+
});
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
reduced() {
|
|
1120
|
+
return this.options.respectReducedMotion && prefersReducedMotion();
|
|
1121
|
+
}
|
|
1122
|
+
listen(target, type, handler, options) {
|
|
1123
|
+
const listener = handler;
|
|
1124
|
+
target.addEventListener(type, listener, options);
|
|
1125
|
+
this.cleanups.push(() => target.removeEventListener(type, listener, options));
|
|
1126
|
+
}
|
|
1127
|
+
/** Measures the elements that will fall and builds the physics world around them. */
|
|
1128
|
+
start() {
|
|
1129
|
+
const { options } = this;
|
|
1130
|
+
let elements = pickElements(this.element, options.selector, options.onlyVisible, options.maxBodies);
|
|
1131
|
+
if (options.splitText) {
|
|
1132
|
+
this.split = splitText(elements, options.splitText, options.maxBodies);
|
|
1133
|
+
elements = this.split.bodies;
|
|
1134
|
+
}
|
|
1135
|
+
const targets = elements;
|
|
1136
|
+
const inline = targets.map((el) => getComputedStyle(el).display === "inline");
|
|
1137
|
+
const items = targets.map((el, i) => {
|
|
1138
|
+
const item = { element: el, body: null, x0: 0, y0: 0, fixed: false, base: "", saved: /* @__PURE__ */ new Map(), original: null };
|
|
1139
|
+
this.style(item, "transition", "none");
|
|
1140
|
+
if (inline[i]) this.style(item, "display", "inline-block");
|
|
1141
|
+
return item;
|
|
1142
|
+
});
|
|
1143
|
+
this.scrollX = window.scrollX;
|
|
1144
|
+
this.scrollY = window.scrollY;
|
|
1145
|
+
const fixedCache = /* @__PURE__ */ new Map();
|
|
1146
|
+
const world = new World({ gravity: options.gravity });
|
|
1147
|
+
this.world = world;
|
|
1148
|
+
this.fitWalls();
|
|
1149
|
+
const room = this.room();
|
|
1150
|
+
for (const item of items) {
|
|
1151
|
+
const el = item.element;
|
|
1152
|
+
const rect = el.getBoundingClientRect();
|
|
1153
|
+
const style = getComputedStyle(el);
|
|
1154
|
+
item.fixed = insideFixed(el, fixedCache);
|
|
1155
|
+
item.base = style.transform === "none" ? "" : ` ${style.transform}`;
|
|
1156
|
+
item.x0 = rect.left + rect.width / 2 + this.scrollX;
|
|
1157
|
+
item.y0 = rect.top + rect.height / 2 + this.scrollY;
|
|
1158
|
+
const html = el;
|
|
1159
|
+
const width = Math.min(html.offsetWidth || rect.width, room.width - 4);
|
|
1160
|
+
const height = html.offsetHeight || rect.height;
|
|
1161
|
+
const radius = parseFloat(style.borderTopLeftRadius);
|
|
1162
|
+
const round = Math.abs(width - height) < 2 && radius >= width * 0.45;
|
|
1163
|
+
const body = new Body({
|
|
1164
|
+
x: item.x0,
|
|
1165
|
+
y: item.y0,
|
|
1166
|
+
width: Math.max(2, width),
|
|
1167
|
+
height: Math.max(2, height),
|
|
1168
|
+
shape: round ? "circle" : "box",
|
|
1169
|
+
friction: options.friction,
|
|
1170
|
+
restitution: options.bounce
|
|
1171
|
+
});
|
|
1172
|
+
body.w = (Math.random() * 2 - 1) * options.spin;
|
|
1173
|
+
body.data = item;
|
|
1174
|
+
item.body = body;
|
|
1175
|
+
world.add(body);
|
|
1176
|
+
}
|
|
1177
|
+
for (const item of items) {
|
|
1178
|
+
this.style(item, "transform-origin", "50% 50%");
|
|
1179
|
+
this.style(item, "transform", item.base.trim() || "none");
|
|
1180
|
+
}
|
|
1181
|
+
if (options.interactive) {
|
|
1182
|
+
for (const item of items) {
|
|
1183
|
+
this.style(item, "touch-action", "none");
|
|
1184
|
+
this.style(item, ["user-select", "-webkit-user-select"], "none");
|
|
1185
|
+
this.style(item, "cursor", "grab");
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
for (const item of items) this.byElement.set(item.element, item);
|
|
1189
|
+
this.items = items;
|
|
1190
|
+
this.state = "falling";
|
|
1191
|
+
}
|
|
1192
|
+
/** The fallen element under a pointer, if any. */
|
|
1193
|
+
itemAt(target) {
|
|
1194
|
+
for (let node = target instanceof Element ? target : null; node && node !== this.element.parentElement; node = node.parentElement) {
|
|
1195
|
+
const item = this.byElement.get(node);
|
|
1196
|
+
if (item) return item;
|
|
1197
|
+
}
|
|
1198
|
+
return null;
|
|
1199
|
+
}
|
|
1200
|
+
grabAt(event) {
|
|
1201
|
+
if (this.state !== "falling" || !this.options.interactive || !this.world || this.drag) return;
|
|
1202
|
+
if (event.pointerType === "mouse" && event.button !== 0) return;
|
|
1203
|
+
const item = this.itemAt(event.target);
|
|
1204
|
+
if (!item) return;
|
|
1205
|
+
event.preventDefault();
|
|
1206
|
+
const x = event.clientX + window.scrollX;
|
|
1207
|
+
const y = event.clientY + window.scrollY;
|
|
1208
|
+
const grab = this.world.grab(item.body, x, y);
|
|
1209
|
+
this.drag = { grab, pointer: event.pointerId, x: event.clientX, y: event.clientY, moved: false };
|
|
1210
|
+
if (getComputedStyle(item.element).position === "static") this.style(item, "position", "relative");
|
|
1211
|
+
this.style(item, "z-index", "2147483000");
|
|
1212
|
+
this.style(item, "cursor", "grabbing");
|
|
1213
|
+
this.wakeUp();
|
|
1214
|
+
}
|
|
1215
|
+
dragTo(event) {
|
|
1216
|
+
const drag = this.drag;
|
|
1217
|
+
if (!drag || event.pointerId !== drag.pointer) return;
|
|
1218
|
+
if (Math.abs(event.clientX - drag.x) + Math.abs(event.clientY - drag.y) > 4) drag.moved = true;
|
|
1219
|
+
drag.grab.moveTo(event.clientX + window.scrollX, event.clientY + window.scrollY);
|
|
1220
|
+
this.wakeUp();
|
|
1221
|
+
}
|
|
1222
|
+
letGo(event) {
|
|
1223
|
+
const drag = this.drag;
|
|
1224
|
+
if (!drag || event && event.pointerId !== drag.pointer) return;
|
|
1225
|
+
this.drag = null;
|
|
1226
|
+
this.world?.release(drag.grab);
|
|
1227
|
+
this.wakeUp();
|
|
1228
|
+
const item = drag.grab.body.data;
|
|
1229
|
+
if (this.items.includes(item)) this.style(item, "cursor", "grab");
|
|
1230
|
+
if (drag.moved) {
|
|
1231
|
+
const swallow = (click) => {
|
|
1232
|
+
click.preventDefault();
|
|
1233
|
+
click.stopPropagation();
|
|
1234
|
+
};
|
|
1235
|
+
window.addEventListener("click", swallow, { capture: true, once: true });
|
|
1236
|
+
setTimeout(() => window.removeEventListener("click", swallow, { capture: true }), 0);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
/** The area elements fall within, in document px. */
|
|
1240
|
+
room() {
|
|
1241
|
+
const walls = this.options.walls ?? (this.element === document.body ? "viewport" : "root");
|
|
1242
|
+
let left;
|
|
1243
|
+
let right;
|
|
1244
|
+
let bottom;
|
|
1245
|
+
if (walls === "viewport") {
|
|
1246
|
+
left = this.scrollX;
|
|
1247
|
+
right = left + document.documentElement.clientWidth;
|
|
1248
|
+
bottom = this.scrollY + document.documentElement.clientHeight;
|
|
1249
|
+
} else {
|
|
1250
|
+
const rect = this.element.getBoundingClientRect();
|
|
1251
|
+
left = rect.left + window.scrollX;
|
|
1252
|
+
right = rect.right + window.scrollX;
|
|
1253
|
+
bottom = rect.bottom + window.scrollY;
|
|
1254
|
+
}
|
|
1255
|
+
return { left, right, bottom, width: right - left };
|
|
1256
|
+
}
|
|
1257
|
+
/** Places the floor and side walls, e.g. after the window is resized. */
|
|
1258
|
+
fitWalls() {
|
|
1259
|
+
const world = this.world;
|
|
1260
|
+
if (!world || this.state === "restoring") return;
|
|
1261
|
+
for (const wall of this.walls) world.remove(wall);
|
|
1262
|
+
let { left, right, bottom, width } = this.room();
|
|
1263
|
+
left += INSET;
|
|
1264
|
+
right -= INSET;
|
|
1265
|
+
bottom -= INSET;
|
|
1266
|
+
width -= INSET * 2;
|
|
1267
|
+
const tall = 2e4;
|
|
1268
|
+
this.walls = [
|
|
1269
|
+
new Body({ x: (left + right) / 2, y: bottom + WALL / 2, width: width + WALL * 2, height: WALL, density: 0 }),
|
|
1270
|
+
new Body({ x: left - WALL / 2, y: bottom - tall / 2, width: WALL, height: tall, density: 0 }),
|
|
1271
|
+
new Body({ x: right + WALL / 2, y: bottom - tall / 2, width: WALL, height: tall, density: 0 })
|
|
1272
|
+
];
|
|
1273
|
+
for (const wall of this.walls) world.add(wall);
|
|
1274
|
+
for (const { body } of this.items) body.wake();
|
|
1275
|
+
this.wakeUp();
|
|
1276
|
+
}
|
|
1277
|
+
wakeUp() {
|
|
1278
|
+
this.restless = 0;
|
|
1279
|
+
this.runtime?.wake();
|
|
1280
|
+
}
|
|
1281
|
+
frame(dt) {
|
|
1282
|
+
if (this.state === "falling" && this.world) {
|
|
1283
|
+
this.world.step(dt);
|
|
1284
|
+
this.restless += dt;
|
|
1285
|
+
if (this.restless > MAX_RESTLESS && !this.drag) this.world.sleepAll();
|
|
1286
|
+
this.render();
|
|
1287
|
+
if (this.world.settled && this.canSleep()) {
|
|
1288
|
+
this.settle();
|
|
1289
|
+
this.runtime?.sleep();
|
|
1290
|
+
}
|
|
1291
|
+
} else if (this.state === "restoring") {
|
|
1292
|
+
this.restoreTime += dt / this.options.restoreDuration;
|
|
1293
|
+
const t = Math.min(1, this.restoreTime);
|
|
1294
|
+
const k = 1 - easeInOut(t);
|
|
1295
|
+
for (const item of this.items) {
|
|
1296
|
+
const from = item.from;
|
|
1297
|
+
this.place(item, from.x * k, from.y * k, from.angle * k);
|
|
1298
|
+
}
|
|
1299
|
+
if (t >= 1) this.reset();
|
|
1300
|
+
} else {
|
|
1301
|
+
this.runtime?.sleep();
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
/** Whether nothing outside the physics (a drag, say) needs frames. */
|
|
1305
|
+
canSleep() {
|
|
1306
|
+
return this.drag === null;
|
|
1307
|
+
}
|
|
1308
|
+
offset(item) {
|
|
1309
|
+
let dx = item.body.x - item.x0;
|
|
1310
|
+
let dy = item.body.y - item.y0;
|
|
1311
|
+
if (item.fixed) {
|
|
1312
|
+
dx -= window.scrollX - this.scrollX;
|
|
1313
|
+
dy -= window.scrollY - this.scrollY;
|
|
1314
|
+
}
|
|
1315
|
+
return [dx, dy];
|
|
1316
|
+
}
|
|
1317
|
+
render() {
|
|
1318
|
+
for (const item of this.items) {
|
|
1319
|
+
if (item.body.sleeping) continue;
|
|
1320
|
+
const [dx, dy] = this.offset(item);
|
|
1321
|
+
this.place(item, dx, dy, item.body.angle);
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
renderFixed() {
|
|
1325
|
+
if (this.state !== "falling") return;
|
|
1326
|
+
for (const item of this.items) {
|
|
1327
|
+
if (!item.fixed) continue;
|
|
1328
|
+
const [dx, dy] = this.offset(item);
|
|
1329
|
+
this.place(item, dx, dy, item.body.angle);
|
|
1330
|
+
}
|
|
1331
|
+
}
|
|
1332
|
+
place(item, dx, dy, angle) {
|
|
1333
|
+
const transform = `translate(${dx.toFixed(2)}px, ${dy.toFixed(2)}px) rotate(${angle.toFixed(4)}rad)${item.base}`;
|
|
1334
|
+
item.element.style.setProperty("transform", transform, "important");
|
|
1335
|
+
}
|
|
1336
|
+
/** Sets an inline style, remembering the original value to put back later. */
|
|
1337
|
+
style(item, properties, value) {
|
|
1338
|
+
const { style } = item.element;
|
|
1339
|
+
const list = typeof properties === "string" ? [properties] : properties;
|
|
1340
|
+
item.original ?? (item.original = { attribute: item.element.getAttribute("style"), css: style.cssText });
|
|
1341
|
+
for (const property of list) {
|
|
1342
|
+
if (!item.saved.has(property)) item.saved.set(property, [style.getPropertyValue(property), style.getPropertyPriority(property)]);
|
|
1343
|
+
}
|
|
1344
|
+
for (const property of list) style.setProperty(property, value, "important");
|
|
1345
|
+
}
|
|
1346
|
+
/** Physics positions take over from wherever the trip back had got to. */
|
|
1347
|
+
resumeFromRestore() {
|
|
1348
|
+
const k = 1 - easeInOut(Math.min(1, this.restoreTime));
|
|
1349
|
+
for (const item of this.items) {
|
|
1350
|
+
const from = item.from;
|
|
1351
|
+
const { body } = item;
|
|
1352
|
+
body.x = item.x0 + from.x * k + (item.fixed ? window.scrollX - this.scrollX : 0);
|
|
1353
|
+
body.y = item.y0 + from.y * k + (item.fixed ? window.scrollY - this.scrollY : 0);
|
|
1354
|
+
body.angle = from.angle * k;
|
|
1355
|
+
body.vx = body.vy = 0;
|
|
1356
|
+
body.w = (Math.random() * 2 - 1) * this.options.spin;
|
|
1357
|
+
body.wake();
|
|
1358
|
+
delete item.from;
|
|
1359
|
+
}
|
|
1360
|
+
this.state = "falling";
|
|
1361
|
+
this.resolveRestore();
|
|
1362
|
+
}
|
|
1363
|
+
/** Puts every element back exactly as it was. */
|
|
1364
|
+
reset() {
|
|
1365
|
+
this.letGo(null);
|
|
1366
|
+
for (const item of this.items) {
|
|
1367
|
+
const { style } = item.element;
|
|
1368
|
+
for (const [property, [value, priority]] of [...item.saved].reverse()) {
|
|
1369
|
+
if (value) style.setProperty(property, value, priority);
|
|
1370
|
+
else style.removeProperty(property);
|
|
1371
|
+
}
|
|
1372
|
+
const { original } = item;
|
|
1373
|
+
if (original && style.cssText === original.css) {
|
|
1374
|
+
item.element.getAttribute("style");
|
|
1375
|
+
if (original.attribute === null) item.element.removeAttribute("style");
|
|
1376
|
+
else item.element.setAttribute("style", original.attribute);
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
this.split?.undo();
|
|
1380
|
+
this.split = null;
|
|
1381
|
+
this.byElement = /* @__PURE__ */ new WeakMap();
|
|
1382
|
+
this.items = [];
|
|
1383
|
+
this.walls = [];
|
|
1384
|
+
this.world = null;
|
|
1385
|
+
this.state = "idle";
|
|
1386
|
+
this.settle();
|
|
1387
|
+
this.resolveRestore();
|
|
1388
|
+
this.runtime?.sleep();
|
|
1389
|
+
}
|
|
1390
|
+
settle() {
|
|
1391
|
+
const waiters = this.settleWaiters;
|
|
1392
|
+
this.settleWaiters = [];
|
|
1393
|
+
for (const resolve of waiters) resolve();
|
|
1394
|
+
}
|
|
1395
|
+
resolveRestore() {
|
|
1396
|
+
const waiters = this.restoreWaiters;
|
|
1397
|
+
this.restoreWaiters = [];
|
|
1398
|
+
for (const resolve of waiters) resolve();
|
|
1399
|
+
}
|
|
1400
|
+
};
|
|
1401
|
+
function insideFixed(element, cache) {
|
|
1402
|
+
const chain = [];
|
|
1403
|
+
let result = false;
|
|
1404
|
+
for (let node = element; node; node = node.parentElement) {
|
|
1405
|
+
const known = cache.get(node);
|
|
1406
|
+
if (known !== void 0) {
|
|
1407
|
+
result = known;
|
|
1408
|
+
break;
|
|
1409
|
+
}
|
|
1410
|
+
chain.push(node);
|
|
1411
|
+
if (getComputedStyle(node).position === "fixed") {
|
|
1412
|
+
result = true;
|
|
1413
|
+
break;
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
for (const node of chain) cache.set(node, result);
|
|
1417
|
+
return result;
|
|
1418
|
+
}
|
|
1419
|
+
function easeInOut(t) {
|
|
1420
|
+
return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
export {
|
|
1424
|
+
Body,
|
|
1425
|
+
collide,
|
|
1426
|
+
Grab,
|
|
1427
|
+
World,
|
|
1428
|
+
createGravity
|
|
1429
|
+
};
|
|
1430
|
+
//# sourceMappingURL=chunk-VP6BXQKB.js.map
|