@vectoad/luxebook-page-physics 0.0.0-stage → 0.1.0

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 Sleman
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,5 @@
1
- # Temporary Holding Version
1
+ # @vectoad/luxebook-page-physics
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
+ Internal building block of [Luxebook](https://github.com/Vectoad/luxebook). Most people want [`@vectoad/luxebook-react`](https://www.npmjs.com/package/@vectoad/luxebook-react) or [`@vectoad/luxebook-core`](https://www.npmjs.com/package/@vectoad/luxebook-core).
4
+
5
+ MIT license.
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Page deformation: cylindrical curl model (reference implementation).
3
+ *
4
+ * Milestone 3 ports `curlVertex` to a GLSL vertex shader; this CPU version is
5
+ * the specification the shader is tested against and is what interaction
6
+ * code uses for hit-testing. See docs/PAGE_CURL.md for the derivation.
7
+ *
8
+ * Coordinate system (page-local, matches the renderer's page geometry):
9
+ * - spine along the y axis at x = 0, page spans x ∈ [0, width]
10
+ * - the flat page lies in z = 0, front face towards +z
11
+ * - curling lifts the page towards +z (towards the reader)
12
+ */
13
+ interface CurlParams {
14
+ /** A point on the curl axis (the line where the page starts to bend). */
15
+ axisX: number;
16
+ axisY: number;
17
+ /**
18
+ * Unit vector in the page plane, perpendicular to the axis, pointing at
19
+ * the part of the page that lifts.
20
+ */
21
+ dirX: number;
22
+ dirY: number;
23
+ /** Radius of the roll at the fold. Must be > 0. */
24
+ radius: number;
25
+ /** Sign of the lift direction; -1 for a leaf whose local +z points away from the reader. */
26
+ bendDirection?: 1 | -1;
27
+ }
28
+ interface CurlResult {
29
+ x: number;
30
+ y: number;
31
+ z: number;
32
+ nx: number;
33
+ ny: number;
34
+ nz: number;
35
+ }
36
+ /**
37
+ * Bend a point on the page around a cylinder lying on the curl axis: paper
38
+ * past the axis rolls over it and, after half a turn, lies flat again,
39
+ * face down, on top of the page. Arc length is preserved (no stretching).
40
+ * Writes into `out` to avoid per-vertex allocation.
41
+ */
42
+ declare function curlVertex(x: number, y: number, p: CurlParams, out: CurlResult): CurlResult;
43
+ /**
44
+ * Fold a leaf so its point `g` lands on `c` (both page-local): the fold axis
45
+ * is the perpendicular bisector of g→c, shifted so the roll of `radius`
46
+ * still puts `g` exactly on `c`. Null when there is nothing to fold.
47
+ */
48
+ declare function foldTo(gx: number, gy: number, cx: number, cy: number, radius: number, bendDirection?: 1 | -1): CurlParams | null;
49
+ type TurnDirection = "forward" | "backward";
50
+ type TurnPhase = "idle" | "dragging" | "settling" | "completed" | "cancelled";
51
+ /**
52
+ * A page turn as a fold: the grabbed point of the leaf (page-local, spine at
53
+ * x = 0, page y ∈ [−height/2, height/2]) follows a target, the pointer while
54
+ * dragging, then a path to the opposite side (or back) once released. The
55
+ * leaf stays bound at the spine: the point never gets further from either
56
+ * end of the spine than it started. Coordinates are page units; time is ms.
57
+ */
58
+ declare class PageTurnPhysics {
59
+ readonly width: number;
60
+ readonly height: number;
61
+ phase: TurnPhase;
62
+ direction: TurnDirection;
63
+ /** 0 = lying where it started, 1 = turned over onto the other side. */
64
+ progress: number;
65
+ velocity: number;
66
+ /** Where the grabbed point is now. */
67
+ cornerX: number;
68
+ cornerY: number;
69
+ /** The grabbed point of the leaf (page-local), set by `begin`. */
70
+ grabX: number;
71
+ grabY: number;
72
+ private targetX;
73
+ private targetY;
74
+ private target;
75
+ private lastProgress;
76
+ private lastTime;
77
+ /** Release: where the point was, and the progress then. */
78
+ private fromX;
79
+ private fromY;
80
+ private fromProgress;
81
+ /** Seconds into a timed turn (`stepTimed`) or a release glide, −1 when not started. */
82
+ private clock;
83
+ /** How long the glide after a release takes. */
84
+ private settleSeconds;
85
+ constructor(width: number, height?: number);
86
+ /** Grab the leaf at (x, y). A grab near the top or bottom takes the corner. */
87
+ begin(direction: TurnDirection, x: number, y: number, time?: number): void;
88
+ /** Move the grabbed point towards (x, y); `step` eases it there. */
89
+ drag(x: number, y: number, time: number): void;
90
+ /** Release commits by progress or fling velocity; pass a value to force either outcome. */
91
+ release(commit?: boolean): boolean;
92
+ /** Advance by `dt` seconds (follow the pointer, or glide after release). True when the turn has completed or cancelled. */
93
+ step(dt: number): boolean;
94
+ /**
95
+ * Settle on a fixed timeline instead of the spring: ease-in-out over
96
+ * `duration` seconds, from wherever the page is. For turns started by a tap
97
+ * or key, where a spring from rest looks like a snap. Returns true when done.
98
+ */
99
+ stepTimed(dt: number, duration: number): boolean;
100
+ /** The fold for the current state, or null while the leaf lies flat. */
101
+ get curl(): CurlParams | null;
102
+ private finish;
103
+ private progressAt;
104
+ /** Released: travel from the release point to the end, lifting slightly off the edge on the way. */
105
+ private followPath;
106
+ /** Keep the leaf attached at the spine: no further from either spine end than the grabbed point was. */
107
+ private bind;
108
+ }
109
+
110
+ export { type CurlParams, type CurlResult, PageTurnPhysics, type TurnDirection, type TurnPhase, curlVertex, foldTo };
package/dist/index.js ADDED
@@ -0,0 +1,218 @@
1
+ // src/index.ts
2
+ function curlVertex(x, y, p, out) {
3
+ const { dirX, dirY, radius: r } = p;
4
+ const bendDirection = p.bendDirection ?? 1;
5
+ const s = (x - p.axisX) * dirX + (y - p.axisY) * dirY;
6
+ out.y = y;
7
+ if (s <= 0) {
8
+ out.x = x;
9
+ out.z = 0;
10
+ out.nx = 0;
11
+ out.ny = 0;
12
+ out.nz = 1;
13
+ return out;
14
+ }
15
+ const footX = x - s * dirX;
16
+ const footY = y - s * dirY;
17
+ let offset;
18
+ let z;
19
+ let sinT;
20
+ let cosT;
21
+ const halfTurn = Math.PI * r;
22
+ if (s <= halfTurn) {
23
+ const theta = s / r;
24
+ sinT = Math.sin(theta);
25
+ cosT = Math.cos(theta);
26
+ offset = r * sinT;
27
+ z = r * (1 - cosT);
28
+ } else {
29
+ sinT = 0;
30
+ cosT = -1;
31
+ offset = -(s - halfTurn);
32
+ z = 2 * r;
33
+ }
34
+ out.x = footX + offset * dirX;
35
+ out.y = footY + offset * dirY;
36
+ out.z = z * bendDirection;
37
+ out.nx = -sinT * dirX * bendDirection;
38
+ out.ny = -sinT * dirY * bendDirection;
39
+ out.nz = cosT;
40
+ return out;
41
+ }
42
+ function foldTo(gx, gy, cx, cy, radius, bendDirection = 1) {
43
+ const d = Math.hypot(gx - cx, gy - cy);
44
+ if (d < 1e-6) return null;
45
+ const dirX = (gx - cx) / d;
46
+ const dirY = (gy - cy) / d;
47
+ const r = Math.max(1e-4, radius);
48
+ const s = (d + Math.PI * r) / 2;
49
+ return { axisX: gx - s * dirX, axisY: gy - s * dirY, dirX, dirY, radius: r, bendDirection };
50
+ }
51
+ var clamp = (value, min, max) => Math.max(min, Math.min(max, value));
52
+ var PageTurnPhysics = class {
53
+ constructor(width, height = 1) {
54
+ this.width = width;
55
+ this.height = height;
56
+ if (!Number.isFinite(width) || width <= 0) throw new RangeError("Page width must be > 0.");
57
+ }
58
+ width;
59
+ height;
60
+ phase = "idle";
61
+ direction = "forward";
62
+ /** 0 = lying where it started, 1 = turned over onto the other side. */
63
+ progress = 0;
64
+ velocity = 0;
65
+ /** Where the grabbed point is now. */
66
+ cornerX = 0;
67
+ cornerY = 0;
68
+ /** The grabbed point of the leaf (page-local), set by `begin`. */
69
+ grabX = 0;
70
+ grabY = 0;
71
+ targetX = 0;
72
+ targetY = 0;
73
+ target = 0;
74
+ lastProgress = 0;
75
+ lastTime = 0;
76
+ /** Release: where the point was, and the progress then. */
77
+ fromX = 0;
78
+ fromY = 0;
79
+ fromProgress = 0;
80
+ /** Seconds into a timed turn (`stepTimed`) or a release glide, −1 when not started. */
81
+ clock = -1;
82
+ /** How long the glide after a release takes. */
83
+ settleSeconds = 0.3;
84
+ /** Grab the leaf at (x, y). A grab near the top or bottom takes the corner. */
85
+ begin(direction, x, y, time = 0) {
86
+ const half = this.height / 2;
87
+ this.direction = direction;
88
+ this.grabX = clamp(x, this.width * 0.25, this.width);
89
+ this.grabY = Math.abs(y) > half * 0.7 ? Math.sign(y) * half : clamp(y, -half, half);
90
+ this.cornerX = this.targetX = this.grabX;
91
+ this.cornerY = this.targetY = this.grabY;
92
+ this.progress = this.velocity = this.target = this.lastProgress = 0;
93
+ this.lastTime = time;
94
+ this.clock = -1;
95
+ this.phase = "dragging";
96
+ }
97
+ /** Move the grabbed point towards (x, y); `step` eases it there. */
98
+ drag(x, y, time) {
99
+ if (this.phase !== "dragging") return;
100
+ [this.targetX, this.targetY] = this.bind(x, y);
101
+ const next = this.progressAt(this.targetX);
102
+ const t = clamp((next - 0.55) / 0.4, 0, 1);
103
+ this.targetY += (this.grabY - this.targetY) * t * t * (3 - 2 * t);
104
+ const elapsed = Math.max((time - this.lastTime) / 1e3, 1 / 240);
105
+ this.velocity = this.velocity * 0.35 + (next - this.lastProgress) / elapsed * 0.65;
106
+ this.lastProgress = next;
107
+ this.lastTime = time;
108
+ }
109
+ /** Release commits by progress or fling velocity; pass a value to force either outcome. */
110
+ release(commit) {
111
+ if (this.phase !== "dragging") return this.target === 1;
112
+ const shouldCommit = commit ?? (this.progressAt(this.targetX) >= 0.34 || this.velocity >= 1.15);
113
+ this.target = shouldCommit ? 1 : 0;
114
+ this.fromX = this.cornerX;
115
+ this.fromY = this.cornerY;
116
+ this.progress = this.fromProgress = this.progressAt(this.cornerX);
117
+ this.settleSeconds = 0.35 + 0.45 * Math.abs(this.target - this.fromProgress);
118
+ this.clock = 0;
119
+ this.velocity = 0;
120
+ this.phase = "settling";
121
+ return shouldCommit;
122
+ }
123
+ /** Advance by `dt` seconds (follow the pointer, or glide after release). True when the turn has completed or cancelled. */
124
+ step(dt) {
125
+ if (this.phase === "dragging") {
126
+ const k = 1 - Math.exp(-clamp(dt, 0, 0.1) * 28);
127
+ this.cornerX += (this.targetX - this.cornerX) * k;
128
+ this.cornerY += (this.targetY - this.cornerY) * k;
129
+ this.progress = this.progressAt(this.cornerX);
130
+ return false;
131
+ }
132
+ if (this.phase !== "settling") return this.phase === "completed" || this.phase === "cancelled";
133
+ this.clock += clamp(dt, 0, 0.1);
134
+ const t = Math.min(1, this.clock / this.settleSeconds);
135
+ this.progress = this.fromProgress + (this.target - this.fromProgress) * (1 - (1 - t) ** 3);
136
+ if (t >= 1) return this.finish();
137
+ this.followPath();
138
+ return false;
139
+ }
140
+ /**
141
+ * Settle on a fixed timeline instead of the spring: ease-in-out over
142
+ * `duration` seconds, from wherever the page is. For turns started by a tap
143
+ * or key, where a spring from rest looks like a snap. Returns true when done.
144
+ */
145
+ stepTimed(dt, duration) {
146
+ if (this.phase !== "settling") return this.phase === "completed" || this.phase === "cancelled";
147
+ if (this.clock < 0) this.clock = 0;
148
+ this.clock += clamp(dt, 0, 0.1);
149
+ const t = Math.min(1, this.clock / duration);
150
+ const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
151
+ this.progress = this.fromProgress + (this.target - this.fromProgress) * eased;
152
+ this.velocity = 0;
153
+ if (t >= 1) return this.finish();
154
+ this.followPath();
155
+ return false;
156
+ }
157
+ /** The fold for the current state, or null while the leaf lies flat. */
158
+ get curl() {
159
+ const d = Math.hypot(this.grabX - this.cornerX, this.grabY - this.cornerY);
160
+ const radius = Math.min(this.width * 0.07 * Math.sin(Math.PI * clamp(this.progress, 0, 1)) ** 0.4, d * 0.3);
161
+ const fold = foldTo(this.grabX, this.grabY, this.cornerX, this.cornerY, Math.max(radius, this.width * 4e-3), this.direction === "forward" ? 1 : -1);
162
+ if (!fold) return null;
163
+ for (const sy of [this.height / 2, -this.height / 2]) {
164
+ const past = (0 - fold.axisX) * fold.dirX + (sy - fold.axisY) * fold.dirY;
165
+ if (past > 0) {
166
+ fold.axisX += past * fold.dirX;
167
+ fold.axisY += past * fold.dirY;
168
+ }
169
+ }
170
+ return fold;
171
+ }
172
+ finish() {
173
+ this.progress = this.target;
174
+ this.velocity = 0;
175
+ this.cornerX = this.target === 1 ? -this.grabX : this.grabX;
176
+ this.cornerY = this.grabY;
177
+ this.phase = this.target === 1 ? "completed" : "cancelled";
178
+ return true;
179
+ }
180
+ progressAt(x) {
181
+ return clamp((this.grabX - x) / (2 * this.grabX), 0, 1);
182
+ }
183
+ /** Released: travel from the release point to the end, lifting slightly off the edge on the way. */
184
+ followPath() {
185
+ const endX = this.target === 1 ? -this.grabX : this.grabX;
186
+ const span = this.target - this.fromProgress;
187
+ const f = Math.abs(span) < 1e-6 ? 1 : clamp((this.progress - this.fromProgress) / span, 0, 1);
188
+ const arc = this.height * 0.04 * Math.abs(endX - this.fromX) / (2 * this.grabX) * Math.sin(Math.PI * f);
189
+ const inward = this.grabY > 0 ? -1 : 1;
190
+ [this.cornerX, this.cornerY] = this.bind(
191
+ this.fromX + (endX - this.fromX) * f,
192
+ this.fromY + (this.grabY - this.fromY) * f + inward * arc
193
+ );
194
+ }
195
+ /** Keep the leaf attached at the spine: no further from either spine end than the grabbed point was. */
196
+ bind(x, y) {
197
+ const half = this.height / 2;
198
+ for (let i = 0; i < 2; i++) {
199
+ for (const sy of [half, -half]) {
200
+ const max = Math.hypot(this.grabX, this.grabY - sy);
201
+ const dx = x;
202
+ const dy = y - sy;
203
+ const dist = Math.hypot(dx, dy);
204
+ if (dist > max) {
205
+ x = dx * max / dist;
206
+ y = sy + dy * max / dist;
207
+ }
208
+ }
209
+ }
210
+ return [x, y];
211
+ }
212
+ };
213
+ export {
214
+ PageTurnPhysics,
215
+ curlVertex,
216
+ foldTo
217
+ };
218
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Page deformation: cylindrical curl model (reference implementation).\n *\n * Milestone 3 ports `curlVertex` to a GLSL vertex shader; this CPU version is\n * the specification the shader is tested against and is what interaction\n * code uses for hit-testing. See docs/PAGE_CURL.md for the derivation.\n *\n * Coordinate system (page-local, matches the renderer's page geometry):\n * - spine along the y axis at x = 0, page spans x ∈ [0, width]\n * - the flat page lies in z = 0, front face towards +z\n * - curling lifts the page towards +z (towards the reader)\n */\n\nexport interface CurlParams {\n /** A point on the curl axis (the line where the page starts to bend). */\n axisX: number;\n axisY: number;\n /**\n * Unit vector in the page plane, perpendicular to the axis, pointing at\n * the part of the page that lifts.\n */\n dirX: number;\n dirY: number;\n /** Radius of the roll at the fold. Must be > 0. */\n radius: number;\n /** Sign of the lift direction; -1 for a leaf whose local +z points away from the reader. */\n bendDirection?: 1 | -1;\n}\n\nexport interface CurlResult {\n x: number;\n y: number;\n z: number;\n nx: number;\n ny: number;\n nz: number;\n}\n\n/**\n * Bend a point on the page around a cylinder lying on the curl axis: paper\n * past the axis rolls over it and, after half a turn, lies flat again,\n * face down, on top of the page. Arc length is preserved (no stretching).\n * Writes into `out` to avoid per-vertex allocation.\n */\nexport function curlVertex(x: number, y: number, p: CurlParams, out: CurlResult): CurlResult {\n const { dirX, dirY, radius: r } = p;\n const bendDirection = p.bendDirection ?? 1;\n const s = (x - p.axisX) * dirX + (y - p.axisY) * dirY;\n out.y = y;\n if (s <= 0) {\n out.x = x;\n out.z = 0;\n out.nx = 0;\n out.ny = 0;\n out.nz = 1;\n return out;\n }\n // Foot of the point on the axis line, then move back out along `dir`.\n const footX = x - s * dirX;\n const footY = y - s * dirY;\n let offset: number;\n let z: number;\n let sinT: number;\n let cosT: number;\n const halfTurn = Math.PI * r;\n if (s <= halfTurn) {\n const theta = s / r;\n sinT = Math.sin(theta);\n cosT = Math.cos(theta);\n offset = r * sinT;\n z = r * (1 - cosT);\n } else {\n sinT = 0;\n cosT = -1;\n offset = -(s - halfTurn);\n z = 2 * r;\n }\n out.x = footX + offset * dirX;\n out.y = footY + offset * dirY;\n out.z = z * bendDirection;\n // The front normal (0,0,1) rotates with the surface: (−sinθ·dir, cosθ).\n out.nx = -sinT * dirX * bendDirection;\n out.ny = -sinT * dirY * bendDirection;\n out.nz = cosT;\n return out;\n}\n\n/**\n * Fold a leaf so its point `g` lands on `c` (both page-local): the fold axis\n * is the perpendicular bisector of g→c, shifted so the roll of `radius`\n * still puts `g` exactly on `c`. Null when there is nothing to fold.\n */\nexport function foldTo(gx: number, gy: number, cx: number, cy: number, radius: number, bendDirection: 1 | -1 = 1): CurlParams | null {\n const d = Math.hypot(gx - cx, gy - cy);\n if (d < 1e-6) return null;\n const dirX = (gx - cx) / d;\n const dirY = (gy - cy) / d;\n const r = Math.max(1e-4, radius);\n const s = (d + Math.PI * r) / 2; // g's distance past the axis\n return { axisX: gx - s * dirX, axisY: gy - s * dirY, dirX, dirY, radius: r, bendDirection };\n}\n\nexport type TurnDirection = \"forward\" | \"backward\";\nexport type TurnPhase = \"idle\" | \"dragging\" | \"settling\" | \"completed\" | \"cancelled\";\n\nconst clamp = (value: number, min: number, max: number): number => Math.max(min, Math.min(max, value));\n\n/**\n * A page turn as a fold: the grabbed point of the leaf (page-local, spine at\n * x = 0, page y ∈ [−height/2, height/2]) follows a target, the pointer while\n * dragging, then a path to the opposite side (or back) once released. The\n * leaf stays bound at the spine: the point never gets further from either\n * end of the spine than it started. Coordinates are page units; time is ms.\n */\nexport class PageTurnPhysics {\n phase: TurnPhase = \"idle\";\n direction: TurnDirection = \"forward\";\n /** 0 = lying where it started, 1 = turned over onto the other side. */\n progress = 0;\n velocity = 0;\n /** Where the grabbed point is now. */\n cornerX = 0;\n cornerY = 0;\n /** The grabbed point of the leaf (page-local), set by `begin`. */\n grabX = 0;\n grabY = 0;\n private targetX = 0;\n private targetY = 0;\n private target = 0;\n private lastProgress = 0;\n private lastTime = 0;\n /** Release: where the point was, and the progress then. */\n private fromX = 0;\n private fromY = 0;\n private fromProgress = 0;\n /** Seconds into a timed turn (`stepTimed`) or a release glide, −1 when not started. */\n private clock = -1;\n /** How long the glide after a release takes. */\n private settleSeconds = 0.3;\n\n constructor(\n readonly width: number,\n readonly height = 1,\n ) {\n if (!Number.isFinite(width) || width <= 0) throw new RangeError(\"Page width must be > 0.\");\n }\n\n /** Grab the leaf at (x, y). A grab near the top or bottom takes the corner. */\n begin(direction: TurnDirection, x: number, y: number, time = 0): void {\n const half = this.height / 2;\n this.direction = direction;\n this.grabX = clamp(x, this.width * 0.25, this.width);\n this.grabY = Math.abs(y) > half * 0.7 ? Math.sign(y) * half : clamp(y, -half, half);\n this.cornerX = this.targetX = this.grabX;\n this.cornerY = this.targetY = this.grabY;\n this.progress = this.velocity = this.target = this.lastProgress = 0;\n this.lastTime = time;\n this.clock = -1;\n this.phase = \"dragging\";\n }\n\n /** Move the grabbed point towards (x, y); `step` eases it there. */\n drag(x: number, y: number, time: number): void {\n if (this.phase !== \"dragging\") return;\n [this.targetX, this.targetY] = this.bind(x, y);\n const next = this.progressAt(this.targetX);\n // Towards the end the page squares up with the book: it lands flat, not twisted.\n const t = clamp((next - 0.55) / 0.4, 0, 1);\n this.targetY += (this.grabY - this.targetY) * t * t * (3 - 2 * t);\n const elapsed = Math.max((time - this.lastTime) / 1000, 1 / 240);\n this.velocity = this.velocity * 0.35 + ((next - this.lastProgress) / elapsed) * 0.65;\n this.lastProgress = next;\n this.lastTime = time;\n }\n\n /** Release commits by progress or fling velocity; pass a value to force either outcome. */\n release(commit?: boolean): boolean {\n if (this.phase !== \"dragging\") return this.target === 1;\n const shouldCommit = commit ?? (this.progressAt(this.targetX) >= 0.34 || this.velocity >= 1.15);\n this.target = shouldCommit ? 1 : 0;\n // Carry on from where the point is now: no jump to the pointer.\n this.fromX = this.cornerX;\n this.fromY = this.cornerY;\n this.progress = this.fromProgress = this.progressAt(this.cornerX);\n // Glide there, easing out, in time for the distance left: a page let go\n // early slides back to rest rather than snapping (whatever its speed).\n this.settleSeconds = 0.35 + 0.45 * Math.abs(this.target - this.fromProgress);\n this.clock = 0;\n this.velocity = 0;\n this.phase = \"settling\";\n return shouldCommit;\n }\n\n /** Advance by `dt` seconds (follow the pointer, or glide after release). True when the turn has completed or cancelled. */\n step(dt: number): boolean {\n if (this.phase === \"dragging\") {\n // Follow the pointer closely but not rigidly: paper has a little inertia.\n const k = 1 - Math.exp(-clamp(dt, 0, 0.1) * 28);\n this.cornerX += (this.targetX - this.cornerX) * k;\n this.cornerY += (this.targetY - this.cornerY) * k;\n this.progress = this.progressAt(this.cornerX);\n return false;\n }\n if (this.phase !== \"settling\") return this.phase === \"completed\" || this.phase === \"cancelled\";\n this.clock += clamp(dt, 0, 0.1);\n const t = Math.min(1, this.clock / this.settleSeconds);\n this.progress = this.fromProgress + (this.target - this.fromProgress) * (1 - (1 - t) ** 3);\n if (t >= 1) return this.finish();\n this.followPath();\n return false;\n }\n\n /**\n * Settle on a fixed timeline instead of the spring: ease-in-out over\n * `duration` seconds, from wherever the page is. For turns started by a tap\n * or key, where a spring from rest looks like a snap. Returns true when done.\n */\n stepTimed(dt: number, duration: number): boolean {\n if (this.phase !== \"settling\") return this.phase === \"completed\" || this.phase === \"cancelled\";\n if (this.clock < 0) this.clock = 0;\n this.clock += clamp(dt, 0, 0.1);\n const t = Math.min(1, this.clock / duration);\n const eased = t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;\n this.progress = this.fromProgress + (this.target - this.fromProgress) * eased;\n this.velocity = 0;\n if (t >= 1) return this.finish();\n this.followPath();\n return false;\n }\n\n /** The fold for the current state, or null while the leaf lies flat. */\n get curl(): CurlParams | null {\n const d = Math.hypot(this.grabX - this.cornerX, this.grabY - this.cornerY);\n // A soft roll mid-turn that tightens as the leaf lands, never so wide the point leaves the pointer.\n const radius = Math.min(this.width * 0.07 * Math.sin(Math.PI * clamp(this.progress, 0, 1)) ** 0.4, d * 0.3);\n const fold = foldTo(this.grabX, this.grabY, this.cornerX, this.cornerY, Math.max(radius, this.width * 0.004), this.direction === \"forward\" ? 1 : -1);\n if (!fold) return null;\n // The spine never folds: if the axis would cut across it, slide the fold back\n // (the corner then trails the pointer a little rather than tearing the page off).\n for (const sy of [this.height / 2, -this.height / 2]) {\n const past = (0 - fold.axisX) * fold.dirX + (sy - fold.axisY) * fold.dirY;\n if (past > 0) {\n fold.axisX += past * fold.dirX;\n fold.axisY += past * fold.dirY;\n }\n }\n return fold;\n }\n\n private finish(): true {\n this.progress = this.target;\n this.velocity = 0;\n this.cornerX = this.target === 1 ? -this.grabX : this.grabX;\n this.cornerY = this.grabY;\n this.phase = this.target === 1 ? \"completed\" : \"cancelled\";\n return true;\n }\n\n private progressAt(x: number): number {\n return clamp((this.grabX - x) / (2 * this.grabX), 0, 1);\n }\n\n /** Released: travel from the release point to the end, lifting slightly off the edge on the way. */\n private followPath(): void {\n const endX = this.target === 1 ? -this.grabX : this.grabX;\n const span = this.target - this.fromProgress;\n const f = Math.abs(span) < 1e-6 ? 1 : clamp((this.progress - this.fromProgress) / span, 0, 1);\n const arc = this.height * 0.04 * Math.abs(endX - this.fromX) / (2 * this.grabX) * Math.sin(Math.PI * f);\n const inward = this.grabY > 0 ? -1 : 1;\n [this.cornerX, this.cornerY] = this.bind(\n this.fromX + (endX - this.fromX) * f,\n this.fromY + (this.grabY - this.fromY) * f + inward * arc,\n );\n }\n\n /** Keep the leaf attached at the spine: no further from either spine end than the grabbed point was. */\n private bind(x: number, y: number): [number, number] {\n const half = this.height / 2;\n for (let i = 0; i < 2; i++) {\n for (const sy of [half, -half]) {\n const max = Math.hypot(this.grabX, this.grabY - sy);\n const dx = x;\n const dy = y - sy;\n const dist = Math.hypot(dx, dy);\n if (dist > max) {\n x = (dx * max) / dist;\n y = sy + (dy * max) / dist;\n }\n }\n }\n return [x, y];\n }\n}\n"],"mappings":";AA4CO,SAAS,WAAW,GAAW,GAAW,GAAe,KAA6B;AAC3F,QAAM,EAAE,MAAM,MAAM,QAAQ,EAAE,IAAI;AAClC,QAAM,gBAAgB,EAAE,iBAAiB;AACzC,QAAM,KAAK,IAAI,EAAE,SAAS,QAAQ,IAAI,EAAE,SAAS;AACjD,MAAI,IAAI;AACR,MAAI,KAAK,GAAG;AACV,QAAI,IAAI;AACR,QAAI,IAAI;AACR,QAAI,KAAK;AACT,QAAI,KAAK;AACT,QAAI,KAAK;AACT,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,IAAI,IAAI;AACtB,QAAM,QAAQ,IAAI,IAAI;AACtB,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,MAAI;AACJ,QAAM,WAAW,KAAK,KAAK;AAC3B,MAAI,KAAK,UAAU;AACjB,UAAM,QAAQ,IAAI;AAClB,WAAO,KAAK,IAAI,KAAK;AACrB,WAAO,KAAK,IAAI,KAAK;AACrB,aAAS,IAAI;AACb,QAAI,KAAK,IAAI;AAAA,EACf,OAAO;AACL,WAAO;AACP,WAAO;AACP,aAAS,EAAE,IAAI;AACf,QAAI,IAAI;AAAA,EACV;AACA,MAAI,IAAI,QAAQ,SAAS;AACzB,MAAI,IAAI,QAAQ,SAAS;AACzB,MAAI,IAAI,IAAI;AAEZ,MAAI,KAAK,CAAC,OAAO,OAAO;AACxB,MAAI,KAAK,CAAC,OAAO,OAAO;AACxB,MAAI,KAA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K,KAAK,KAAK,IAAI,KAAK,SAAS,KAAK,IAAI,KAAK,KAAK,CAAC;AACtG,UAAM,SAAS,KAAK,QAAQ,IAAI,KAAK;AACrC,KAAC,KAAK,SAAS,KAAK,OAAO,IAAI,KAAK;AAAA,MAClC,KAAK,SAAS,OAAO,KAAK,SAAS;AAAA,MACnC,KAAK,SAAS,KAAK,QAAQ,KAAK,SAAS,IAAI,SAAS;AAAA,IACxD;AAAA,EACF;AAAA;AAAA,EAGQ,KAAK,GAAW,GAA6B;AACnD,UAAM,OAAO,KAAK,SAAS;AAC3B,aAAS,IAAI,GAAG,IAAI,GAAG,KAAK;AAC1B,iBAAW,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG;AAC9B,cAAM,MAAM,KAAK,MAAM,KAAK,OAAO,KAAK,QAAQ,EAAE;AAClD,cAAM,KAAK;AACX,cAAM,KAAK,IAAI;AACf,cAAM,OAAO,KAAK,MAAM,IAAI,EAAE;AAC9B,YAAI,OAAO,KAAK;AACd,cAAK,KAAK,MAAO;AACjB,cAAI,KAAM,KAAK,MAAO;AAAA,QACxB;AAAA,MACF;AAAA,IACF;AACA,WAAO,CAAC,GAAG,CAAC;AAAA,EACd;AACF;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,30 @@
1
1
  {
2
2
  "name": "@vectoad/luxebook-page-physics",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "exports": {
8
+ ".": {
9
+ "luxebook-source": "./src/index.ts",
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "types": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/Vectoad/luxebook.git",
21
+ "directory": "packages/page-physics"
22
+ },
23
+ "publishConfig": {
24
+ "registry": "https://registry.npmjs.org",
25
+ "access": "public"
26
+ },
27
+ "scripts": {
28
+ "build": "tsup src/index.ts --format esm --dts --clean --sourcemap --tsconfig ../../tsconfig.base.json"
29
+ }
6
30
  }