@cosmictraveler002/anim-kit 1.4.0 → 1.6.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.
Files changed (56) hide show
  1. package/README.md +256 -23
  2. package/dist/anim-kit.standalone.js +5 -5
  3. package/dist/anim-kit.standalone.js.map +1 -1
  4. package/dist/core/smooth-scroll.d.ts +29 -1
  5. package/dist/core/smooth-scroll.d.ts.map +1 -1
  6. package/dist/core/smooth-scroll.js +23 -3
  7. package/dist/core/smooth-scroll.js.map +1 -1
  8. package/dist/effects/horizontal-scroll.d.ts.map +1 -1
  9. package/dist/effects/horizontal-scroll.js +3 -0
  10. package/dist/effects/horizontal-scroll.js.map +1 -1
  11. package/dist/effects/ink-wipe.d.ts +41 -0
  12. package/dist/effects/ink-wipe.d.ts.map +1 -0
  13. package/dist/effects/ink-wipe.js +342 -0
  14. package/dist/effects/ink-wipe.js.map +1 -0
  15. package/dist/effects/scatter-text.d.ts.map +1 -1
  16. package/dist/effects/scatter-text.js +3 -0
  17. package/dist/effects/scatter-text.js.map +1 -1
  18. package/dist/effects/stacked-cards.d.ts.map +1 -1
  19. package/dist/effects/stacked-cards.js +3 -0
  20. package/dist/effects/stacked-cards.js.map +1 -1
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +2 -0
  24. package/dist/index.js.map +1 -1
  25. package/dist/three/coverflow-wheel.d.ts +26 -0
  26. package/dist/three/coverflow-wheel.d.ts.map +1 -0
  27. package/dist/three/coverflow-wheel.js +495 -0
  28. package/dist/three/coverflow-wheel.js.map +1 -0
  29. package/dist/three/dither-reveal.d.ts +36 -0
  30. package/dist/three/dither-reveal.d.ts.map +1 -0
  31. package/dist/three/dither-reveal.js +292 -0
  32. package/dist/three/dither-reveal.js.map +1 -0
  33. package/dist/three/index.d.ts +8 -0
  34. package/dist/three/index.d.ts.map +1 -1
  35. package/dist/three/index.js +4 -0
  36. package/dist/three/index.js.map +1 -1
  37. package/dist/three/tear-reveal.d.ts +54 -0
  38. package/dist/three/tear-reveal.d.ts.map +1 -0
  39. package/dist/three/tear-reveal.js +269 -0
  40. package/dist/three/tear-reveal.js.map +1 -0
  41. package/dist/three/wordmark-wave.d.ts +29 -0
  42. package/dist/three/wordmark-wave.d.ts.map +1 -0
  43. package/dist/three/wordmark-wave.js +461 -0
  44. package/dist/three/wordmark-wave.js.map +1 -0
  45. package/package.json +1 -1
  46. package/src/core/smooth-scroll.ts +44 -2
  47. package/src/effects/horizontal-scroll.ts +3 -0
  48. package/src/effects/ink-wipe.ts +409 -0
  49. package/src/effects/scatter-text.ts +3 -0
  50. package/src/effects/stacked-cards.ts +3 -0
  51. package/src/index.ts +3 -0
  52. package/src/three/coverflow-wheel.ts +569 -0
  53. package/src/three/dither-reveal.ts +365 -0
  54. package/src/three/index.ts +8 -0
  55. package/src/three/tear-reveal.ts +351 -0
  56. package/src/three/wordmark-wave.ts +517 -0
@@ -0,0 +1,409 @@
1
+ /**
2
+ * Ink wipe — a brush-stroke page transition.
3
+ *
4
+ * A full-screen ink sheet sweeps the viewport on a tilted, noise-torn edge
5
+ * with bristle spurs and flung flecks:
6
+ *
7
+ * cover() the sheet sweeps in one direction and swallows the page —
8
+ * swap your DOM (or navigate) while it sits at full cover;
9
+ * unveil() the sheet keeps going and exits the far side, so the new page
10
+ * arrives behind the same stroke that removed the old one.
11
+ *
12
+ * One continuous left→right stroke across both halves — the edge never
13
+ * reverses, which is what keeps the two halves reading as a single gesture
14
+ * instead of a wipe and its undo.
15
+ *
16
+ * The transition is also wired for multi-page sites: pass `links` to catch
17
+ * navigations (cover → leave), and the next page's `inkWipe()` finds the
18
+ * session flag, paints covered on first frame and unveils itself.
19
+ *
20
+ * Reduced motion: cover/unveil snap instantly (a solid sheet while covered,
21
+ * no brush detail) so navigation still never flashes the swap.
22
+ *
23
+ * Works without any 2D context (SSR, jsdom): cover/unveil resolve
24
+ * immediately and nothing paints — destroy() is always safe to call.
25
+ */
26
+ import { gsap, initGSAP } from "../core/gsap.js";
27
+ import { one, prefersReducedMotion } from "../core/util.js";
28
+ import type { CommonOptions, Destroy, TargetLike } from "../core/types.js";
29
+
30
+ export interface InkWipeOptions extends CommonOptions {
31
+ /** Overlay host — the canvas is appended here. @default document.body */
32
+ host?: TargetLike;
33
+ /** Ink colour. @default "#0b0b0b" */
34
+ color?: string;
35
+ /** Cover duration, ms. @default 550 */
36
+ coverMs?: number;
37
+ /** Unveil duration, ms. @default 650 */
38
+ unveilMs?: number;
39
+ /** GSAP ease for both halves. @default "power2.inOut" */
40
+ ease?: string;
41
+ /** Edge lean — fraction of viewport height the top edge leads by. @default 0.35 */
42
+ tilt?: number;
43
+ /** Noise amplitude on the edge, px. @default 44 */
44
+ rough?: number;
45
+ /** Longest bristle spur, px. @default 180 */
46
+ bristle?: number;
47
+ /**
48
+ * Selector for links that should run the wipe before navigating
49
+ * (intercepted clicks; the next page unveils itself via `sessionKey`).
50
+ */
51
+ links?: string;
52
+ /** sessionStorage key for the cross-page handoff. @default "ak-ink-wipe" */
53
+ sessionKey?: string;
54
+ /** Automatically unveil when the session flag says the page loaded covered. @default true */
55
+ autoUnveil?: boolean;
56
+ /** Overlay z-index. @default 9998 */
57
+ z?: number;
58
+ }
59
+
60
+ export interface InkWipeHandle {
61
+ /** Sweep the sheet across the page. Resolves at full cover. */
62
+ cover(): Promise<void>;
63
+ /** Sweep the sheet off the far side. Resolves when the page is clear. */
64
+ unveil(): Promise<void>;
65
+ /** Remove the canvas, listeners and link interception. */
66
+ destroy: Destroy;
67
+ }
68
+
69
+ /** Distinguish inkWipe("#host", {...}) from inkWipe({ ... }). */
70
+ const isTargetArg = (v: unknown): v is TargetLike =>
71
+ v === undefined ||
72
+ v === null ||
73
+ typeof v === "string" ||
74
+ (typeof Element !== "undefined" && v instanceof Element) ||
75
+ (typeof NodeList !== "undefined" && v instanceof NodeList) ||
76
+ Array.isArray(v);
77
+
78
+ /** jsdom reports a 2D context it cannot actually provide — probe safely. */
79
+ const hasCanvas2D = (): boolean => {
80
+ if (typeof document === "undefined") return false;
81
+ if (typeof navigator !== "undefined" && /jsdom/i.test(navigator.userAgent || "")) return false;
82
+ try {
83
+ const probe = document.createElement("canvas");
84
+ if (typeof probe.getContext !== "function") return false;
85
+ return !!probe.getContext("2d");
86
+ } catch {
87
+ return false;
88
+ }
89
+ };
90
+
91
+ /* Deterministic pseudo-random: same seed → same stroke every frame, so the
92
+ edge shape is stable while it travels instead of boiling. */
93
+ const hash2 = (x: number, y: number, seed: number): number => {
94
+ const s = Math.sin(x * 127.1 + y * 311.7 + seed * 74.7) * 43758.5453;
95
+ return s - Math.floor(s);
96
+ };
97
+ const smooth = (t: number) => t * t * (3 - 2 * t);
98
+ /** 2D value noise, -0.5..0.5. */
99
+ const noise2 = (x: number, y: number, seed: number): number => {
100
+ const xi = Math.floor(x), yi = Math.floor(y);
101
+ const xf = x - xi, yf = y - yi;
102
+ const u = smooth(xf), v = smooth(yf);
103
+ const a = hash2(xi, yi, seed), b = hash2(xi + 1, yi, seed);
104
+ const c = hash2(xi, yi + 1, seed), d = hash2(xi + 1, yi + 1, seed);
105
+ const top = a + (b - a) * u;
106
+ const bot = c + (d - c) * u;
107
+ return top + (bot - top) * v - 0.5;
108
+ };
109
+ /** Fractal edge profile along y — the torn, organic boundary. */
110
+ const fbmY = (y: number, seed: number, octaves = 4): number => {
111
+ let v = 0, amp = 0.55, freq = 1;
112
+ for (let i = 0; i < octaves; i++) {
113
+ v += amp * noise2(y * freq, i * 13.7, seed + i);
114
+ amp *= 0.52;
115
+ freq *= 2.13;
116
+ }
117
+ return v;
118
+ };
119
+
120
+ export function inkWipe(options: InkWipeOptions): InkWipeHandle;
121
+ export function inkWipe(target: TargetLike, options?: Partial<InkWipeOptions>): InkWipeHandle;
122
+ export function inkWipe(
123
+ targetOrOptions: InkWipeOptions | TargetLike,
124
+ maybeOptions: Partial<InkWipeOptions> = {},
125
+ ): InkWipeHandle {
126
+ initGSAP();
127
+
128
+ const options: InkWipeOptions = isTargetArg(targetOrOptions)
129
+ ? { ...maybeOptions, host: targetOrOptions ?? maybeOptions.host }
130
+ : targetOrOptions;
131
+
132
+ if (typeof document === "undefined") return inertHandle();
133
+
134
+ const {
135
+ color = "#0b0b0b",
136
+ coverMs = 550,
137
+ unveilMs = 650,
138
+ ease = "power2.inOut",
139
+ tilt = 0.35,
140
+ rough = 44,
141
+ bristle = 180,
142
+ links,
143
+ sessionKey = "ak-ink-wipe",
144
+ autoUnveil = true,
145
+ z = 9998,
146
+ } = options;
147
+
148
+ const host =
149
+ options.host === undefined ? document.body : one<HTMLElement>(options.host);
150
+ if (!host) return inertHandle();
151
+
152
+ const reduced = prefersReducedMotion() && !options.force;
153
+ const canvas = document.createElement("canvas");
154
+ canvas.className = "ak-ink";
155
+ canvas.setAttribute("aria-hidden", "true");
156
+ const ctx = hasCanvas2D() ? canvas.getContext("2d") : null;
157
+ if (!ctx) {
158
+ /* No painter (SSR/jsdom): the handle still sequences. */
159
+ return inertHandle();
160
+ }
161
+
162
+ /* Sweep state: sheet spans [trail, lead] in t-space; t 0→1 crosses the
163
+ viewport, ±1 parks it off either side. lead <= trail = nothing drawn. */
164
+ const state = { trail: 1, lead: -1 };
165
+ const seed = 1 + Math.floor(Math.random() * 1e6);
166
+ let w = 0, h = 0, dpr = 1;
167
+ let destroyed = false;
168
+ let current: gsap.core.Tween | null = null;
169
+ let pendingResolve: (() => void) | null = null;
170
+
171
+ /** Kill the in-flight sweep, settling its promise (never strand an await). */
172
+ const killSweep = () => {
173
+ const r = pendingResolve;
174
+ pendingResolve = null;
175
+ if (current) {
176
+ current.kill();
177
+ current = null;
178
+ }
179
+ r?.();
180
+ };
181
+
182
+ const pad = () => Math.max(w, h) * 0.5 + rough * 2 + bristle;
183
+ /** Edge x at sweep position t, y-coordinate y (tilt + noise applied). */
184
+ const edgeX = (t: number, y: number): number => {
185
+ const base = -pad() + t * (w + 2 * pad());
186
+ const lean = (0.5 - y / Math.max(1, h)) * h * tilt;
187
+ const n = fbmY((y / Math.max(1, h)) * 6 + t * 1.7, seed) * rough;
188
+ return base + lean + n;
189
+ };
190
+
191
+ const resize = () => {
192
+ dpr = Math.min(window.devicePixelRatio || 1, 2);
193
+ w = window.innerWidth;
194
+ h = window.innerHeight;
195
+ canvas.width = Math.max(2, Math.round(w * dpr));
196
+ canvas.height = Math.max(2, Math.round(h * dpr));
197
+ canvas.style.width = `${w}px`;
198
+ canvas.style.height = `${h}px`;
199
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
200
+ draw();
201
+ };
202
+
203
+ /** One bristle spur: a thin wedge reaching away from the sheet interior. */
204
+ const spur = (x: number, y: number, dir: 1 | -1, len: number, width: number) => {
205
+ ctx.beginPath();
206
+ ctx.moveTo(x, y - width / 2);
207
+ ctx.lineTo(x + dir * len, y - width * 0.18);
208
+ ctx.lineTo(x + dir * len * 0.96, y + width * 0.22);
209
+ ctx.lineTo(x, y + width / 2);
210
+ ctx.closePath();
211
+ ctx.fill();
212
+ };
213
+
214
+ const draw = () => {
215
+ ctx.clearRect(0, 0, w, h);
216
+ const { trail, lead } = state;
217
+ if (lead <= trail || w === 0) return;
218
+
219
+ ctx.fillStyle = color;
220
+
221
+ /* --- the sheet: a quad between the two edges, sampled along y --- */
222
+ const STEPS = Math.max(16, Math.min(64, Math.round(h / 18)));
223
+ ctx.beginPath();
224
+ for (let i = 0; i <= STEPS; i++) {
225
+ const y = (i / STEPS) * h;
226
+ const x = edgeX(lead, y);
227
+ i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
228
+ }
229
+ for (let i = STEPS; i >= 0; i--) {
230
+ const y = (i / STEPS) * h;
231
+ ctx.lineTo(edgeX(trail, y), y);
232
+ }
233
+ ctx.closePath();
234
+ ctx.fill();
235
+
236
+ /* --- bristle spurs + flung flecks, away from the sheet interior --- */
237
+ const strands = 30;
238
+ for (let i = 0; i < strands; i++) {
239
+ const y = hash2(i, 3.1, seed) * h;
240
+ /* lead edge pushes into untouched page (+x), trail leaves through
241
+ cleared page (-x) — both reach away from the ink body. */
242
+ const len = (0.25 + 0.75 * hash2(i, 7.7, seed)) * bristle;
243
+ const width = 1.5 + hash2(i, 11.3, seed) * 7;
244
+ const lx = edgeX(lead, y);
245
+ if (lx < w + bristle) spur(lx - 2, y, 1, len, width);
246
+ const tx = edgeX(trail, y);
247
+ if (tx > -bristle) spur(tx + 2, y, -1, len, width);
248
+ }
249
+ /* Flecks: flung when an edge passes their land point, drawn only while
250
+ they sit outside the sheet (they vanish under it as it catches up). */
251
+ const flecks = 46;
252
+ for (let i = 0; i < flecks; i++) {
253
+ const landT = hash2(i, 21.7, seed);
254
+ const y = hash2(i, 5.9, seed) * h;
255
+ const throwLen = 24 + hash2(i, 9.4, seed) * 170;
256
+ const r = 0.8 + hash2(i, 13.6, seed) * 3.4;
257
+ const lx = edgeX(landT, y) + throwLen;
258
+ if (landT <= lead && lx > edgeX(lead, y) + 6 && lx < w + r) {
259
+ ctx.beginPath();
260
+ ctx.arc(lx, y + r, r, 0, Math.PI * 2);
261
+ ctx.fill();
262
+ }
263
+ const tx = edgeX(landT, y) - throwLen;
264
+ if (landT <= trail && tx < edgeX(trail, y) - 6 && tx > -r) {
265
+ ctx.beginPath();
266
+ ctx.arc(tx, y - r, r, 0, Math.PI * 2);
267
+ ctx.fill();
268
+ }
269
+ }
270
+ };
271
+
272
+ const paintSolid = () => {
273
+ ctx.fillStyle = color;
274
+ ctx.fillRect(0, 0, w, h);
275
+ };
276
+
277
+ const sweep = (prop: "lead" | "trail", to: number, ms: number): Promise<void> =>
278
+ new Promise((resolve) => {
279
+ if (destroyed) return resolve();
280
+ killSweep();
281
+ pendingResolve = resolve;
282
+ current = gsap.to(state, {
283
+ [prop]: to,
284
+ duration: ms / 1000,
285
+ ease,
286
+ overwrite: true,
287
+ onUpdate: draw,
288
+ onComplete: () => {
289
+ current = null;
290
+ const r = pendingResolve;
291
+ pendingResolve = null;
292
+ draw();
293
+ r?.();
294
+ },
295
+ });
296
+ });
297
+
298
+ /* ---- mounting ---- */
299
+ canvas.style.position = "fixed";
300
+ canvas.style.inset = "0";
301
+ canvas.style.pointerEvents = "none";
302
+ canvas.style.zIndex = String(z);
303
+ host.appendChild(canvas);
304
+ resize();
305
+ window.addEventListener("resize", resize);
306
+
307
+ /* ---- multi-page handoff ---- */
308
+ const readFlag = (): string | null => {
309
+ try {
310
+ return sessionStorage.getItem(sessionKey);
311
+ } catch {
312
+ return null;
313
+ }
314
+ };
315
+ const writeFlag = (v: string | null): void => {
316
+ try {
317
+ v === null ? sessionStorage.removeItem(sessionKey) : sessionStorage.setItem(sessionKey, v);
318
+ } catch {
319
+ /* private mode — the wipe still works, just without the handoff */
320
+ }
321
+ };
322
+
323
+ if (autoUnveil && readFlag() === "covered") {
324
+ writeFlag(null);
325
+ state.trail = -1;
326
+ state.lead = 1;
327
+ draw();
328
+ canvas.style.pointerEvents = "auto";
329
+ /* Let first paint land, then sweep the new page in. */
330
+ requestAnimationFrame(() => void unveil());
331
+ }
332
+
333
+ /* ---- link interception ---- */
334
+ const onClick = (e: MouseEvent) => {
335
+ if (e.defaultPrevented || e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
336
+ return;
337
+ }
338
+ const a = (e.target as Element | null)?.closest?.("a[href]") as HTMLAnchorElement | null;
339
+ if (!a || !a.href || a.target === "_blank" || a.hasAttribute("download")) return;
340
+ const url = new URL(a.href, location.href);
341
+ if (url.origin !== location.origin || url.href === location.href) return;
342
+ if (links && !a.matches(links)) return;
343
+ e.preventDefault();
344
+ void cover().then(() => {
345
+ writeFlag("covered");
346
+ location.href = url.href;
347
+ });
348
+ };
349
+ if (links) document.addEventListener("click", onClick);
350
+
351
+ async function cover(): Promise<void> {
352
+ if (destroyed) return;
353
+ killSweep();
354
+ if (reduced) {
355
+ state.trail = -1;
356
+ state.lead = 1;
357
+ paintSolid();
358
+ canvas.style.pointerEvents = "auto";
359
+ return;
360
+ }
361
+ state.trail = -1;
362
+ state.lead = 0;
363
+ draw();
364
+ await sweep("lead", 1, coverMs);
365
+ if (!destroyed) canvas.style.pointerEvents = "auto";
366
+ }
367
+
368
+ async function unveil(): Promise<void> {
369
+ if (destroyed) return;
370
+ killSweep();
371
+ canvas.style.pointerEvents = "none";
372
+ if (reduced) {
373
+ state.trail = 1;
374
+ state.lead = -1;
375
+ ctx?.clearRect(0, 0, w, h);
376
+ return;
377
+ }
378
+ state.trail = -1;
379
+ state.lead = 1;
380
+ draw();
381
+ await sweep("trail", 1, unveilMs);
382
+ if (!destroyed) {
383
+ /* park ready for the next cover() */
384
+ state.trail = 1;
385
+ state.lead = -1;
386
+ ctx?.clearRect(0, 0, w, h);
387
+ }
388
+ }
389
+
390
+ function destroy(): void {
391
+ if (destroyed) return;
392
+ destroyed = true;
393
+ killSweep();
394
+ window.removeEventListener("resize", resize);
395
+ if (links) document.removeEventListener("click", onClick);
396
+ canvas.remove();
397
+ }
398
+
399
+ return { cover, unveil, destroy };
400
+ }
401
+
402
+ /** Missing host / no 2D context: sequence instantly, never throw. */
403
+ function inertHandle(): InkWipeHandle {
404
+ return {
405
+ cover: () => Promise.resolve(),
406
+ unveil: () => Promise.resolve(),
407
+ destroy: () => {},
408
+ };
409
+ }
@@ -78,6 +78,9 @@ export function scatterText(
78
78
  scrollTrigger: {
79
79
  trigger: wrap,
80
80
  pin: pinTarget,
81
+ // Refresh pins first (creation order ≠ document order): our spacer must
82
+ // exist before triggers below us measure, or they start ~6k px early.
83
+ refreshPriority: 1,
81
84
  scrub,
82
85
  end: () => "+=" + travel(),
83
86
  invalidateOnRefresh: true,
@@ -115,6 +115,9 @@ export function stackedCardsPinned(
115
115
  end: "bottom bottom",
116
116
  pin: viewport,
117
117
  pinSpacing: false,
118
+ // Refresh pins first (creation order ≠ document order) so pin+scrub pairs
119
+ // agree on positions no matter where in the script this effect is created.
120
+ refreshPriority: 1,
118
121
  anticipatePin: 1,
119
122
  invalidateOnRefresh: true,
120
123
  });
package/src/index.ts CHANGED
@@ -42,6 +42,7 @@
42
42
  * themeReveal() View Transitions circular wipe
43
43
  * menuOverlay() clip-path curtain menu
44
44
  * preloader() 0→100 counter intro
45
+ * inkWipe() brush-stroke page transition (cover → swap → unveil)
45
46
  */
46
47
 
47
48
  /* ---- core ---- */
@@ -135,3 +136,5 @@ export type { MenuOverlayOptions, MenuOverlayHandle } from "./effects/menu-overl
135
136
 
136
137
  export { preloader } from "./effects/preloader.js";
137
138
  export type { PreloaderOptions } from "./effects/preloader.js";
139
+ export { inkWipe } from "./effects/ink-wipe.js";
140
+ export type { InkWipeOptions, InkWipeHandle } from "./effects/ink-wipe.js";