@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 aviyer42
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,126 @@
1
- # Temporary Holding Version
1
+ # @elementalfx/gravity
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Gravity for any web page. Headings, paragraphs, images, buttons and cards come loose, fall, tumble and pile up at the bottom of the screen. Visitors can grab them and throw them around, and Escape flies everything back into place.
4
+
5
+ Good for 404 pages, Easter eggs and launch-day fun. About 11 KB gzipped, physics engine included, with no dependencies beyond the shared elementalfx runtime.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @elementalfx/gravity
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```js
16
+ import { createGravity } from '@elementalfx/gravity';
17
+
18
+ const gravity = createGravity(); // the whole page
19
+
20
+ button.addEventListener('click', () => gravity.drop());
21
+ // Later: gravity.restore(), or the visitor presses Escape.
22
+ ```
23
+
24
+ Let the page fall on its own:
25
+
26
+ ```js
27
+ createGravity(document.body, { trigger: 'konami' }); // ↑ ↑ ↓ ↓ ← → ← → B A
28
+ createGravity(document.body, { trigger: { idle: 60 } }); // after a minute of nothing
29
+ createGravity(document.body, { trigger: 'load' }); // straight away (a 404 page?)
30
+ createGravity(document.body, { trigger: { click: '#gravity-button' } });
31
+ ```
32
+
33
+ Make words or letters fall one by one:
34
+
35
+ ```js
36
+ createGravity('.hero', { splitText: 'words' }).drop();
37
+ ```
38
+
39
+ ### HTML element
40
+
41
+ ```js
42
+ import '@elementalfx/gravity/element';
43
+ ```
44
+
45
+ ```html
46
+ <!-- Only this box's contents fall, and they land inside it -->
47
+ <fx-gravity trigger="click #shake" style="height: 400px">
48
+ <h2>Nothing is nailed down</h2>
49
+ <button id="shake">Shake it</button>
50
+ </fx-gravity>
51
+
52
+ <!-- The whole page, on the Konami code -->
53
+ <fx-gravity page trigger="konami"></fx-gravity>
54
+ ```
55
+
56
+ Attributes: `selector`, `split-text`, `gravity`, `bounce`, `friction`, `spin`, `walls`, `only-visible`, `max-bodies`, `interactive`, `trigger` (`load`, `konami`, `idle 30`, `click #button`), `restore-on-escape`, `restore-duration`, `page`.
57
+
58
+ ### Script tag
59
+
60
+ ```html
61
+ <script src="https://unpkg.com/@elementalfx/gravity/dist/gravity.global.js"></script>
62
+ <script>
63
+ ElementalFX.gravity.createGravity(document.body, { trigger: 'konami' });
64
+ </script>
65
+ ```
66
+
67
+ ## What falls
68
+
69
+ By default, the smallest elements that show something: paragraphs, headings, list items, links, images, video, canvas, SVG, buttons and form fields. Small painted boxes, such as cards, badges and chips, fall whole with everything inside them. The sections and wrappers around them stay put.
70
+
71
+ - Pass `selector` to choose the elements yourself.
72
+ - Put `data-gravity="ignore"` on anything that should stay where it is, along with everything inside it.
73
+ - Put `data-gravity="body"` on anything that should fall as one piece.
74
+
75
+ Only elements on screen when the page drops will fall, unless you set `onlyVisible: false`.
76
+
77
+ ## Options
78
+
79
+ | Option | Default | Description |
80
+ | --- | --- | --- |
81
+ | `selector` | auto | CSS selector for the elements that fall |
82
+ | `splitText` | `false` | `'words'` or `'letters'` to drop text piece by piece |
83
+ | `gravity` | `2000` | Downward acceleration in px/s² |
84
+ | `bounce` | `0.25` | How bouncy elements are, 0 to 1 |
85
+ | `friction` | `0.5` | Surface friction, 0 to 1 |
86
+ | `spin` | `0.6` | Random spin as elements start to fall, in rad/s |
87
+ | `walls` | auto | Where elements land: `'viewport'` (the bottom of the screen; the default for the page) or `'root'` (the target's box; the default for other elements) |
88
+ | `onlyVisible` | `true` | Only elements on screen fall |
89
+ | `maxBodies` | `400` | Most elements that fall at once |
90
+ | `interactive` | `true` | Drag and throw fallen elements with a mouse or finger |
91
+ | `trigger` | `'manual'` | `'load'`, `'konami'`, `{ idle: seconds }` or `{ click: selector }` |
92
+ | `restoreOnEscape` | `true` | Escape puts everything back |
93
+ | `restoreDuration` | `0.9` | Seconds the trip back takes |
94
+ | `respectReducedMotion` | `true` | Do nothing when the user prefers reduced motion |
95
+
96
+ ## Methods
97
+
98
+ | Method | Description |
99
+ | --- | --- |
100
+ | `drop()` | Let everything fall. Resolves once it all rests. |
101
+ | `restore()` | Fly everything back. Resolves when the page is exactly as it was. |
102
+ | `toggle()` | Drop or restore. |
103
+ | `explode(at?, strength?)` | Blow everything apart from a point in viewport px (the middle by default). |
104
+ | `set(options)` | Change options. Physics options apply straight away. |
105
+ | `destroy()` | Put everything back at once and stop listening. |
106
+
107
+ Also available: `state` (`'idle'`, `'falling'` or `'restoring'`) and `bodies` (how many elements are falling).
108
+
109
+ ## How it works
110
+
111
+ Nothing is moved out of the page. Each falling element keeps its place in the layout and moves with a CSS `transform`, so the page never reflows. `restore()` removes every inline style it added and puts any split text back as the original text nodes.
112
+
113
+ The physics is a small rigid-body engine for boxes and circles, written for this package after Erin Catto's [Box2D Lite](https://github.com/erincatto/box2d-lite). It finds box contacts with separating axes and edge clipping, and solves them with warm-started sequential impulses. It steps at a fixed 240 Hz, which keeps stacks standing. Touching bodies fall asleep together once they stop, and a page at rest uses no frames at all. You can use the engine directly:
114
+
115
+ ```js
116
+ import { World, Body } from '@elementalfx/gravity';
117
+
118
+ const world = new World({ gravity: 2000 });
119
+ world.add(new Body({ x: 400, y: 620, width: 800, height: 40, density: 0 })); // static floor
120
+ const box = world.add(new Body({ x: 400, y: 100, width: 60, height: 40 }));
121
+ world.step(1 / 60);
122
+ ```
123
+
124
+ Elements inside scrolling containers or `overflow: hidden` boxes are clipped by them as they fall.
125
+
126
+ MIT License
package/dist/body.d.ts ADDED
@@ -0,0 +1,52 @@
1
+ /**
2
+ * A rigid box or circle in CSS px, radians and seconds. y points down, like
3
+ * the page, so positive angles turn clockwise on screen.
4
+ */
5
+ export interface BodyOptions {
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ shape?: 'box' | 'circle';
11
+ angle?: number;
12
+ /** Mass per CSS px² of area. Default 1. 0 makes the body static. */
13
+ density?: number;
14
+ friction?: number;
15
+ restitution?: number;
16
+ }
17
+ export declare class Body {
18
+ static nextId: number;
19
+ readonly id: number;
20
+ x: number;
21
+ y: number;
22
+ angle: number;
23
+ vx: number;
24
+ vy: number;
25
+ w: number;
26
+ readonly shape: 'box' | 'circle';
27
+ readonly hw: number;
28
+ readonly hh: number;
29
+ readonly radius: number;
30
+ readonly invMass: number;
31
+ readonly invInertia: number;
32
+ readonly mass: number;
33
+ friction: number;
34
+ restitution: number;
35
+ sleeping: boolean;
36
+ sleepTime: number;
37
+ /** @internal The bodies that fell asleep together with this one; they wake together too. */
38
+ island: Body[] | null;
39
+ /** @internal Union-find parent while grouping bodies into islands. */
40
+ root: Body;
41
+ /** Arbitrary data, e.g. the DOM element this body moves. */
42
+ data: unknown;
43
+ constructor(options: BodyOptions);
44
+ get isStatic(): boolean;
45
+ /** Wake this body and everything that fell asleep in contact with it. */
46
+ wake(): void;
47
+ /** Axis-aligned bounds: [minX, minY, maxX, maxY]. */
48
+ bounds(): [number, number, number, number];
49
+ /** Applies an impulse (px·mass/s) at a world point. */
50
+ applyImpulse(px: number, py: number, atX?: number, atY?: number): void;
51
+ }
52
+ //# sourceMappingURL=body.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"body.d.ts","sourceRoot":"","sources":["../src/body.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,KAAK,GAAG,QAAQ,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,oEAAoE;IACpE,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,qBAAa,IAAI;IACf,MAAM,CAAC,MAAM,SAAK;IAClB,QAAQ,CAAC,EAAE,SAAiB;IAC5B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,EAAE,SAAK;IACP,EAAE,SAAK;IACP,CAAC,SAAK;IACN,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ,CAAC;IACjC,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,UAAS;IACjB,SAAS,SAAK;IACd,4FAA4F;IAC5F,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,CAAQ;IAC7B,sEAAsE;IACtE,IAAI,EAAE,IAAI,CAAQ;IAClB,4DAA4D;IAC5D,IAAI,EAAE,OAAO,CAAQ;gBAET,OAAO,EAAE,WAAW;IA0BhC,IAAI,QAAQ,IAAI,OAAO,CAEtB;IAED,yEAAyE;IACzE,IAAI,IAAI,IAAI;IAWZ,qDAAqD;IACrD,MAAM,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC;IAW1C,uDAAuD;IACvD,YAAY,CAAC,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,SAAS,EAAE,GAAG,SAAS,GAAG,IAAI;CAOvE"}