@vysmo/flipbook 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/src/types.ts ADDED
@@ -0,0 +1,123 @@
1
+ import type { EasingFn } from "@vysmo/easings";
2
+
3
+ /**
4
+ * A single page. URL strings are decoded into images; `HTMLImageElement`
5
+ * / `HTMLCanvasElement` are used as-is. Rich-DOM pages are out of scope
6
+ * for v1 — pre-render to a canvas if you need them.
7
+ */
8
+ export type PageSource = string | HTMLImageElement | HTMLCanvasElement;
9
+
10
+ export type FlipbookAxis = "horizontal" | "vertical";
11
+
12
+ export interface FlipbookOptions {
13
+ /**
14
+ * Host element. The flipbook creates its WebGL canvas inside this
15
+ * container. Size the container via CSS — the canvas fills 100% of it.
16
+ */
17
+ container: HTMLElement;
18
+ /** Page sources. Order matters: index 0 is the cover unless `initialPage` says otherwise. */
19
+ pages: readonly PageSource[];
20
+ /** Starting page index in [0, pages.length). Default 0. */
21
+ initialPage?: number;
22
+ /**
23
+ * Curl axis. `"horizontal"` peels the right edge leftward like an
24
+ * English book (pinned spine on the left). `"vertical"` peels the
25
+ * bottom edge upward like a wall calendar (pinned binding at the top).
26
+ * Default `"horizontal"`.
27
+ */
28
+ axis?: FlipbookAxis;
29
+ /**
30
+ * Hinge tilt in radians, **added on top of the axis baseline** (0 for
31
+ * horizontal, π/2 for vertical). Tilts the curl line away from a clean
32
+ * vertical/horizontal sweep. Default 0.12.
33
+ */
34
+ tilt?: number;
35
+ /** Page-back colour passed straight to the page-curl transition. */
36
+ backColor?: readonly [number, number, number];
37
+ /** Flip duration in milliseconds. Default 900. */
38
+ flipDuration?: number;
39
+ /** Easing for the flip progress. Default `cubicInOut` from `@vysmo/easings`. */
40
+ ease?: EasingFn;
41
+ /** Wrap from last → first (and vice versa). Default false — books have ends. */
42
+ loop?: boolean;
43
+ /**
44
+ * Click anywhere to flip: left half = prev, right half = next (vertical
45
+ * axis: top half = prev, bottom half = next). Default true.
46
+ */
47
+ clickNavigation?: boolean;
48
+ /**
49
+ * Pointer-drag scrubbing: drag in the flip direction to peel the page
50
+ * mid-curl. Release past `dragCommitThreshold` commits; below reverts.
51
+ * Default true.
52
+ */
53
+ dragNavigation?: boolean;
54
+ /**
55
+ * Drag commit threshold in [0, 1]. Released drag past this progress
56
+ * commits the flip; below reverts. Default 0.5. Lower (0.3) makes
57
+ * flips "sticky" — easier to commit. Higher (0.7) requires a more
58
+ * deliberate drag.
59
+ */
60
+ dragCommitThreshold?: number;
61
+ /**
62
+ * Keyboard navigation: `ArrowRight`/`ArrowDown` → next, `ArrowLeft`/
63
+ * `ArrowUp` → prev, `Home`/`End` → first/last. Default true.
64
+ */
65
+ keyboardNavigation?: boolean;
66
+ /**
67
+ * Auto-advance pages on a timer. `true` uses default 4000ms interval;
68
+ * pass `{ intervalMs }` to set your own. Pauses while the user is
69
+ * dragging or while the page lacks focus. Off by default. Use
70
+ * `play()` / `pause()` on the handle to toggle at runtime.
71
+ */
72
+ autoplay?: boolean | { intervalMs: number };
73
+ /** Accessible label exposed via `aria-label`. Default `"Flipbook"`. */
74
+ ariaLabel?: string;
75
+ }
76
+
77
+ export interface FlipOptions {
78
+ /**
79
+ * Skip the curl animation and snap directly to the target page. Still
80
+ * emits `change` but not `flipstart` / `flipend`.
81
+ */
82
+ instant?: boolean;
83
+ }
84
+
85
+ export type FlipbookEvent = "change" | "flipstart" | "flipend";
86
+
87
+ export interface FlipbookHandle {
88
+ readonly current: number;
89
+ readonly length: number;
90
+ readonly isFlipping: boolean;
91
+ /**
92
+ * Resolves when every string / image source has finished decoding.
93
+ * Canvas sources resolve instantly. Safe to call `next()` etc. before
94
+ * this resolves — they queue until the first frame is ready.
95
+ */
96
+ readonly ready: Promise<void>;
97
+ /** Advance to the next page. No-op while a flip is already in flight. */
98
+ next(): Promise<void>;
99
+ /** Retreat to the previous page. No-op while a flip is already in flight. */
100
+ prev(): Promise<void>;
101
+ /** Flip directly to a specific index. No-op if already there. */
102
+ goTo(index: number, options?: FlipOptions): Promise<void>;
103
+ /**
104
+ * Drive the current flip's progress externally. `progress` in [0, 1]
105
+ * scrubs from the current page to the next. Useful for scroll-driven
106
+ * flipbooks: pipe `@vysmo/scroll` progress straight in. Calling `seek`
107
+ * cancels any in-flight tween.
108
+ *
109
+ * Reaching 1 commits the flip; reaching 0 reverts to the current page.
110
+ * Other values hold the page mid-curl.
111
+ */
112
+ seek(progress: number): void;
113
+ /** Start auto-advance. Idempotent — no-op if already playing. */
114
+ play(): void;
115
+ /** Stop auto-advance. Idempotent — no-op if already paused. */
116
+ pause(): void;
117
+ /** True when autoplay is active (started + not paused). */
118
+ readonly isPlaying: boolean;
119
+ on(event: "change", cb: (current: number, previous: number) => void): () => void;
120
+ on(event: "flipstart", cb: (from: number, to: number) => void): () => void;
121
+ on(event: "flipend", cb: (from: number, to: number) => void): () => void;
122
+ destroy(): void;
123
+ }