@gfazioli/mantine-book 1.0.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/CODE_OF_CONDUCT.md +128 -0
- package/CONTRIBUTING.md +18 -0
- package/LICENSE +21 -0
- package/README.md +105 -0
- package/dist/cjs/Book/Book.cjs +327 -0
- package/dist/cjs/Book/Book.cjs.map +1 -0
- package/dist/cjs/Book/Book.context.cjs +28 -0
- package/dist/cjs/Book/Book.context.cjs.map +1 -0
- package/dist/cjs/Book/Book.module.css.cjs +7 -0
- package/dist/cjs/Book/Book.module.css.cjs.map +1 -0
- package/dist/cjs/Book/BookPage.cjs +27 -0
- package/dist/cjs/Book/BookPage.cjs.map +1 -0
- package/dist/cjs/Book/page-index.cjs +15 -0
- package/dist/cjs/Book/page-index.cjs.map +1 -0
- package/dist/cjs/Curl/Curl.cjs +298 -0
- package/dist/cjs/Curl/Curl.cjs.map +1 -0
- package/dist/cjs/Curl/Curl.module.css.cjs +7 -0
- package/dist/cjs/Curl/Curl.module.css.cjs.map +1 -0
- package/dist/cjs/Curl/webgl/CurlWebglLayer.cjs +262 -0
- package/dist/cjs/Curl/webgl/CurlWebglLayer.cjs.map +1 -0
- package/dist/cjs/Curl/webgl/glRenderer.cjs +358 -0
- package/dist/cjs/Curl/webgl/glRenderer.cjs.map +1 -0
- package/dist/cjs/Curl/webgl/pool.cjs +56 -0
- package/dist/cjs/Curl/webgl/pool.cjs.map +1 -0
- package/dist/cjs/Curl/webgl/poolContext.cjs +9 -0
- package/dist/cjs/Curl/webgl/poolContext.cjs.map +1 -0
- package/dist/cjs/Curl/webgl/snapshot.cjs +25 -0
- package/dist/cjs/Curl/webgl/snapshot.cjs.map +1 -0
- package/dist/cjs/flip/animator.cjs +53 -0
- package/dist/cjs/flip/animator.cjs.map +1 -0
- package/dist/cjs/flip/coneDeformer.cjs +36 -0
- package/dist/cjs/flip/coneDeformer.cjs.map +1 -0
- package/dist/cjs/flip/drag.cjs +185 -0
- package/dist/cjs/flip/drag.cjs.map +1 -0
- package/dist/cjs/flip/geometry.cjs +146 -0
- package/dist/cjs/flip/geometry.cjs.map +1 -0
- package/dist/cjs/flip/useCurlController.cjs +178 -0
- package/dist/cjs/flip/useCurlController.cjs.map +1 -0
- package/dist/cjs/index.cjs +14 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/esm/Book/Book.context.mjs +24 -0
- package/dist/esm/Book/Book.context.mjs.map +1 -0
- package/dist/esm/Book/Book.mjs +325 -0
- package/dist/esm/Book/Book.mjs.map +1 -0
- package/dist/esm/Book/Book.module.css.mjs +5 -0
- package/dist/esm/Book/Book.module.css.mjs.map +1 -0
- package/dist/esm/Book/BookPage.mjs +25 -0
- package/dist/esm/Book/BookPage.mjs.map +1 -0
- package/dist/esm/Book/page-index.mjs +12 -0
- package/dist/esm/Book/page-index.mjs.map +1 -0
- package/dist/esm/Curl/Curl.mjs +295 -0
- package/dist/esm/Curl/Curl.mjs.map +1 -0
- package/dist/esm/Curl/Curl.module.css.mjs +5 -0
- package/dist/esm/Curl/Curl.module.css.mjs.map +1 -0
- package/dist/esm/Curl/webgl/CurlWebglLayer.mjs +260 -0
- package/dist/esm/Curl/webgl/CurlWebglLayer.mjs.map +1 -0
- package/dist/esm/Curl/webgl/glRenderer.mjs +356 -0
- package/dist/esm/Curl/webgl/glRenderer.mjs.map +1 -0
- package/dist/esm/Curl/webgl/pool.mjs +54 -0
- package/dist/esm/Curl/webgl/pool.mjs.map +1 -0
- package/dist/esm/Curl/webgl/poolContext.mjs +7 -0
- package/dist/esm/Curl/webgl/poolContext.mjs.map +1 -0
- package/dist/esm/Curl/webgl/snapshot.mjs +23 -0
- package/dist/esm/Curl/webgl/snapshot.mjs.map +1 -0
- package/dist/esm/flip/animator.mjs +50 -0
- package/dist/esm/flip/animator.mjs.map +1 -0
- package/dist/esm/flip/coneDeformer.mjs +34 -0
- package/dist/esm/flip/coneDeformer.mjs.map +1 -0
- package/dist/esm/flip/drag.mjs +180 -0
- package/dist/esm/flip/drag.mjs.map +1 -0
- package/dist/esm/flip/geometry.mjs +138 -0
- package/dist/esm/flip/geometry.mjs.map +1 -0
- package/dist/esm/flip/useCurlController.mjs +176 -0
- package/dist/esm/flip/useCurlController.mjs.map +1 -0
- package/dist/esm/index.mjs +4 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/styles.css +1 -0
- package/dist/styles.layer.css +1 -0
- package/dist/types/Book/Book.context.d.ts +52 -0
- package/dist/types/Book/Book.d.ts +116 -0
- package/dist/types/Book/BookPage.d.ts +22 -0
- package/dist/types/Book/page-index.d.ts +13 -0
- package/dist/types/Curl/Curl.d.ts +140 -0
- package/dist/types/Curl/webgl/CurlWebglLayer.d.ts +53 -0
- package/dist/types/Curl/webgl/glRenderer.d.ts +64 -0
- package/dist/types/Curl/webgl/pool.d.ts +40 -0
- package/dist/types/Curl/webgl/poolContext.d.ts +11 -0
- package/dist/types/Curl/webgl/snapshot.d.ts +16 -0
- package/dist/types/CurlFace/CurlFace.d.ts +43 -0
- package/dist/types/flip/animator.d.ts +64 -0
- package/dist/types/flip/coneDeformer.d.ts +55 -0
- package/dist/types/flip/drag.d.ts +152 -0
- package/dist/types/flip/geometry.d.ts +101 -0
- package/dist/types/flip/useCurlController.d.ts +66 -0
- package/dist/types/index.d.mts +9 -0
- package/dist/types/index.d.ts +9 -0
- package/package.json +58 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { type BoxProps, type Factory, type MantineColor, type StylesApiProps } from '@mantine/core';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { CurlFace, type CurlFaceAlign } from '../CurlFace/CurlFace';
|
|
4
|
+
import { type TurnOrigin } from '../flip/geometry';
|
|
5
|
+
export type CurlStylesNames = 'root' | 'restSheet' | 'curlSheet' | 'hardSheet' | 'shadowLayer' | 'revealLayer' | 'face';
|
|
6
|
+
export type CurlCssVariables = {
|
|
7
|
+
root: '--curl-page-width' | '--curl-page-height' | '--curl-page-background' | '--curl-reveal-background' | '--curl-shadow-color';
|
|
8
|
+
};
|
|
9
|
+
export interface CurlBaseProps {
|
|
10
|
+
/** Page width in CSS px. The play-zone is twice this. @default 300 */
|
|
11
|
+
width?: number;
|
|
12
|
+
/** Page height in CSS px. @default 600 */
|
|
13
|
+
height?: number;
|
|
14
|
+
/** Default content alignment for both faces. @default center / center */
|
|
15
|
+
align?: CurlFaceAlign;
|
|
16
|
+
/** Maximum opacity of the curl shadows. @default 0.5 */
|
|
17
|
+
shadowOpacity?: number;
|
|
18
|
+
/** Color used for the curl shadows. @default 'dark.9' */
|
|
19
|
+
shadowColor?: MantineColor | string;
|
|
20
|
+
/** Background color applied to each face. @default 'white' */
|
|
21
|
+
pageBackground?: MantineColor | string;
|
|
22
|
+
/**
|
|
23
|
+
* Layer painted under this page, shown through the area the curl uncovers.
|
|
24
|
+
* When unset no layer is rendered and the uncovered region shows whatever
|
|
25
|
+
* sits beneath (the next page in a Book, or the Book's inside cover).
|
|
26
|
+
*/
|
|
27
|
+
revealBackground?: MantineColor | string;
|
|
28
|
+
/**
|
|
29
|
+
* Curl radius in px for `variant="rounded"` — smaller wraps tighter and turns
|
|
30
|
+
* the page sooner; larger is a gentler, looser curl. @default ~0.32 × width
|
|
31
|
+
*/
|
|
32
|
+
curlRadius?: number;
|
|
33
|
+
/** Disable the drag interaction entirely (resting only). @default false */
|
|
34
|
+
disabled?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* `variant="rounded"` only: keep the face snapshots (the WebGL textures)
|
|
37
|
+
* captured eagerly at rest, so a fold starts with its textures ready. The
|
|
38
|
+
* Book sets this on the two top-of-stack pages and clears it on buried
|
|
39
|
+
* ones — a big book pays for one warm spread instead of a snapshot per
|
|
40
|
+
* page. When `false` the capture runs on demand as the fold starts (the
|
|
41
|
+
* flat fold shows until the first WebGL frame lands). @default true
|
|
42
|
+
*/
|
|
43
|
+
warmSnapshots?: boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Turn this page RIGID, like a hard cover: the whole sheet rotates flat
|
|
46
|
+
* around the spine (3D, under the play-zone perspective) instead of
|
|
47
|
+
* curling. Dragging, thresholds, swipes and callbacks behave exactly like
|
|
48
|
+
* a soft page — only the rendering differs (no fold, no WebGL). Inside a
|
|
49
|
+
* Book, `withCover` marks the first and last pages hard. @default false
|
|
50
|
+
*/
|
|
51
|
+
hard?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Controlled resting side: `false` rests in the right half, `true` in the
|
|
54
|
+
* left half (turned). When set, external changes run an animated turn onto
|
|
55
|
+
* the new side; use together with `onFlip` to own the state (the Book does
|
|
56
|
+
* this for each page).
|
|
57
|
+
*/
|
|
58
|
+
flipped?: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Pointer surface used to start a fold. `'play-zone'` (default) grabs from
|
|
61
|
+
* anywhere in the 2×W play-zone; `'sheet'` only from the resting page
|
|
62
|
+
* itself — inside a Book this is what routes the right half to the current
|
|
63
|
+
* page and the left half to the previously turned one.
|
|
64
|
+
* @default 'play-zone'
|
|
65
|
+
*/
|
|
66
|
+
grabZone?: 'play-zone' | 'sheet';
|
|
67
|
+
/**
|
|
68
|
+
* Where a programmatic (controlled) turn pretends to grab the free edge:
|
|
69
|
+
* `'bottom'` / `'top'` curl from that corner with a diagonal crease,
|
|
70
|
+
* `'middle'` folds the page straight over. Only affects turns driven by the
|
|
71
|
+
* `flipped` / `page` state — a real drag always follows the pointer.
|
|
72
|
+
* @default 'bottom'
|
|
73
|
+
*/
|
|
74
|
+
turnOrigin?: TurnOrigin;
|
|
75
|
+
/** Duration in ms of the settle animation after release. @default 600 */
|
|
76
|
+
flippingTime?: number;
|
|
77
|
+
/** Progress (0–100) at/above which a release completes the flip to B. @default 50 */
|
|
78
|
+
flipThreshold?: number;
|
|
79
|
+
/** Distance in px a swipe must cover to trigger a flip. @default 30 */
|
|
80
|
+
swipeDistance?: number;
|
|
81
|
+
/** Max duration in ms for a fast gesture to still count as a swipe. @default 250 */
|
|
82
|
+
swipeTimeThreshold?: number;
|
|
83
|
+
/** On touch, wait for a horizontal-bias gesture before claiming the drag. @default true */
|
|
84
|
+
mobileScrollSupport?: boolean;
|
|
85
|
+
/** Called as the fold changes (during drag and during the settle animation). */
|
|
86
|
+
onFold?: (info: {
|
|
87
|
+
progress: number;
|
|
88
|
+
phase: 'grab' | 'move' | 'settle';
|
|
89
|
+
}) => void;
|
|
90
|
+
/** Called once when a release settle finishes, with the resting state. */
|
|
91
|
+
onFlip?: (info: {
|
|
92
|
+
flipped: boolean;
|
|
93
|
+
}) => void;
|
|
94
|
+
/** Faces — Book.Page.Front and the optional Book.Page.Back. */
|
|
95
|
+
children?: React.ReactNode;
|
|
96
|
+
}
|
|
97
|
+
export interface CurlProps extends BoxProps, CurlBaseProps, StylesApiProps<CurlFactory> {
|
|
98
|
+
}
|
|
99
|
+
export type CurlFactory = Factory<{
|
|
100
|
+
props: CurlProps;
|
|
101
|
+
ref: HTMLDivElement;
|
|
102
|
+
stylesNames: CurlStylesNames;
|
|
103
|
+
vars: CurlCssVariables;
|
|
104
|
+
/**
|
|
105
|
+
* Curl renderer. `'flat'` is the DOM reflection fold (default, fully
|
|
106
|
+
* interactive at rest, the universal fallback); `'rounded'` is the true 3D
|
|
107
|
+
* WebGL curl — a crease-aligned cylinder wrap (the faces are a static
|
|
108
|
+
* snapshot during the curl, live at rest).
|
|
109
|
+
*/
|
|
110
|
+
variant: 'flat' | 'rounded';
|
|
111
|
+
staticComponents: {
|
|
112
|
+
Front: typeof CurlFace;
|
|
113
|
+
Back: typeof CurlFace;
|
|
114
|
+
};
|
|
115
|
+
}>;
|
|
116
|
+
export declare const Curl: import("@mantine/core").MantineComponent<{
|
|
117
|
+
props: CurlProps;
|
|
118
|
+
ref: HTMLDivElement;
|
|
119
|
+
stylesNames: CurlStylesNames;
|
|
120
|
+
vars: CurlCssVariables;
|
|
121
|
+
/**
|
|
122
|
+
* Curl renderer. `'flat'` is the DOM reflection fold (default, fully
|
|
123
|
+
* interactive at rest, the universal fallback); `'rounded'` is the true 3D
|
|
124
|
+
* WebGL curl — a crease-aligned cylinder wrap (the faces are a static
|
|
125
|
+
* snapshot during the curl, live at rest).
|
|
126
|
+
*/
|
|
127
|
+
variant: "flat" | "rounded";
|
|
128
|
+
staticComponents: {
|
|
129
|
+
Front: typeof CurlFace;
|
|
130
|
+
Back: typeof CurlFace;
|
|
131
|
+
};
|
|
132
|
+
}>;
|
|
133
|
+
/**
|
|
134
|
+
* Two distinct marker components (each its own function object so the
|
|
135
|
+
* `__curlSlot` tag and `displayName` don't collide). Both render nothing —
|
|
136
|
+
* `Curl` reads their props via `React.Children`. Typed as `CurlFace` so the
|
|
137
|
+
* factory's `staticComponents` signature is satisfied. Exported so `Book.Page`
|
|
138
|
+
* can mint its own `Front`/`Back` markers with proper display names.
|
|
139
|
+
*/
|
|
140
|
+
export declare function makeFaceMarker(slot: 'front' | 'back', name: string): typeof CurlFace;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react';
|
|
2
|
+
import type { ReflectionFold } from '../../flip/geometry';
|
|
3
|
+
export interface CurlWebglLayerProps {
|
|
4
|
+
/** Sheet width in px (play-zone is 2×). */
|
|
5
|
+
width: number;
|
|
6
|
+
/** Sheet height in px. */
|
|
7
|
+
height: number;
|
|
8
|
+
/** True while a fold is active (dragging or settling) — shows the canvas. */
|
|
9
|
+
active: boolean;
|
|
10
|
+
/** The active reflection fold (crease + geometry) that drives the curl. */
|
|
11
|
+
fold: ReflectionFold | null;
|
|
12
|
+
/** Which side the sheet rests on (false = right, true = left). */
|
|
13
|
+
flipped: boolean;
|
|
14
|
+
/** Curl radius in px (smaller = tighter wrap / turns sooner). */
|
|
15
|
+
curlRadius: number;
|
|
16
|
+
/** Self-shadow strength (0–1) shading the curl as it curves away. */
|
|
17
|
+
shadowOpacity: number;
|
|
18
|
+
/** Resolved CSS color the self-shadow tints toward (= shadowColor). */
|
|
19
|
+
shadowColor?: string;
|
|
20
|
+
/** Page background painted behind each captured face. */
|
|
21
|
+
pageBackground?: string;
|
|
22
|
+
/** Front / back face content — rendered off-screen only as snapshot sources. */
|
|
23
|
+
frontContent: ReactNode;
|
|
24
|
+
backContent: ReactNode;
|
|
25
|
+
/**
|
|
26
|
+
* Keep the face snapshots warm: capture eagerly at rest so a fold starts
|
|
27
|
+
* with its textures ready. The Book enables this only on the two
|
|
28
|
+
* top-of-stack pages (the only ones a pointer can grab — and, during a
|
|
29
|
+
* riffle, the page about to step), so a 100-page book pays for 2 snapshots,
|
|
30
|
+
* not 200. A cold page captures on demand when its fold starts; until that
|
|
31
|
+
* frame lands the parent keeps the flat fold visible. @default true
|
|
32
|
+
*/
|
|
33
|
+
warm?: boolean;
|
|
34
|
+
/** Called when WebGL is unavailable or a snapshot taints — caller falls back. */
|
|
35
|
+
onUnavailable?: () => void;
|
|
36
|
+
/**
|
|
37
|
+
* Reports whether the canvas holds a drawn frame for the CURRENT fold. The
|
|
38
|
+
* parent keeps the DOM resting sheet visible until `true`, so the page
|
|
39
|
+
* beneath never flashes through in the gap before the first WebGL frame.
|
|
40
|
+
*/
|
|
41
|
+
onReadyChange?: (ready: boolean) => void;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* The opt-in WebGL "rounded" curl layer. Keeps the live faces as off-screen DOM
|
|
45
|
+
* (interactive at rest in the parent) and snapshots them to plain images. The
|
|
46
|
+
* GPU side is a SHARED pool ({@link CurlWebglPool}): inside a Book every page
|
|
47
|
+
* borrows the same renderer/canvas while it folds (one context per book, not
|
|
48
|
+
* per page); a standalone page owns a local pool-of-one. The canvas element is
|
|
49
|
+
* adopted into this layer's positioned mount while the fold is active and
|
|
50
|
+
* parked on release. Client-only; the parent lazy-mounts it so SSR / flat-mode
|
|
51
|
+
* users pay nothing.
|
|
52
|
+
*/
|
|
53
|
+
export declare function CurlWebglLayer(props: CurlWebglLayerProps): React.JSX.Element;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
type TexImageSource = HTMLImageElement | HTMLCanvasElement | ImageBitmap;
|
|
2
|
+
export declare class CurlGlRenderer {
|
|
3
|
+
private gl;
|
|
4
|
+
private program;
|
|
5
|
+
private vao;
|
|
6
|
+
private posBuf;
|
|
7
|
+
private normBuf;
|
|
8
|
+
private frontTex;
|
|
9
|
+
private backTex;
|
|
10
|
+
private hasBack;
|
|
11
|
+
private readonly cols;
|
|
12
|
+
private readonly rows;
|
|
13
|
+
private readonly texcoords;
|
|
14
|
+
private readonly indices;
|
|
15
|
+
private readonly scaledPos;
|
|
16
|
+
private readonly normals;
|
|
17
|
+
private W;
|
|
18
|
+
private H;
|
|
19
|
+
/** Vertical headroom (px each side) so the curl can extend past the page. */
|
|
20
|
+
private padY;
|
|
21
|
+
constructor(canvas: HTMLCanvasElement, cols?: number, rows?: number);
|
|
22
|
+
/** Vertical headroom as a fraction of the sheet height (each side). */
|
|
23
|
+
static readonly PAD_RATIO = 0.18;
|
|
24
|
+
/** Size the drawing buffer to the play-zone (2W × (H + 2·pad)) at the given DPR. */
|
|
25
|
+
resize(sheetWidth: number, sheetHeight: number, dpr: number): void;
|
|
26
|
+
/**
|
|
27
|
+
* Clear the drawing buffer to transparent. Called on every lease acquire:
|
|
28
|
+
* the pooled canvas still holds the LAST page's final frame, and without
|
|
29
|
+
* the clear it would show through for the gap before this fold's first
|
|
30
|
+
* draw (the resize above no longer implicitly clears when sizes match).
|
|
31
|
+
*/
|
|
32
|
+
clear(): void;
|
|
33
|
+
private upload;
|
|
34
|
+
setFront(source: TexImageSource): void;
|
|
35
|
+
setBack(source: TexImageSource | null): void;
|
|
36
|
+
/** Recompute per-vertex smooth normals from the scaled positions. */
|
|
37
|
+
private computeNormals;
|
|
38
|
+
/**
|
|
39
|
+
* Render the curl by wrapping the page around the CREASE line (cylinder model,
|
|
40
|
+
* kivy/Hung). The crease — its midpoint and normal — comes straight from the
|
|
41
|
+
* reflection fold, so the curl follows the drag in EVERY direction (horizontal,
|
|
42
|
+
* vertical, diagonal) with the correct sign, and there is no fixed-axis spike.
|
|
43
|
+
*
|
|
44
|
+
* @param creaseMidX page-coord crease midpoint x (spine at x=0)
|
|
45
|
+
* @param creaseMidY page-coord crease midpoint y
|
|
46
|
+
* @param nx,ny crease normal (unit), pointing toward the grabbed edge:
|
|
47
|
+
* page points with positive distance along it are the flap
|
|
48
|
+
* that wraps; the spine side stays flat.
|
|
49
|
+
* @param radius curl radius in px (larger = gentler wrap)
|
|
50
|
+
* @param sheetLeft page x of the sheet's spine edge: 0 at rest, −W when flipped
|
|
51
|
+
* @param shadowStrength 0–1 self-shadow strength shading the curl as it curves away
|
|
52
|
+
* @param shadowColor RGB (0–1) the self-shadow tints toward (= shadowColor); default near-black
|
|
53
|
+
*/
|
|
54
|
+
render(creaseMidX: number, creaseMidY: number, nx: number, ny: number, radius: number, sheetLeft: number, shadowStrength: number, shadowColor?: readonly [number, number, number]): void;
|
|
55
|
+
dispose(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Full teardown: drop the GL objects AND return the context slot to the
|
|
58
|
+
* browser. Safe here (unlike a React effect cleanup) because the pool owns
|
|
59
|
+
* BOTH the renderer and its canvas — a recreated pool gets a fresh canvas,
|
|
60
|
+
* so StrictMode double-mounts never reuse a lost context.
|
|
61
|
+
*/
|
|
62
|
+
loseContext(): void;
|
|
63
|
+
}
|
|
64
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { CurlGlRenderer } from './glRenderer';
|
|
2
|
+
/** What a folding page borrows from the pool. */
|
|
3
|
+
export interface CurlGlLease {
|
|
4
|
+
renderer: CurlGlRenderer;
|
|
5
|
+
canvas: HTMLCanvasElement;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* One WebGL2 context for a whole Book.
|
|
9
|
+
*
|
|
10
|
+
* Every rounded page used to own a live context for its entire lifetime, so a
|
|
11
|
+
* long book exhausted the browser's context budget (~16 in Chrome) and later
|
|
12
|
+
* pages silently degraded to flat. The Book's turn queue guarantees a single
|
|
13
|
+
* page folds at a time, so ONE shared renderer is enough by construction:
|
|
14
|
+
* pages keep their face snapshots as plain images (CPU) and upload them to
|
|
15
|
+
* the pool's two GL textures when they acquire the lease for a fold.
|
|
16
|
+
*
|
|
17
|
+
* The renderer and its canvas are created lazily on the first acquire (an
|
|
18
|
+
* untouched book costs zero GPU contexts) and reclaimed with
|
|
19
|
+
* `WEBGL_lose_context` on {@link dispose} (Book unmount).
|
|
20
|
+
*/
|
|
21
|
+
export declare class CurlWebglPool {
|
|
22
|
+
private readonly createRenderer;
|
|
23
|
+
private renderer;
|
|
24
|
+
private canvas;
|
|
25
|
+
private owner;
|
|
26
|
+
private failed;
|
|
27
|
+
constructor(createRenderer?: (canvas: HTMLCanvasElement) => CurlGlRenderer);
|
|
28
|
+
/**
|
|
29
|
+
* Borrow the shared renderer for one folding page. Last-wins: a new owner
|
|
30
|
+
* steals the lease (the queue makes overlap impossible in practice; the
|
|
31
|
+
* previous holder's `release` then becomes a no-op). Returns `null` when
|
|
32
|
+
* WebGL is unavailable — latched, so the caller can fall back to flat.
|
|
33
|
+
*/
|
|
34
|
+
acquire(owner: symbol): CurlGlLease | null;
|
|
35
|
+
/** Return the lease. Parks the canvas out of the DOM so the previous page's
|
|
36
|
+
* last frame can never flash under the next fold. No-op for stale owners. */
|
|
37
|
+
release(owner: symbol): void;
|
|
38
|
+
/** True teardown (Book unmount): reclaim the GPU context slot. */
|
|
39
|
+
dispose(): void;
|
|
40
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { CurlWebglPool } from './pool';
|
|
2
|
+
/**
|
|
3
|
+
* Mutable holder shared between a Book and its pages' WebGL layers. The Book
|
|
4
|
+
* provides an empty holder; the first rounded layer that needs to draw fills
|
|
5
|
+
* `pool` (lazy import path); the Book disposes it on unmount.
|
|
6
|
+
*/
|
|
7
|
+
export interface CurlWebglPoolHolder {
|
|
8
|
+
pool: CurlWebglPool | null;
|
|
9
|
+
}
|
|
10
|
+
/** `null` outside a Book — a standalone page then owns a local pool-of-one. */
|
|
11
|
+
export declare const CurlWebglPoolContext: import("react").Context<CurlWebglPoolHolder | null>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capture a live DOM node into an <img> usable as a WebGL texture.
|
|
3
|
+
*
|
|
4
|
+
* Uses snapdom — it renders element backgrounds, shadows and pseudo-elements
|
|
5
|
+
* more faithfully than html-to-image, and emits an origin-clean data-URL image,
|
|
6
|
+
* so the result uploads via `texImage2D` without tainting the GL context.
|
|
7
|
+
* Imported lazily so it never enters the SSR / initial bundle; only the opt-in
|
|
8
|
+
* `variant="rounded"` path loads it.
|
|
9
|
+
*
|
|
10
|
+
* `embedFonts: false` skips non-icon web-font inlining (which would read
|
|
11
|
+
* cross-origin stylesheet cssRules and throw); text falls back to a system font
|
|
12
|
+
* in the snapshot, which is fine for a page mid-curl.
|
|
13
|
+
*
|
|
14
|
+
* Returns `null` if capture fails — the caller then falls back to the DOM curl.
|
|
15
|
+
*/
|
|
16
|
+
export declare function captureFaceTexture(node: HTMLElement, pixelRatio: number, backgroundColor?: string): Promise<HTMLImageElement | null>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type React from 'react';
|
|
2
|
+
import type { ReactNode } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Per-face content alignment. Resolves to flex `justify-content` /
|
|
5
|
+
* `align-items` on the face wrapper. Smaller-than-page content is placed
|
|
6
|
+
* according to these; larger content overflows and is clipped.
|
|
7
|
+
*/
|
|
8
|
+
export interface CurlFaceAlign {
|
|
9
|
+
/** Horizontal placement of content smaller than the face. @default 'center' */
|
|
10
|
+
horizontal?: 'start' | 'center' | 'end';
|
|
11
|
+
/** Vertical placement of content smaller than the face. @default 'center' */
|
|
12
|
+
vertical?: 'start' | 'center' | 'end';
|
|
13
|
+
}
|
|
14
|
+
export interface CurlFaceProps {
|
|
15
|
+
/**
|
|
16
|
+
* Content alignment for this face, overriding the `align` set on the
|
|
17
|
+
* parent `<Curl>`. Defaults to center / center.
|
|
18
|
+
*/
|
|
19
|
+
align?: CurlFaceAlign;
|
|
20
|
+
/** Face contents. */
|
|
21
|
+
children?: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Declarative descriptor for one face of a `<Curl>` sheet.
|
|
25
|
+
*
|
|
26
|
+
* `CurlFace` is a **marker component**: it renders nothing on its own.
|
|
27
|
+
* The parent `Curl` reads its props (`children`, `align`) through
|
|
28
|
+
* `React.Children` and renders the content into the curl layers itself —
|
|
29
|
+
* so the React node (and its event handlers) is rendered exactly once and
|
|
30
|
+
* never cloned via `innerHTML`.
|
|
31
|
+
*
|
|
32
|
+
* <Curl>
|
|
33
|
+
* <Curl.Front>Front side</Curl.Front>
|
|
34
|
+
* <Curl.Back>Back side</Curl.Back>
|
|
35
|
+
* </Curl>
|
|
36
|
+
*
|
|
37
|
+
* `Curl.Back` is optional — when omitted the back of the sheet renders as
|
|
38
|
+
* a blank face filled with the page background.
|
|
39
|
+
*/
|
|
40
|
+
export declare function CurlFace(_props: CurlFaceProps): React.ReactElement | null;
|
|
41
|
+
export declare namespace CurlFace {
|
|
42
|
+
var displayName: string;
|
|
43
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `requestAnimationFrame`-driven lerp animator for click-to-flip page
|
|
3
|
+
* turns. Pure-by-default: easing functions are named exports and unit
|
|
4
|
+
* tested without a DOM; the React hook (`useFlipAnimator`) is a thin
|
|
5
|
+
* wrapper that owns a single rAF loop and cancels cleanly on unmount.
|
|
6
|
+
*
|
|
7
|
+
* The book is **quiescent at rest** — there's no perpetual rAF loop
|
|
8
|
+
* (unlike `Scene.Globe`'s auto-rotate). Animation kicks in only when
|
|
9
|
+
* the consumer calls `start({...})` from `onRelease` or from a
|
|
10
|
+
* programmatic `flipNext()` call.
|
|
11
|
+
*/
|
|
12
|
+
/** Easing function signature — input in `[0, 1]`, output in `[0, 1]`. */
|
|
13
|
+
export type Easing = (t: number) => number;
|
|
14
|
+
/** Constant-rate easing — useful for very short flips. */
|
|
15
|
+
export declare const linear: Easing;
|
|
16
|
+
/** Cubic ease-out — `t === 0` → 0, `t === 1` → 1, gentle landing. Default for click-to-flip. */
|
|
17
|
+
export declare const easeOutCubic: Easing;
|
|
18
|
+
/** Cubic ease-in-out — symmetric S-curve. */
|
|
19
|
+
export declare const easeInOutCubic: Easing;
|
|
20
|
+
/** Inputs to a single `start(...)` invocation. */
|
|
21
|
+
export interface AnimateOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Animation length in ms. If `0` the animation completes synchronously
|
|
24
|
+
* on the next frame (still passes through `onProgress(1, 1)`).
|
|
25
|
+
*
|
|
26
|
+
* @default 1000
|
|
27
|
+
*/
|
|
28
|
+
duration?: number;
|
|
29
|
+
/**
|
|
30
|
+
* Easing function applied to raw `t = elapsed / duration`. Result is
|
|
31
|
+
* passed to `onProgress` as the first argument.
|
|
32
|
+
*
|
|
33
|
+
* @default easeOutCubic
|
|
34
|
+
*/
|
|
35
|
+
easing?: Easing;
|
|
36
|
+
/**
|
|
37
|
+
* Called on every animation frame with both the eased value and the
|
|
38
|
+
* raw linear `t`. Use either depending on what you're driving.
|
|
39
|
+
*/
|
|
40
|
+
onProgress: (eased: number, t: number) => void;
|
|
41
|
+
/** Called once when the animation reaches `t === 1`. */
|
|
42
|
+
onComplete?: () => void;
|
|
43
|
+
}
|
|
44
|
+
/** Object returned by `useFlipAnimator`. All methods are referentially stable. */
|
|
45
|
+
export interface FlipAnimator {
|
|
46
|
+
/**
|
|
47
|
+
* Starts a new animation. If a previous animation is still running,
|
|
48
|
+
* it is cancelled first — only one animation can be active at a time.
|
|
49
|
+
*
|
|
50
|
+
* Returns a `stop` function for the caller's convenience, in addition
|
|
51
|
+
* to the controller-level `stop()`.
|
|
52
|
+
*/
|
|
53
|
+
start: (options: AnimateOptions) => () => void;
|
|
54
|
+
/** Cancels the running animation (no-op when none is active). */
|
|
55
|
+
stop: () => void;
|
|
56
|
+
/** True while the rAF loop is scheduled. */
|
|
57
|
+
isActive: () => boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Single-loop rAF animator. Internally there is at most **one** scheduled
|
|
61
|
+
* frame at any time — calling `start()` while an animation is running
|
|
62
|
+
* cancels the old one and seamlessly takes over.
|
|
63
|
+
*/
|
|
64
|
+
export declare function useFlipAnimator(): FlipAnimator;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cone page-curl deformer — the Hong et al. "Deforming Pages of Electronic
|
|
3
|
+
* Books" cone model, ported from prideout/pageturn (Apache-2.0). Pure math: it
|
|
4
|
+
* maps a flat unit page mesh (texcoords u, v ∈ [0, 1]) onto the curled 3D cone
|
|
5
|
+
* surface and writes the `positions`. No WebGL, no DOM.
|
|
6
|
+
*
|
|
7
|
+
* STATUS: only {@link buildConeMesh} (the static grid) is used by the
|
|
8
|
+
* shipping renderer. The deform path ({@link deformConeMesh} / {@link coneParams})
|
|
9
|
+
* is NOT wired — glRenderer.ts uses a crease-aligned CYLINDER wrap instead.
|
|
10
|
+
* This module is kept (and unit-tested) as a possible future asymmetric
|
|
11
|
+
* "dog-ear" model.
|
|
12
|
+
*
|
|
13
|
+
* The cone apex sits far down −v, so curvature grows with distance from the
|
|
14
|
+
* binding: the page wraps tightest near the spine and the far corner peels up —
|
|
15
|
+
* the asymmetric "dog-ear" iBooks look. Unlike prideout's canned t-animation,
|
|
16
|
+
* our curl is DRAG-driven, so {@link coneParams} maps our 0–100 fold progress
|
|
17
|
+
* straight to (theta, apex, rotation); the release easing is done upstream by
|
|
18
|
+
* useFlipAnimator.
|
|
19
|
+
*/
|
|
20
|
+
/** Apex offset scale (prideout's −15 for a unit page: the apex sits far down −v). */
|
|
21
|
+
export declare const CONE_APEX_SCALE = 15;
|
|
22
|
+
export interface ConeParams {
|
|
23
|
+
/** Cone half-angle: 0 = flat, π/2 = fully wrapped to the other side. */
|
|
24
|
+
theta: number;
|
|
25
|
+
/** Cone apex position along −v (negative; further = gentler curl). */
|
|
26
|
+
apex: number;
|
|
27
|
+
/** Rigid rotation (radians) applied to the whole page after deforming. */
|
|
28
|
+
rotation: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Map fold progress (0–100) to cone parameters. progress 0 → flat, 100 → a full
|
|
32
|
+
* turn onto the other side. Linear in the deformation amount — the settle
|
|
33
|
+
* easing is applied upstream (the animator feeds an already-eased progress).
|
|
34
|
+
*/
|
|
35
|
+
export declare function coneParams(progress: number): ConeParams;
|
|
36
|
+
/**
|
|
37
|
+
* Deform a flat unit page mesh into the cone surface, in place. Verbatim Hong
|
|
38
|
+
* cone math (per vertex): (u, v) maps onto a cone of half-angle `theta` whose
|
|
39
|
+
* apex is at (0, apex). At `theta ≤ MIN_THETA` the page is flat (z = 0). The
|
|
40
|
+
* `−0.5` on y centres the unit page on its v axis (prideout's convention).
|
|
41
|
+
*
|
|
42
|
+
* @param positions Float32Array of 3·N floats (xyz per vertex) — written here.
|
|
43
|
+
* @param texcoords Float32Array of 2·N floats (uv per vertex) — read here.
|
|
44
|
+
*/
|
|
45
|
+
export declare function deformConeMesh(positions: Float32Array, texcoords: Float32Array, theta: number, apex: number): void;
|
|
46
|
+
/**
|
|
47
|
+
* Build the flat unit page mesh used by {@link deformConeMesh}: a
|
|
48
|
+
* (cols+1)×(rows+1) grid of texcoords (u, v ∈ [0, 1]) plus the matching
|
|
49
|
+
* triangle index buffer. Returns typed arrays sized for the WebGL renderer.
|
|
50
|
+
*/
|
|
51
|
+
export declare function buildConeMesh(cols: number, rows: number): {
|
|
52
|
+
texcoords: Float32Array;
|
|
53
|
+
indices: Uint16Array;
|
|
54
|
+
vertexCount: number;
|
|
55
|
+
};
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pointer drag controller for the page-turn gesture.
|
|
3
|
+
*
|
|
4
|
+
* Splits cleanly into two layers:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Pure helpers** (named exports) — no React, no DOM:
|
|
7
|
+
* - `computeReleaseVelocity(samples)`
|
|
8
|
+
* - `classifyGesture({ delta, duration, swipeDistance, swipeTimeThreshold })`
|
|
9
|
+
* - `pruneSampleWindow(samples, now, windowMs)`
|
|
10
|
+
*
|
|
11
|
+
* These are unit-tested in isolation and reused by both `useDragController`
|
|
12
|
+
* and (later) the touch path on the spread layout component.
|
|
13
|
+
*
|
|
14
|
+
* 2. **`useDragController` hook** — wires React `useRef` + PointerEvent
|
|
15
|
+
* handlers around the pure helpers. Returns ready-to-spread handlers
|
|
16
|
+
* `{ onPointerDown, onPointerMove, onPointerUp, onPointerCancel }`
|
|
17
|
+
* plus a stable `isDragging()` getter.
|
|
18
|
+
*
|
|
19
|
+
* Design notes:
|
|
20
|
+
*
|
|
21
|
+
* - The rolling 60ms window pattern is the same one validated on
|
|
22
|
+
* `Scene.Globe` (see mantine-scene `SceneGlobe.tsx`): release velocity
|
|
23
|
+
* is averaged over the very last 60ms of motion so that a brief pause
|
|
24
|
+
* before lifting the finger doesn't kill the inertia.
|
|
25
|
+
*
|
|
26
|
+
* - `mobileScrollHoldMs` makes touch start *passive* for the first ~250ms.
|
|
27
|
+
* Vertical scroll wins by default; only horizontal-biased motion claims
|
|
28
|
+
* the gesture as a drag. Matches StPageFlip's `UI.ts` heuristic.
|
|
29
|
+
*
|
|
30
|
+
* - We use `setPointerCapture` so the drag continues even when the cursor
|
|
31
|
+
* leaves the book bounds (a typical case when grabbing the bottom-right
|
|
32
|
+
* corner and dragging across the spine).
|
|
33
|
+
*/
|
|
34
|
+
import type { Point } from './geometry';
|
|
35
|
+
/** One sample of the pointer's position at a given timestamp. */
|
|
36
|
+
export interface DragSample {
|
|
37
|
+
/** Timestamp from `performance.now()` (ms). */
|
|
38
|
+
t: number;
|
|
39
|
+
/** Page-local X. */
|
|
40
|
+
x: number;
|
|
41
|
+
/** Page-local Y. */
|
|
42
|
+
y: number;
|
|
43
|
+
}
|
|
44
|
+
/** Three discrete outcomes of a single gesture, decided at release time. */
|
|
45
|
+
export type GestureKind = 'click' | 'swipe' | 'drag';
|
|
46
|
+
/** Summary handed to `onRelease` callbacks. */
|
|
47
|
+
export interface DragSummary {
|
|
48
|
+
/** What kind of gesture this turned out to be. */
|
|
49
|
+
kind: GestureKind;
|
|
50
|
+
/** Total time press → release (ms). */
|
|
51
|
+
duration: number;
|
|
52
|
+
/** Total displacement from press to release (px). */
|
|
53
|
+
delta: Point;
|
|
54
|
+
/** Final release point in page-local coords. */
|
|
55
|
+
releasePoint: Point;
|
|
56
|
+
/** Velocity at release in px/ms, averaged over the rolling window. */
|
|
57
|
+
velocity: Point;
|
|
58
|
+
}
|
|
59
|
+
export interface DragControllerOptions {
|
|
60
|
+
/**
|
|
61
|
+
* Minimum displacement (px) before a gesture is classified as
|
|
62
|
+
* `swipe` (when fast) or `drag` (when long).
|
|
63
|
+
*
|
|
64
|
+
* @default 30
|
|
65
|
+
*/
|
|
66
|
+
swipeDistance?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Max time (ms) a fast gesture may take to still count as a `swipe`.
|
|
69
|
+
*
|
|
70
|
+
* @default 250
|
|
71
|
+
*/
|
|
72
|
+
swipeTimeThreshold?: number;
|
|
73
|
+
/**
|
|
74
|
+
* On touch input, ignore the gesture until the user has dragged
|
|
75
|
+
* horizontally beyond `swipeDistance / 4`. This lets vertical scroll
|
|
76
|
+
* win when the gesture begins more-vertically than horizontally — the
|
|
77
|
+
* same heuristic StPageFlip uses.
|
|
78
|
+
*
|
|
79
|
+
* @default true
|
|
80
|
+
*/
|
|
81
|
+
mobileScrollSupport?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Rolling sample window (ms) used to estimate release velocity.
|
|
84
|
+
*
|
|
85
|
+
* @default 60
|
|
86
|
+
*/
|
|
87
|
+
velocityWindowMs?: number;
|
|
88
|
+
/** Called every pointermove while dragging. */
|
|
89
|
+
onMove?: (point: Point) => void;
|
|
90
|
+
/** Called once when a press starts (before drag/swipe classification). */
|
|
91
|
+
onStart?: (point: Point) => void;
|
|
92
|
+
/** Called on release with the gesture summary. */
|
|
93
|
+
onRelease?: (summary: DragSummary) => void;
|
|
94
|
+
/**
|
|
95
|
+
* Convert raw client coordinates from the PointerEvent into the
|
|
96
|
+
* page-local coordinate system the consumer wants to track. Defaults
|
|
97
|
+
* to no-op (the consumer is responsible for translating).
|
|
98
|
+
*/
|
|
99
|
+
getLocalPoint?: (clientX: number, clientY: number) => Point;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Drop samples older than `now - windowMs` (keeps at least 2 entries
|
|
103
|
+
* so the velocity calc never starves).
|
|
104
|
+
*/
|
|
105
|
+
export declare function pruneSampleWindow(samples: DragSample[], now: number, windowMs: number): DragSample[];
|
|
106
|
+
/**
|
|
107
|
+
* Estimates the release velocity (px / ms per axis) as the average over
|
|
108
|
+
* the rolling window. Returns `{x: 0, y: 0}` for ≤1 sample.
|
|
109
|
+
*
|
|
110
|
+
* Algorithm: take the first and last sample, divide displacement by the
|
|
111
|
+
* elapsed time. Steadier than the last-frame delta, which is often ~0 if
|
|
112
|
+
* the user paused before lifting (see Scene.Globe v2.2.1 fix).
|
|
113
|
+
*/
|
|
114
|
+
export declare function computeReleaseVelocity(samples: DragSample[]): Point;
|
|
115
|
+
/** Inputs to {@link classifyGesture} — all primitives so it's trivial to test. */
|
|
116
|
+
export interface ClassifyInput {
|
|
117
|
+
delta: Point;
|
|
118
|
+
duration: number;
|
|
119
|
+
swipeDistance: number;
|
|
120
|
+
swipeTimeThreshold: number;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Classifies a finished gesture as `click`, `swipe`, or `drag`.
|
|
124
|
+
*
|
|
125
|
+
* - **click**: hardly moved AND short duration (release within `swipeTimeThreshold`
|
|
126
|
+
* and total displacement under the swipe threshold).
|
|
127
|
+
* - **swipe**: moved at least `swipeDistance` AND released within
|
|
128
|
+
* `swipeTimeThreshold`.
|
|
129
|
+
* - **drag**: moved at least `swipeDistance` OR took longer than the swipe
|
|
130
|
+
* time. The page curl tracks the cursor in real time during a drag.
|
|
131
|
+
*/
|
|
132
|
+
export declare function classifyGesture(input: ClassifyInput): GestureKind;
|
|
133
|
+
/** The bundle of handlers + introspection returned by the hook. */
|
|
134
|
+
export interface DragController {
|
|
135
|
+
onPointerDown: (event: React.PointerEvent<HTMLElement>) => void;
|
|
136
|
+
onPointerMove: (event: React.PointerEvent<HTMLElement>) => void;
|
|
137
|
+
onPointerUp: (event: React.PointerEvent<HTMLElement>) => void;
|
|
138
|
+
onPointerCancel: (event: React.PointerEvent<HTMLElement>) => void;
|
|
139
|
+
/** True between a successful `pointerdown` and the matching `pointerup` / `pointercancel`. */
|
|
140
|
+
isDragging: () => boolean;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* React hook that converts raw PointerEvents into the gesture summary
|
|
144
|
+
* the book renderer needs. See the module header for the design model.
|
|
145
|
+
*
|
|
146
|
+
* The returned handlers are stable across renders — drop them directly
|
|
147
|
+
* on the gesture surface element:
|
|
148
|
+
*
|
|
149
|
+
* const drag = useDragController({ onStart, onMove, onRelease });
|
|
150
|
+
* return <div {...drag} />;
|
|
151
|
+
*/
|
|
152
|
+
export declare function useDragController(options?: DragControllerOptions): DragController;
|