motionary 10.1.0 → 10.2.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/AGENTS.md +61 -0
- package/CHANGELOG.md +14 -0
- package/README.md +37 -0
- package/dist/chunks/{keyframes-CyH_abhI.js → keyframes-W-474DMc.js} +2 -2
- package/dist/chunks/{keyframes-CyH_abhI.js.map → keyframes-W-474DMc.js.map} +1 -1
- package/dist/chunks/{keyframes-cUdsXSXQ.cjs → keyframes-ZheOkpt0.cjs} +2 -2
- package/dist/chunks/{keyframes-cUdsXSXQ.cjs.map → keyframes-ZheOkpt0.cjs.map} +1 -1
- package/dist/chunks/{registry-jF8VIA_9.js → registry-BIEjKngP.js} +2 -2
- package/dist/chunks/{registry-jF8VIA_9.js.map → registry-BIEjKngP.js.map} +1 -1
- package/dist/chunks/{registry-C0xLNq3-.cjs → registry-eJgpFAzI.cjs} +2 -2
- package/dist/chunks/{registry-C0xLNq3-.cjs.map → registry-eJgpFAzI.cjs.map} +1 -1
- package/dist/chunks/ticker-CM_56oVy.js +70 -0
- package/dist/chunks/ticker-CM_56oVy.js.map +1 -0
- package/dist/chunks/ticker-D4o6enH0.cjs +72 -0
- package/dist/chunks/ticker-D4o6enH0.cjs.map +1 -0
- package/dist/chunks/{tween-JEthWrjo.cjs → tween-CBuWYpI-.cjs} +5 -72
- package/dist/chunks/tween-CBuWYpI-.cjs.map +1 -0
- package/dist/chunks/{tween-DDObu9iA.js → tween-CqX1JBuj.js} +5 -71
- package/dist/chunks/tween-CqX1JBuj.js.map +1 -0
- package/dist/components/core.cjs +106 -0
- package/dist/components/core.cjs.map +1 -1
- package/dist/components/core.d.cts +44 -2
- package/dist/components/core.d.ts +44 -2
- package/dist/components/core.js +105 -1
- package/dist/components/core.js.map +1 -1
- package/dist/components/widgets.cjs +401 -198
- package/dist/components/widgets.cjs.map +1 -1
- package/dist/components/widgets.d.cts +146 -2
- package/dist/components/widgets.d.ts +146 -2
- package/dist/components/widgets.js +399 -199
- package/dist/components/widgets.js.map +1 -1
- package/dist/llms-full.txt +98 -1
- package/dist/llms.txt +6 -2
- package/dist/manifest.json +259 -1
- package/dist/runtime/format-css.cjs +4 -3
- package/dist/runtime/format-css.cjs.map +1 -1
- package/dist/runtime/format-css.js +4 -3
- package/dist/runtime/format-css.js.map +1 -1
- package/dist/runtime/format-motion.cjs +4 -3
- package/dist/runtime/format-motion.cjs.map +1 -1
- package/dist/runtime/format-motion.js +4 -3
- package/dist/runtime/format-motion.js.map +1 -1
- package/dist/runtime/format-svg.cjs +457 -0
- package/dist/runtime/format-svg.cjs.map +1 -0
- package/dist/runtime/format-svg.d.cts +214 -0
- package/dist/runtime/format-svg.d.ts +214 -0
- package/dist/runtime/format-svg.iife.js +2 -0
- package/dist/runtime/format-svg.iife.js.map +1 -0
- package/dist/runtime/format-svg.js +446 -0
- package/dist/runtime/format-svg.js.map +1 -0
- package/dist/runtime/scroll.cjs +301 -0
- package/dist/runtime/scroll.cjs.map +1 -0
- package/dist/runtime/scroll.d.cts +236 -0
- package/dist/runtime/scroll.d.ts +236 -0
- package/dist/runtime/scroll.iife.js +2 -0
- package/dist/runtime/scroll.iife.js.map +1 -0
- package/dist/runtime/scroll.js +292 -0
- package/dist/runtime/scroll.js.map +1 -0
- package/dist/runtime.cjs +5 -4
- package/dist/runtime.cjs.map +1 -1
- package/dist/runtime.d.cts +1 -1
- package/dist/runtime.d.ts +1 -1
- package/dist/runtime.iife.js +1 -1
- package/dist/runtime.iife.js.map +1 -1
- package/dist/runtime.js +5 -4
- package/dist/runtime.js.map +1 -1
- package/dist/widgets.umd.js +1 -1
- package/dist/widgets.umd.js.map +1 -1
- package/docs/ROADMAP.md +1 -1
- package/docs/ai-prompt-guide.md +54 -0
- package/docs/components/README.md +253 -0
- package/docs/components/usa-accordion.md +38 -0
- package/docs/components/usa-acrylic.md +38 -0
- package/docs/components/usa-add-to-cart.md +40 -0
- package/docs/components/usa-ambient.md +32 -0
- package/docs/components/usa-anim-icon.md +34 -0
- package/docs/components/usa-audio.md +38 -0
- package/docs/components/usa-aurora.md +36 -0
- package/docs/components/usa-auto-animate.md +44 -0
- package/docs/components/usa-auto-skeleton.md +38 -0
- package/docs/components/usa-avatar-stack.md +38 -0
- package/docs/components/usa-back-to-top.md +32 -0
- package/docs/components/usa-badge-wall.md +39 -0
- package/docs/components/usa-badge.md +36 -0
- package/docs/components/usa-bar-chart.md +33 -0
- package/docs/components/usa-bg-generator.md +32 -0
- package/docs/components/usa-blobs.md +32 -0
- package/docs/components/usa-bottom-sheet.md +32 -0
- package/docs/components/usa-button.md +78 -0
- package/docs/components/usa-card-stack.md +40 -0
- package/docs/components/usa-card.md +94 -0
- package/docs/components/usa-carousel-3d.md +40 -0
- package/docs/components/usa-carousel.md +40 -0
- package/docs/components/usa-cart-drawer.md +33 -0
- package/docs/components/usa-chapter-nav.md +40 -0
- package/docs/components/usa-chat-composer.md +33 -0
- package/docs/components/usa-check.md +32 -0
- package/docs/components/usa-checkbox.md +32 -0
- package/docs/components/usa-click.md +36 -0
- package/docs/components/usa-clock-control.md +37 -0
- package/docs/components/usa-code-export.md +36 -0
- package/docs/components/usa-color-picker.md +32 -0
- package/docs/components/usa-command-palette.md +39 -0
- package/docs/components/usa-compare.md +38 -0
- package/docs/components/usa-countdown.md +32 -0
- package/docs/components/usa-counter.md +32 -0
- package/docs/components/usa-cube-gallery.md +40 -0
- package/docs/components/usa-cube.md +36 -0
- package/docs/components/usa-cursor.md +32 -0
- package/docs/components/usa-date-picker.md +32 -0
- package/docs/components/usa-depth.md +44 -0
- package/docs/components/usa-dialog.md +40 -0
- package/docs/components/usa-disclosure.md +38 -0
- package/docs/components/usa-distort.md +36 -0
- package/docs/components/usa-dock.md +40 -0
- package/docs/components/usa-dot-network.md +32 -0
- package/docs/components/usa-double-tap.md +36 -0
- package/docs/components/usa-draggable.md +38 -0
- package/docs/components/usa-draw.md +36 -0
- package/docs/components/usa-drawer.md +34 -0
- package/docs/components/usa-equalizer.md +33 -0
- package/docs/components/usa-fab.md +40 -0
- package/docs/components/usa-festival-banner.md +32 -0
- package/docs/components/usa-field.md +34 -0
- package/docs/components/usa-file-drop.md +35 -0
- package/docs/components/usa-fullpage.md +38 -0
- package/docs/components/usa-fx.md +851 -0
- package/docs/components/usa-gauge.md +32 -0
- package/docs/components/usa-gen-art.md +32 -0
- package/docs/components/usa-gesture-fx.md +36 -0
- package/docs/components/usa-gesture-sticker.md +36 -0
- package/docs/components/usa-glitch.md +32 -0
- package/docs/components/usa-globe.md +33 -0
- package/docs/components/usa-gradient-text.md +32 -0
- package/docs/components/usa-grain.md +36 -0
- package/docs/components/usa-grid-glow.md +36 -0
- package/docs/components/usa-gyro-card.md +38 -0
- package/docs/components/usa-handwriting.md +32 -0
- package/docs/components/usa-hero-video.md +38 -0
- package/docs/components/usa-hold.md +32 -0
- package/docs/components/usa-hud-panel.md +38 -0
- package/docs/components/usa-hydrate.md +42 -0
- package/docs/components/usa-icon-morph.md +34 -0
- package/docs/components/usa-install-button.md +32 -0
- package/docs/components/usa-kanban.md +46 -0
- package/docs/components/usa-keyframe-editor.md +37 -0
- package/docs/components/usa-kpi.md +32 -0
- package/docs/components/usa-leaderboard.md +41 -0
- package/docs/components/usa-like.md +32 -0
- package/docs/components/usa-liquid-nav.md +36 -0
- package/docs/components/usa-liquid.md +36 -0
- package/docs/components/usa-loading-bar.md +35 -0
- package/docs/components/usa-location-card.md +34 -0
- package/docs/components/usa-lottie-icon.md +34 -0
- package/docs/components/usa-lottie.md +32 -0
- package/docs/components/usa-lyrics.md +38 -0
- package/docs/components/usa-magnetic.md +32 -0
- package/docs/components/usa-marquee.md +38 -0
- package/docs/components/usa-mask-reveal.md +36 -0
- package/docs/components/usa-masonry-flow.md +42 -0
- package/docs/components/usa-masonry.md +38 -0
- package/docs/components/usa-menu-toggle.md +34 -0
- package/docs/components/usa-menu.md +44 -0
- package/docs/components/usa-message-list.md +39 -0
- package/docs/components/usa-milestones.md +40 -0
- package/docs/components/usa-modal.md +42 -0
- package/docs/components/usa-morph.md +34 -0
- package/docs/components/usa-motion-inspector.md +32 -0
- package/docs/components/usa-motion-prefs.md +32 -0
- package/docs/components/usa-motion-spec.md +32 -0
- package/docs/components/usa-motion-switch.md +34 -0
- package/docs/components/usa-motion-theme.md +40 -0
- package/docs/components/usa-motion.md +42 -0
- package/docs/components/usa-music-player.md +36 -0
- package/docs/components/usa-native-preview.md +36 -0
- package/docs/components/usa-nav-morph.md +40 -0
- package/docs/components/usa-navbar.md +36 -0
- package/docs/components/usa-notification-bell.md +37 -0
- package/docs/components/usa-odometer.md +34 -0
- package/docs/components/usa-organic-card.md +36 -0
- package/docs/components/usa-otp.md +33 -0
- package/docs/components/usa-overscroll.md +36 -0
- package/docs/components/usa-pack.md +111 -0
- package/docs/components/usa-pagination.md +32 -0
- package/docs/components/usa-panorama.md +32 -0
- package/docs/components/usa-particles.md +32 -0
- package/docs/components/usa-pause-all.md +32 -0
- package/docs/components/usa-perf-monitor.md +32 -0
- package/docs/components/usa-pinch-zoom.md +36 -0
- package/docs/components/usa-player.md +38 -0
- package/docs/components/usa-plugin-card.md +61 -0
- package/docs/components/usa-plugin-store.md +36 -0
- package/docs/components/usa-popover.md +38 -0
- package/docs/components/usa-post-fx.md +36 -0
- package/docs/components/usa-presence.md +36 -0
- package/docs/components/usa-press.md +32 -0
- package/docs/components/usa-prize-wheel.md +32 -0
- package/docs/components/usa-product-gallery.md +40 -0
- package/docs/components/usa-progress-ring.md +38 -0
- package/docs/components/usa-progress.md +34 -0
- package/docs/components/usa-prop-panel.md +34 -0
- package/docs/components/usa-pull-cord.md +32 -0
- package/docs/components/usa-pull-refresh.md +37 -0
- package/docs/components/usa-radar.md +32 -0
- package/docs/components/usa-reactions.md +32 -0
- package/docs/components/usa-red-envelope.md +32 -0
- package/docs/components/usa-retro-button.md +34 -0
- package/docs/components/usa-reveal.md +36 -0
- package/docs/components/usa-ripple.md +32 -0
- package/docs/components/usa-scene.md +38 -0
- package/docs/components/usa-scramble.md +32 -0
- package/docs/components/usa-scroll-highlight.md +34 -0
- package/docs/components/usa-scroll-progress.md +36 -0
- package/docs/components/usa-scroll-scene.md +66 -0
- package/docs/components/usa-scrolly.md +40 -0
- package/docs/components/usa-segmented.md +40 -0
- package/docs/components/usa-shader.md +74 -0
- package/docs/components/usa-sheet.md +42 -0
- package/docs/components/usa-shimmer-text.md +32 -0
- package/docs/components/usa-shortcut.md +34 -0
- package/docs/components/usa-skeleton-reveal.md +42 -0
- package/docs/components/usa-skeleton.md +36 -0
- package/docs/components/usa-sketch-chart.md +32 -0
- package/docs/components/usa-slider.md +32 -0
- package/docs/components/usa-sparkline.md +32 -0
- package/docs/components/usa-spatial-card.md +40 -0
- package/docs/components/usa-spinner.md +34 -0
- package/docs/components/usa-splash.md +36 -0
- package/docs/components/usa-split-text.md +32 -0
- package/docs/components/usa-spotlight.md +40 -0
- package/docs/components/usa-spring.md +42 -0
- package/docs/components/usa-stagger.md +40 -0
- package/docs/components/usa-star-rating.md +34 -0
- package/docs/components/usa-stepper.md +42 -0
- package/docs/components/usa-sticky-stack.md +40 -0
- package/docs/components/usa-sticky-wall.md +40 -0
- package/docs/components/usa-stories.md +38 -0
- package/docs/components/usa-story.md +80 -0
- package/docs/components/usa-suggestion-chips.md +32 -0
- package/docs/components/usa-swipe-deck.md +40 -0
- package/docs/components/usa-swipeable.md +36 -0
- package/docs/components/usa-switch.md +32 -0
- package/docs/components/usa-tab-bar.md +46 -0
- package/docs/components/usa-tabs.md +40 -0
- package/docs/components/usa-terminal.md +38 -0
- package/docs/components/usa-text-rotate.md +32 -0
- package/docs/components/usa-theme-surface.md +36 -0
- package/docs/components/usa-theme-switcher.md +32 -0
- package/docs/components/usa-tilt.md +36 -0
- package/docs/components/usa-timeline.md +40 -0
- package/docs/components/usa-tip.md +44 -0
- package/docs/components/usa-toast-stack.md +36 -0
- package/docs/components/usa-toaster.md +36 -0
- package/docs/components/usa-typewriter.md +32 -0
- package/docs/components/usa-upload-progress.md +33 -0
- package/docs/components/usa-video-card.md +40 -0
- package/docs/components/usa-view-switch.md +38 -0
- package/docs/components/usa-voice-button.md +33 -0
- package/docs/components/usa-volume-knob.md +32 -0
- package/docs/components/usa-water-ripple.md +36 -0
- package/docs/components/usa-wave-text.md +32 -0
- package/docs/components/usa-weather-card.md +32 -0
- package/docs/components/usa-worker-canvas.md +32 -0
- package/docs/components/usa-xp-bar.md +33 -0
- package/docs/components.md +50 -0
- package/docs/runtime/format-svg.md +62 -0
- package/docs/runtime/scroll.md +61 -0
- package/package.json +25 -3
- package/dist/chunks/tween-DDObu9iA.js.map +0 -1
- package/dist/chunks/tween-JEthWrjo.cjs.map +0 -1
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
/** A runtime module: `{ id, version, api }`, registered with `use()`. */
|
|
2
|
+
interface RuntimeModule<A = unknown> {
|
|
3
|
+
/** Module id: 'core', 'format-css', 'scroll', … (import path `motionary/runtime/<id>`). */
|
|
4
|
+
id: string;
|
|
5
|
+
version: string;
|
|
6
|
+
/** Other modules this one needs (registered first by `use()` callers). */
|
|
7
|
+
requires?: string[];
|
|
8
|
+
/** The module's public API (what `requireModule(id)` returns). */
|
|
9
|
+
api: A;
|
|
10
|
+
/** Optional one-time setup, called on first registration. */
|
|
11
|
+
setup?(registry: RuntimeRegistry): void;
|
|
12
|
+
}
|
|
13
|
+
interface RuntimeRegistry {
|
|
14
|
+
version: string;
|
|
15
|
+
modules: Map<string, RuntimeModule>;
|
|
16
|
+
/** Shared per-page state slots (the ticker lives here). */
|
|
17
|
+
slots: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Easing functions (t ∈ [0, 1] → progress). Original implementations of the standard Penner-style curves. */
|
|
21
|
+
type Ease = (t: number) => number;
|
|
22
|
+
|
|
23
|
+
type Target = object | Element;
|
|
24
|
+
type Props = Record<string, number | string>;
|
|
25
|
+
interface PlayOptions {
|
|
26
|
+
/** Delay before the first iteration, ms. */
|
|
27
|
+
delay?: number;
|
|
28
|
+
/** Extra iterations (-1 = forever). */
|
|
29
|
+
repeat?: number;
|
|
30
|
+
/** Alternate direction on every other iteration. */
|
|
31
|
+
yoyo?: boolean;
|
|
32
|
+
/** Start paused (`play()` to start). */
|
|
33
|
+
paused?: boolean;
|
|
34
|
+
onUpdate?: (progress: number) => void;
|
|
35
|
+
onComplete?: () => void;
|
|
36
|
+
}
|
|
37
|
+
interface TweenOptions extends PlayOptions {
|
|
38
|
+
to?: Props;
|
|
39
|
+
from?: Props;
|
|
40
|
+
/** ms (default 600). */
|
|
41
|
+
duration?: number;
|
|
42
|
+
ease?: string | Ease;
|
|
43
|
+
/** Delay added per target index (ms) when several targets are given. */
|
|
44
|
+
stagger?: number;
|
|
45
|
+
}
|
|
46
|
+
/** Common playback: delay, repeat, yoyo, direction, ticker attachment, promise. */
|
|
47
|
+
declare abstract class Playable {
|
|
48
|
+
delay: number;
|
|
49
|
+
repeat: number;
|
|
50
|
+
yoyo: boolean;
|
|
51
|
+
/** Playback rate multiplier. */
|
|
52
|
+
timeScale: number;
|
|
53
|
+
onUpdate?: (progress: number) => void;
|
|
54
|
+
onComplete?: () => void;
|
|
55
|
+
protected _t: number;
|
|
56
|
+
private _dir;
|
|
57
|
+
private _off;
|
|
58
|
+
private _done;
|
|
59
|
+
private _resolve;
|
|
60
|
+
/** Resolves on completion (forward end, or start when reversed). */
|
|
61
|
+
finished: Promise<void>;
|
|
62
|
+
/** Set when owned by a timeline (then the ticker never drives it). */
|
|
63
|
+
parent: Timeline | null;
|
|
64
|
+
constructor(o: PlayOptions);
|
|
65
|
+
/** One iteration, ms. */
|
|
66
|
+
abstract get duration(): number;
|
|
67
|
+
/** Render at `ms` into one iteration. */
|
|
68
|
+
protected abstract renderLocal(ms: number, iterationEnded: boolean): void;
|
|
69
|
+
get totalDuration(): number;
|
|
70
|
+
get time(): number;
|
|
71
|
+
get progress(): number;
|
|
72
|
+
set progress(p: number);
|
|
73
|
+
get isActive(): boolean;
|
|
74
|
+
get reversed(): boolean;
|
|
75
|
+
/** Jump to `ms` (total time, including the delay) and render. */
|
|
76
|
+
seek(ms: number): this;
|
|
77
|
+
play(): this;
|
|
78
|
+
pause(): this;
|
|
79
|
+
/** Play backwards from the current time. */
|
|
80
|
+
reverse(): this;
|
|
81
|
+
restart(): this;
|
|
82
|
+
/** Stop and detach for good. */
|
|
83
|
+
kill(): void;
|
|
84
|
+
/** Promise-like: `await tween(...)`. */
|
|
85
|
+
then<R>(ok?: (v: void) => R, err?: (e: unknown) => R): Promise<R>;
|
|
86
|
+
private attach;
|
|
87
|
+
private advance;
|
|
88
|
+
protected complete(): void;
|
|
89
|
+
}
|
|
90
|
+
/** Position: ms number, '<' (start of previous), '>' (end of previous, default), '+=200' / '-=200' (relative to the end), 'label', 'label+=100', '<+=100'. */
|
|
91
|
+
type Position = number | string;
|
|
92
|
+
interface TimelineOptions extends PlayOptions {
|
|
93
|
+
/** Defaults merged into every `.to()`. */
|
|
94
|
+
defaults?: Omit<TweenOptions, 'to' | 'from'>;
|
|
95
|
+
}
|
|
96
|
+
/** A sequence of tweens, nested timelines and callbacks. */
|
|
97
|
+
declare class Timeline extends Playable {
|
|
98
|
+
private children;
|
|
99
|
+
private labels;
|
|
100
|
+
private prevStart;
|
|
101
|
+
private prevEnd;
|
|
102
|
+
private defaults;
|
|
103
|
+
private lastLocal;
|
|
104
|
+
constructor(o?: TimelineOptions);
|
|
105
|
+
get duration(): number;
|
|
106
|
+
private resolve;
|
|
107
|
+
/** Add a tween, timeline or callback at a position. */
|
|
108
|
+
add(item: Playable | (() => void), position?: Position): this;
|
|
109
|
+
/** `tween(target, vars)` placed at `position` (stagger across several targets). */
|
|
110
|
+
to(target: Target | Target[] | ArrayLike<Target>, vars: TweenOptions, position?: Position): this;
|
|
111
|
+
call(fn: () => void, position?: Position): this;
|
|
112
|
+
label(name: string, position?: Position): this;
|
|
113
|
+
/** Time of a label, ms. */
|
|
114
|
+
labelTime(name: string): number | undefined;
|
|
115
|
+
remove(item: Playable): void;
|
|
116
|
+
/** Children in start order (callbacks excluded). */
|
|
117
|
+
getChildren(): Playable[];
|
|
118
|
+
protected renderLocal(ms: number): void;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* `motionary/runtime/scroll` (10.2) — scroll-linked scenes, an original
|
|
123
|
+
* implementation: start / end rules, scrubbing (direct or smoothed), pinning,
|
|
124
|
+
* debug markers, enter / leave callbacks and per-edge actions for a runtime
|
|
125
|
+
* tween or timeline.
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* import { use, timeline } from 'motionary/runtime';
|
|
129
|
+
* import { scroll, scrollScene } from 'motionary/runtime/scroll';
|
|
130
|
+
* use(scroll);
|
|
131
|
+
* const tl = timeline({ paused: true }).to('.card', { to: { x: 200, rotate: 8 } });
|
|
132
|
+
* scrollScene({ trigger: '.section', start: 'top 80%', end: 'bottom 20%', scrub: 120, pin: true, markers: true, animation: tl });
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* Rules: `"<trigger edge> <viewport edge>"` where an edge is `top | center | bottom`,
|
|
136
|
+
* a percentage or pixels, optionally `+=N` / `-=N` (e.g. `"top top+=80"`);
|
|
137
|
+
* `end` may also be `"+=600"` (pixels after start). Horizontal scenes use
|
|
138
|
+
* `left | center | right`. Nothing touches `window` until a scene is created.
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
type SceneAction = 'play' | 'pause' | 'resume' | 'reverse' | 'restart' | 'reset' | 'complete' | 'none';
|
|
142
|
+
interface SceneOptions {
|
|
143
|
+
/** Element (or selector) whose position defines the scene. */
|
|
144
|
+
trigger: Element | string;
|
|
145
|
+
/** Default 'top bottom' (trigger top meets viewport bottom). */
|
|
146
|
+
start?: string;
|
|
147
|
+
/** Default 'bottom top'. `'+=600'` = 600 px after start. */
|
|
148
|
+
end?: string;
|
|
149
|
+
/** Link the animation to the scroll position: `true` (direct) or a smoothing time in ms. */
|
|
150
|
+
scrub?: boolean | number;
|
|
151
|
+
/** Runtime tween / timeline (created `paused: true`). */
|
|
152
|
+
animation?: Playable;
|
|
153
|
+
/** Without scrub: actions on enter, leave, enter back, leave back. Default 'play none none reverse'. */
|
|
154
|
+
actions?: string;
|
|
155
|
+
/** Pin the trigger (or another element) while the scene is active. */
|
|
156
|
+
pin?: boolean | Element | string;
|
|
157
|
+
/** Show start / end markers (debugging). */
|
|
158
|
+
markers?: boolean | {
|
|
159
|
+
color?: string;
|
|
160
|
+
};
|
|
161
|
+
/** Class toggled on the trigger while active. */
|
|
162
|
+
toggleClass?: string;
|
|
163
|
+
/** Kill after the first forward completion. */
|
|
164
|
+
once?: boolean;
|
|
165
|
+
horizontal?: boolean;
|
|
166
|
+
/** Scroll container (default: the window). */
|
|
167
|
+
scroller?: Element | string;
|
|
168
|
+
onEnter?: (s: ScrollScene) => void;
|
|
169
|
+
onLeave?: (s: ScrollScene) => void;
|
|
170
|
+
onEnterBack?: (s: ScrollScene) => void;
|
|
171
|
+
onLeaveBack?: (s: ScrollScene) => void;
|
|
172
|
+
onUpdate?: (s: ScrollScene) => void;
|
|
173
|
+
onToggle?: (s: ScrollScene) => void;
|
|
174
|
+
}
|
|
175
|
+
/** One side of a rule ('top', '80%', '120px', 'center+=40') → pixels within `size`. */
|
|
176
|
+
declare function parseEdge(spec: string, size: number): number;
|
|
177
|
+
/** Scroll offset at which `rule` is met: trigger edge (at `triggerStart`, size `triggerSize`) meets viewport edge. */
|
|
178
|
+
declare function resolveRule(rule: string, triggerStart: number, triggerSize: number, viewport: number): number;
|
|
179
|
+
/** A scroll scene (see `scrollScene()`). */
|
|
180
|
+
declare class ScrollScene {
|
|
181
|
+
readonly trigger: Element;
|
|
182
|
+
readonly options: SceneOptions;
|
|
183
|
+
/** Scroll offsets (px) of the start and end. */
|
|
184
|
+
start: number;
|
|
185
|
+
end: number;
|
|
186
|
+
/** 0 → 1 between start and end. */
|
|
187
|
+
progress: number;
|
|
188
|
+
/** 1 scrolling forward, -1 back. */
|
|
189
|
+
direction: number;
|
|
190
|
+
isActive: boolean;
|
|
191
|
+
private shown;
|
|
192
|
+
private last;
|
|
193
|
+
private offs;
|
|
194
|
+
private pinEl;
|
|
195
|
+
private spacer;
|
|
196
|
+
private marks;
|
|
197
|
+
private scroller;
|
|
198
|
+
private killed;
|
|
199
|
+
constructor(o: SceneOptions);
|
|
200
|
+
private refreshBound;
|
|
201
|
+
private pos;
|
|
202
|
+
private viewport;
|
|
203
|
+
/** Re-measure start / end (after layout changes). */
|
|
204
|
+
refresh(): void;
|
|
205
|
+
/** Read the scroll position and fire callbacks / drive the animation. */
|
|
206
|
+
update(): void;
|
|
207
|
+
private smooth;
|
|
208
|
+
private act;
|
|
209
|
+
private setupPin;
|
|
210
|
+
private applyPin;
|
|
211
|
+
private drawMarkers;
|
|
212
|
+
/** Remove listeners, pin spacer and markers (`keepState` keeps the animation where it is). */
|
|
213
|
+
kill(keepState?: boolean): void;
|
|
214
|
+
}
|
|
215
|
+
/** Create a scroll scene. */
|
|
216
|
+
declare function scrollScene(o: SceneOptions): ScrollScene;
|
|
217
|
+
/** Re-measure every scene (call after layout changes such as images loading). */
|
|
218
|
+
declare function refreshScenes(): void;
|
|
219
|
+
/** Kill every scene. */
|
|
220
|
+
declare function killScenes(): void;
|
|
221
|
+
/** The live scenes. */
|
|
222
|
+
declare const allScenes: () => ScrollScene[];
|
|
223
|
+
interface ScrollApi {
|
|
224
|
+
scrollScene: typeof scrollScene;
|
|
225
|
+
refreshScenes: typeof refreshScenes;
|
|
226
|
+
killScenes: typeof killScenes;
|
|
227
|
+
allScenes: typeof allScenes;
|
|
228
|
+
parseEdge: typeof parseEdge;
|
|
229
|
+
resolveRule: typeof resolveRule;
|
|
230
|
+
ScrollScene: typeof ScrollScene;
|
|
231
|
+
}
|
|
232
|
+
/** The module object for `use(scroll)`. */
|
|
233
|
+
declare const scroll: RuntimeModule<ScrollApi>;
|
|
234
|
+
|
|
235
|
+
export { ScrollScene, allScenes, killScenes, parseEdge, refreshScenes, resolveRule, scroll, scrollScene };
|
|
236
|
+
export type { SceneAction, SceneOptions, ScrollApi };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(t,s,e){"use strict";const i={top:0,left:0,center:.5,bottom:1,right:1};function o(t,s){const e=/^([a-z]+|-?[\d.]+%|-?[\d.]+(?:px)?)?\s*(?:([+-])=\s*(-?[\d.]+)(px|%)?)?$/.exec(t.trim());if(!e)throw new Error(`[motionary] scroll: bad edge "${t}"`);let o=0;const r=e[1]||"0";if(r in i)o=i[r]*s;else if(r.endsWith("%"))o=parseFloat(r)/100*s;else{if(!/^-?[\d.]/.test(r))throw new Error(`[motionary] scroll: bad edge "${t}"`);o=parseFloat(r)}if(e[2]){const t="%"===e[4]?parseFloat(e[3])/100*s:parseFloat(e[3]);o+="+"===e[2]?t:-t}return o}function r(t,s,e,i){const r=t.trim().split(/\s+(?![+-]=)/),n=r[0]??"top",h=r[1]??"bottom";return s+o(n,e)-o(h,i)}const n=new Set,h=t=>{const s="string"==typeof t?document.querySelector(t):t;if(!s)throw new Error(`[motionary] scroll: element "${t}" not found`);return s};class l{constructor(t){if(this.start=0,this.end=0,this.progress=0,this.direction=1,this.isActive=!1,this.shown=0,this.last=-1,this.offs=[],this.pinEl=null,this.spacer=null,this.marks=[],this.killed=!1,this.refreshBound=()=>this.refresh(),"undefined"==typeof window)throw new Error("[motionary] scroll scenes need a browser window");this.options=t,this.trigger=h(t.trigger),this.scroller=t.scroller?h(t.scroller):window,t.pin&&this.setupPin(!0===t.pin?this.trigger:h(t.pin));const s=this.scroller,i=()=>this.update();s.addEventListener("scroll",i,{passive:!0}),window.addEventListener("resize",this.refreshBound),this.offs.push(()=>s.removeEventListener("scroll",i),()=>window.removeEventListener("resize",this.refreshBound)),t.scrub&&"number"==typeof t.scrub&&this.offs.push(e.getTicker().add((t,s)=>this.smooth(s))),n.add(this),this.refresh()}pos(){const t=this.scroller;return t===window?this.options.horizontal?window.scrollX:window.scrollY:this.options.horizontal?t.scrollLeft:t.scrollTop}viewport(){const t=this.scroller;return t===window?this.options.horizontal?window.innerWidth:window.innerHeight:this.options.horizontal?t.clientWidth:t.clientHeight}refresh(){if(this.killed)return;const t=!!this.options.horizontal,s=(this.spacer||this.trigger).getBoundingClientRect(),e=this.scroller===window?{top:0,left:0}:this.scroller.getBoundingClientRect(),i=(t?s.left-e.left:s.top-e.top)+this.pos(),o=(this.pinEl?t?this.pinEl.offsetWidth:this.pinEl.offsetHeight:t?s.width:s.height)||0,n=this.viewport();this.start=r(this.options.start||(t?"left right":"top bottom"),i,o,n);const h=this.options.end||(t?"right left":"bottom top");this.end=/^\+=/.test(h.trim())?this.start+parseFloat(h.trim().slice(2)):r(h,i,o,n),this.end<=this.start&&(this.end=this.start+1),this.spacer&&this.pinEl&&(this.spacer.style[t?"paddingRight":"paddingBottom"]=this.end-this.start+"px"),this.drawMarkers(n),this.last=-1,this.update()}update(){if(this.killed)return;const t=this.pos(),s=Math.min(1,Math.max(0,(t-this.start)/(this.end-this.start)));t!==this.last&&this.last>=0&&(this.direction=t>this.last?1:-1);const e=this.progress,i=this.isActive;this.progress=s,this.isActive=t>this.start&&t<this.end,this.last=t;const o=this.options;if(this.isActive!==i||0===e&&1===s||1===e&&0===s){const t=[];s>0&&0===e&&t.push(["onEnter",0]),1===s&&e<1&&t.push(["onLeave",1]),s<1&&1===e&&t.push(["onEnterBack",2]),0===s&&e>0&&t.push(["onLeaveBack",3]);for(const[s,e]of t)o[s]?.(this),o.scrub||this.act(e);if(o.toggleClass&&this.trigger.classList.toggle(o.toggleClass,this.isActive),o.onToggle?.(this),o.once&&1===s)return this.applyPin(1),void this.kill(!0)}this.applyPin(s),!0===o.scrub&&o.animation&&(o.animation.progress=s),o.onUpdate?.(this)}smooth(t){const s=this.options.animation;if(!s)return;const e=1-Math.exp(-t/Math.max(1,this.options.scrub));this.shown+=(this.progress-this.shown)*e,Math.abs(this.progress-this.shown)<5e-4&&(this.shown=this.progress),s.progress=this.shown}act(t){const s=this.options.animation;if(!s)return;const e=(this.options.actions||"play none none reverse").split(/\s+/)[t]||"none";"play"===e?s.play():"pause"===e?s.pause():"resume"===e?s.play():"reverse"===e?s.reverse():"restart"===e?s.restart():"reset"===e?(s.pause(),s.seek(0)):"complete"===e&&(s.pause(),s.progress=1)}setupPin(t){this.pinEl=t;const s=document.createElement("div");s.className="usa-pin-spacer",s.style.boxSizing="content-box",t.parentNode?.insertBefore(s,t),s.appendChild(t),this.spacer=s,this.offs.push(()=>{t.style.position=t.style.top=t.style.left=t.style.width=t.style.transform="",s.parentNode?.insertBefore(t,s),s.remove()})}applyPin(t){const s=this.pinEl,e=this.spacer;if(!s||!e)return;const i=!!this.options.horizontal;if(t>0&&t<1&&this.scroller===window){const t=e.getBoundingClientRect(),o=this.start-(i?t.left+window.scrollX:t.top+window.scrollY);s.style.position="fixed",s.style.width=`${t.width}px`,s.style.left=i?-o+"px":`${t.left}px`,s.style.top=i?`${t.top}px`:-o+"px",s.style.transform=""}else{s.style.position=s.style.top=s.style.left=s.style.width="";const e=t>=1?this.end-this.start:t>0?this.pos()-this.start:0;s.style.transform=e?i?`translateX(${e}px)`:`translateY(${e}px)`:""}}drawMarkers(t){this.marks.forEach(t=>t.remove()),this.marks=[];const s=this.options.markers;if(!s)return;const e="object"==typeof s&&s.color||"#e11d48",i=!!this.options.horizontal,r=this.scroller===window?document.body:this.scroller,n=(t,s,e,o)=>{const n=document.createElement("div");n.className="usa-scroll-marker",n.textContent=t,n.setAttribute("aria-hidden","true"),Object.assign(n.style,{position:e?"fixed":"absolute",[i?"left":"top"]:`${s}px`,[i?"top":"right"]:"0",[i?"height":"width"]:"80px",[i?"borderLeft":"borderTop"]:`2px solid ${o}`,color:o,font:"600 11px/1.2 ui-monospace,monospace",padding:"2px 4px",zIndex:"2147483646",pointerEvents:"none",background:"rgba(255,255,255,.7)"}),(e?document.body:r).appendChild(n),this.marks.push(n)},h=(t,s)=>(t||s).trim().split(/\s+(?![+-]=)/)[1]??"bottom";n("start "+(0|this.start),this.start,!1,e),n("end "+(0|this.end),this.end,!1,e),n("scroller-start",o(h(this.options.start,"top bottom"),t),!0,"#16a34a"),/^\+=/.test((this.options.end||"").trim())||n("scroller-end",o(h(this.options.end,"bottom top"),t),!0,"#2563eb")}kill(t=!1){this.killed||(this.killed=!0,t||this.options.animation?.pause(),this.offs.splice(0).forEach(t=>t()),this.marks.forEach(t=>t.remove()),n.delete(this))}}function a(t){return new l(t)}function p(){n.forEach(t=>t.refresh())}function c(){Array.from(n).forEach(t=>t.kill())}const d=()=>Array.from(n),f={id:"scroll",version:s.RUNTIME_VERSION,requires:["core"],api:{scrollScene:a,refreshScenes:p,killScenes:c,allScenes:d,parseEdge:o,resolveRule:r,ScrollScene:l}};s.register(f),t.ScrollScene=l,t.allScenes=d,t.killScenes=c,t.parseEdge=o,t.refreshScenes=p,t.resolveRule=r,t.scroll=f,t.scrollScene=a}(this.MotionaryRuntime=this.MotionaryRuntime||{},MotionaryRuntime,MotionaryRuntime);
|
|
2
|
+
//# sourceMappingURL=scroll.iife.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scroll.iife.js","sources":["../../src/runtime/scroll.ts","../../src/runtime/iife/scroll.ts"],"sourcesContent":["/**\n * `motionary/runtime/scroll` (10.2) — scroll-linked scenes, an original\n * implementation: start / end rules, scrubbing (direct or smoothed), pinning,\n * debug markers, enter / leave callbacks and per-edge actions for a runtime\n * tween or timeline.\n *\n * ```ts\n * import { use, timeline } from 'motionary/runtime';\n * import { scroll, scrollScene } from 'motionary/runtime/scroll';\n * use(scroll);\n * const tl = timeline({ paused: true }).to('.card', { to: { x: 200, rotate: 8 } });\n * scrollScene({ trigger: '.section', start: 'top 80%', end: 'bottom 20%', scrub: 120, pin: true, markers: true, animation: tl });\n * ```\n *\n * Rules: `\"<trigger edge> <viewport edge>\"` where an edge is `top | center | bottom`,\n * a percentage or pixels, optionally `+=N` / `-=N` (e.g. `\"top top+=80\"`);\n * `end` may also be `\"+=600\"` (pixels after start). Horizontal scenes use\n * `left | center | right`. Nothing touches `window` until a scene is created.\n */\nimport { RUNTIME_VERSION, type RuntimeModule } from './registry';\nimport { getTicker } from './ticker';\nimport type { Playable } from './tween';\n\nexport type SceneAction = 'play' | 'pause' | 'resume' | 'reverse' | 'restart' | 'reset' | 'complete' | 'none';\n\nexport interface SceneOptions {\n /** Element (or selector) whose position defines the scene. */\n trigger: Element | string;\n /** Default 'top bottom' (trigger top meets viewport bottom). */\n start?: string;\n /** Default 'bottom top'. `'+=600'` = 600 px after start. */\n end?: string;\n /** Link the animation to the scroll position: `true` (direct) or a smoothing time in ms. */\n scrub?: boolean | number;\n /** Runtime tween / timeline (created `paused: true`). */\n animation?: Playable;\n /** Without scrub: actions on enter, leave, enter back, leave back. Default 'play none none reverse'. */\n actions?: string;\n /** Pin the trigger (or another element) while the scene is active. */\n pin?: boolean | Element | string;\n /** Show start / end markers (debugging). */\n markers?: boolean | { color?: string };\n /** Class toggled on the trigger while active. */\n toggleClass?: string;\n /** Kill after the first forward completion. */\n once?: boolean;\n horizontal?: boolean;\n /** Scroll container (default: the window). */\n scroller?: Element | string;\n onEnter?: (s: ScrollScene) => void;\n onLeave?: (s: ScrollScene) => void;\n onEnterBack?: (s: ScrollScene) => void;\n onLeaveBack?: (s: ScrollScene) => void;\n onUpdate?: (s: ScrollScene) => void;\n onToggle?: (s: ScrollScene) => void;\n}\n\nconst KEYWORDS: Record<string, number> = { top: 0, left: 0, center: 0.5, bottom: 1, right: 1 };\n\n/** One side of a rule ('top', '80%', '120px', 'center+=40') → pixels within `size`. */\nexport function parseEdge(spec: string, size: number): number {\n const m = /^([a-z]+|-?[\\d.]+%|-?[\\d.]+(?:px)?)?\\s*(?:([+-])=\\s*(-?[\\d.]+)(px|%)?)?$/.exec(spec.trim());\n if (!m) throw new Error(`[motionary] scroll: bad edge \"${spec}\"`);\n let v = 0;\n const b = m[1] || '0';\n if (b in KEYWORDS) v = KEYWORDS[b] * size;\n else if (b.endsWith('%')) v = (parseFloat(b) / 100) * size;\n else if (/^-?[\\d.]/.test(b)) v = parseFloat(b);\n else throw new Error(`[motionary] scroll: bad edge \"${spec}\"`);\n if (m[2]) {\n const n = m[4] === '%' ? (parseFloat(m[3]) / 100) * size : parseFloat(m[3]);\n v += m[2] === '+' ? n : -n;\n }\n return v;\n}\n\n/** Scroll offset at which `rule` is met: trigger edge (at `triggerStart`, size `triggerSize`) meets viewport edge. */\nexport function resolveRule(rule: string, triggerStart: number, triggerSize: number, viewport: number): number {\n const parts = rule.trim().split(/\\s+(?![+-]=)/);\n const t = parts[0] ?? 'top';\n const s = parts[1] ?? 'bottom';\n return triggerStart + parseEdge(t, triggerSize) - parseEdge(s, viewport);\n}\n\nconst scenes = new Set<ScrollScene>();\nconst q = (x: Element | string): Element => {\n const el = typeof x === 'string' ? document.querySelector(x) : x;\n if (!el) throw new Error(`[motionary] scroll: element \"${x}\" not found`);\n return el;\n};\n\n/** A scroll scene (see `scrollScene()`). */\nexport class ScrollScene {\n readonly trigger: Element;\n readonly options: SceneOptions;\n /** Scroll offsets (px) of the start and end. */\n start = 0;\n end = 0;\n /** 0 → 1 between start and end. */\n progress = 0;\n /** 1 scrolling forward, -1 back. */\n direction = 1;\n isActive = false;\n private shown = 0;\n private last = -1;\n private offs: (() => void)[] = [];\n private pinEl: HTMLElement | null = null;\n private spacer: HTMLElement | null = null;\n private marks: HTMLElement[] = [];\n private scroller: Element | Window;\n private killed = false;\n\n constructor(o: SceneOptions) {\n if (typeof window === 'undefined') throw new Error('[motionary] scroll scenes need a browser window');\n this.options = o;\n this.trigger = q(o.trigger);\n this.scroller = o.scroller ? q(o.scroller) : window;\n if (o.pin) this.setupPin(o.pin === true ? (this.trigger as HTMLElement) : (q(o.pin) as HTMLElement));\n const target: EventTarget = this.scroller;\n const onScroll = () => this.update();\n target.addEventListener('scroll', onScroll, { passive: true });\n window.addEventListener('resize', this.refreshBound);\n this.offs.push(() => target.removeEventListener('scroll', onScroll), () => window.removeEventListener('resize', this.refreshBound));\n if (o.scrub && typeof o.scrub === 'number') this.offs.push(getTicker().add((_, dt) => this.smooth(dt)));\n scenes.add(this);\n this.refresh();\n }\n private refreshBound = () => this.refresh();\n\n private pos(): number {\n const s = this.scroller as any;\n if (s === window) return this.options.horizontal ? window.scrollX : window.scrollY;\n return this.options.horizontal ? s.scrollLeft : s.scrollTop;\n }\n private viewport(): number {\n const s = this.scroller as any;\n if (s === window) return this.options.horizontal ? window.innerWidth : window.innerHeight;\n return this.options.horizontal ? s.clientWidth : s.clientHeight;\n }\n\n /** Re-measure start / end (after layout changes). */\n refresh(): void {\n if (this.killed) return;\n const h = !!this.options.horizontal;\n const ref = (this.spacer || this.trigger) as Element;\n const r = ref.getBoundingClientRect();\n const sr = this.scroller === window ? { top: 0, left: 0 } : (this.scroller as Element).getBoundingClientRect();\n const tStart = (h ? r.left - sr.left : r.top - sr.top) + this.pos();\n const tSize = (this.pinEl ? (h ? this.pinEl.offsetWidth : this.pinEl.offsetHeight) : h ? r.width : r.height) || 0;\n const vp = this.viewport();\n this.start = resolveRule(this.options.start || (h ? 'left right' : 'top bottom'), tStart, tSize, vp);\n const e = this.options.end || (h ? 'right left' : 'bottom top');\n this.end = /^\\+=/.test(e.trim()) ? this.start + parseFloat(e.trim().slice(2)) : resolveRule(e, tStart, tSize, vp);\n if (this.end <= this.start) this.end = this.start + 1;\n if (this.spacer && this.pinEl) this.spacer.style[h ? 'paddingRight' : 'paddingBottom'] = `${this.end - this.start}px`;\n this.drawMarkers(vp);\n this.last = -1;\n this.update();\n }\n\n /** Read the scroll position and fire callbacks / drive the animation. */\n update(): void {\n if (this.killed) return;\n const y = this.pos();\n const p = Math.min(1, Math.max(0, (y - this.start) / (this.end - this.start)));\n if (y !== this.last && this.last >= 0) this.direction = y > this.last ? 1 : -1;\n const prev = this.progress;\n const was = this.isActive;\n this.progress = p;\n this.isActive = y > this.start && y < this.end;\n this.last = y;\n const o = this.options;\n if (this.isActive !== was || (prev === 0 && p === 1) || (prev === 1 && p === 0)) {\n const toggles: [keyof SceneOptions, number][] = [];\n if (p > 0 && prev === 0) toggles.push(['onEnter', 0]);\n if (p === 1 && prev < 1) toggles.push(['onLeave', 1]);\n if (p < 1 && prev === 1) toggles.push(['onEnterBack', 2]);\n if (p === 0 && prev > 0) toggles.push(['onLeaveBack', 3]);\n for (const [cb, i] of toggles) {\n (o[cb] as ((s: ScrollScene) => void) | undefined)?.(this);\n if (!o.scrub) this.act(i);\n }\n if (o.toggleClass) this.trigger.classList.toggle(o.toggleClass, this.isActive);\n o.onToggle?.(this);\n if (o.once && p === 1) {\n this.applyPin(1);\n this.kill(true);\n return;\n }\n }\n this.applyPin(p);\n if (o.scrub === true && o.animation) o.animation.progress = p;\n o.onUpdate?.(this);\n }\n\n private smooth(dt: number): void {\n const a = this.options.animation;\n if (!a) return;\n const k = 1 - Math.exp(-dt / Math.max(1, this.options.scrub as number));\n this.shown += (this.progress - this.shown) * k;\n if (Math.abs(this.progress - this.shown) < 0.0005) this.shown = this.progress;\n a.progress = this.shown;\n }\n\n private act(i: number): void {\n const a = this.options.animation;\n if (!a) return;\n const act = ((this.options.actions || 'play none none reverse').split(/\\s+/)[i] || 'none') as SceneAction;\n if (act === 'play') a.play();\n else if (act === 'pause') a.pause();\n else if (act === 'resume') a.play();\n else if (act === 'reverse') a.reverse();\n else if (act === 'restart') a.restart();\n else if (act === 'reset') (a.pause(), a.seek(0));\n else if (act === 'complete') (a.pause(), (a.progress = 1));\n }\n\n private setupPin(el: HTMLElement): void {\n this.pinEl = el;\n const sp = document.createElement('div');\n sp.className = 'usa-pin-spacer';\n sp.style.boxSizing = 'content-box';\n el.parentNode?.insertBefore(sp, el);\n sp.appendChild(el);\n this.spacer = sp;\n this.offs.push(() => {\n el.style.position = el.style.top = el.style.left = el.style.width = el.style.transform = '';\n sp.parentNode?.insertBefore(el, sp);\n sp.remove();\n });\n }\n\n private applyPin(p: number): void {\n const el = this.pinEl, sp = this.spacer;\n if (!el || !sp) return;\n const h = !!this.options.horizontal;\n if (p > 0 && p < 1 && this.scroller === window) {\n const r = sp.getBoundingClientRect();\n const offset = this.start - (h ? r.left + window.scrollX : r.top + window.scrollY);\n el.style.position = 'fixed';\n el.style.width = `${r.width}px`;\n el.style.left = h ? `${-offset}px` : `${r.left}px`;\n el.style.top = h ? `${r.top}px` : `${-offset}px`;\n el.style.transform = '';\n } else {\n el.style.position = el.style.top = el.style.left = el.style.width = '';\n // inside a scroll container (or after the end) pinning is done with a transform\n const d = p >= 1 ? this.end - this.start : p > 0 ? this.pos() - this.start : 0;\n el.style.transform = d ? (h ? `translateX(${d}px)` : `translateY(${d}px)`) : '';\n }\n }\n\n private drawMarkers(vp: number): void {\n this.marks.forEach((m) => m.remove());\n this.marks = [];\n const mk = this.options.markers;\n if (!mk) return;\n const color = (typeof mk === 'object' && mk.color) || '#e11d48';\n const h = !!this.options.horizontal;\n const host = this.scroller === window ? document.body : (this.scroller as HTMLElement);\n const line = (label: string, at: number, fixed: boolean, c: string) => {\n const d = document.createElement('div');\n d.className = 'usa-scroll-marker';\n d.textContent = label;\n d.setAttribute('aria-hidden', 'true');\n Object.assign(d.style, { position: fixed ? 'fixed' : 'absolute', [h ? 'left' : 'top']: `${at}px`, [h ? 'top' : 'right']: '0', [h ? 'height' : 'width']: '80px', [h ? 'borderLeft' : 'borderTop']: `2px solid ${c}`, color: c, font: '600 11px/1.2 ui-monospace,monospace', padding: '2px 4px', zIndex: '2147483646', pointerEvents: 'none', background: 'rgba(255,255,255,.7)' });\n (fixed ? document.body : host).appendChild(d);\n this.marks.push(d);\n };\n const rule = (r: string | undefined, d: string) => (r || d).trim().split(/\\s+(?![+-]=)/)[1] ?? 'bottom';\n line(`start ${this.start | 0}`, this.start, false, color);\n line(`end ${this.end | 0}`, this.end, false, color);\n line('scroller-start', parseEdge(rule(this.options.start, 'top bottom'), vp), true, '#16a34a');\n if (!/^\\+=/.test((this.options.end || '').trim())) line('scroller-end', parseEdge(rule(this.options.end, 'bottom top'), vp), true, '#2563eb');\n }\n\n /** Remove listeners, pin spacer and markers (`keepState` keeps the animation where it is). */\n kill(keepState = false): void {\n if (this.killed) return;\n this.killed = true;\n if (!keepState) this.options.animation?.pause();\n this.offs.splice(0).forEach((f) => f());\n this.marks.forEach((m) => m.remove());\n scenes.delete(this);\n }\n}\n\n/** Create a scroll scene. */\nexport function scrollScene(o: SceneOptions): ScrollScene {\n return new ScrollScene(o);\n}\n/** Re-measure every scene (call after layout changes such as images loading). */\nexport function refreshScenes(): void {\n scenes.forEach((s) => s.refresh());\n}\n/** Kill every scene. */\nexport function killScenes(): void {\n Array.from(scenes).forEach((s) => s.kill());\n}\n/** The live scenes. */\nexport const allScenes = (): ScrollScene[] => Array.from(scenes);\n\nexport interface ScrollApi {\n scrollScene: typeof scrollScene;\n refreshScenes: typeof refreshScenes;\n killScenes: typeof killScenes;\n allScenes: typeof allScenes;\n parseEdge: typeof parseEdge;\n resolveRule: typeof resolveRule;\n ScrollScene: typeof ScrollScene;\n}\n\n/** The module object for `use(scroll)`. */\nexport const scroll: RuntimeModule<ScrollApi> = { id: 'scroll', version: RUNTIME_VERSION, requires: ['core'], api: { scrollScene, refreshScenes, killScenes, allScenes, parseEdge, resolveRule, ScrollScene } };\n","// CDN build: load runtime.iife.js first, then this file → registers motionary/runtime/scroll and adds its API to window.MotionaryRuntime.\nimport { register } from '../registry';\nimport { scroll } from '../scroll';\nexport * from '../scroll';\nregister(scroll);\n"],"names":["KEYWORDS","top","left","center","bottom","right","parseEdge","spec","size","m","exec","trim","Error","v","b","endsWith","parseFloat","test","n","resolveRule","rule","triggerStart","triggerSize","viewport","parts","split","t","s","scenes","Set","q","x","el","document","querySelector","ScrollScene","constructor","o","this","start","end","progress","direction","isActive","shown","last","offs","pinEl","spacer","marks","killed","refreshBound","refresh","window","options","trigger","scroller","pin","setupPin","target","onScroll","update","addEventListener","passive","push","removeEventListener","scrub","getTicker","add","_","dt","smooth","pos","horizontal","scrollX","scrollY","scrollLeft","scrollTop","innerWidth","innerHeight","clientWidth","clientHeight","h","r","getBoundingClientRect","sr","tStart","tSize","offsetWidth","offsetHeight","width","height","vp","e","slice","style","drawMarkers","y","p","Math","min","max","prev","was","toggles","cb","i","act","toggleClass","classList","toggle","onToggle","once","applyPin","kill","animation","onUpdate","a","k","exp","abs","actions","play","pause","reverse","restart","seek","sp","createElement","className","boxSizing","parentNode","insertBefore","appendChild","position","transform","remove","offset","d","forEach","mk","markers","color","host","body","line","label","at","fixed","c","textContent","setAttribute","Object","assign","font","padding","zIndex","pointerEvents","background","keepState","splice","f","delete","scrollScene","refreshScenes","killScenes","Array","from","allScenes","scroll","id","version","RUNTIME_VERSION","requires","api","register"],"mappings":"8BAyDA,MAAMA,EAAmC,CAAEC,IAAK,EAAGC,KAAM,EAAGC,OAAQ,GAAKC,OAAQ,EAAGC,MAAO,GAGrF,SAAUC,EAAUC,EAAcC,GACtC,MAAMC,EAAI,2EAA2EC,KAAKH,EAAKI,QAC/F,IAAKF,EAAG,MAAM,IAAIG,MAAM,iCAAiCL,MACzD,IAAIM,EAAI,EACR,MAAMC,EAAIL,EAAE,IAAM,IAClB,GAAIK,KAAKd,EAAUa,EAAIb,EAASc,GAAKN,OAChC,GAAIM,EAAEC,SAAS,KAAMF,EAAKG,WAAWF,GAAK,IAAON,MACjD,KAAI,WAAWS,KAAKH,GACpB,MAAM,IAAIF,MAAM,iCAAiCL,MADzBM,EAAIG,WAAWF,EACkB,CAC9D,GAAIL,EAAE,GAAI,CACR,MAAMS,EAAa,MAATT,EAAE,GAAcO,WAAWP,EAAE,IAAM,IAAOD,EAAOQ,WAAWP,EAAE,IACxEI,GAAc,MAATJ,EAAE,GAAaS,GAAKA,CAC3B,CACA,OAAOL,CACT,CAGM,SAAUM,EAAYC,EAAcC,EAAsBC,EAAqBC,GACnF,MAAMC,EAAQJ,EAAKT,OAAOc,MAAM,gBAC1BC,EAAIF,EAAM,IAAM,MAChBG,EAAIH,EAAM,IAAM,SACtB,OAAOH,EAAef,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G,EAAOsI,OAAO5H,MAChB,EAII,SAAU6H,EAAY9H,GAC1B,OAAO,IAAIF,EAAYE,EACzB,UAEgB+H,IACdxI,EAAO+G,QAAShH,GAAMA,EAAEyB,UAC1B,UAEgBiH,IACdC,MAAMC,KAAK3I,GAAQ+G,QAAShH,GAAMA,EAAEuF,OACtC,CAEO,MAAMsD,EAAY,IAAqBF,MAAMC,KAAK3I,GAa5C6I,EAAmC,CAAEC,GAAI,SAAUC,QAASC,EAAAA,gBAAiBC,SAAU,CAAC,QAASC,IAAK,CAAEX,cAAaC,gBAAeC,aAAYG,YAAWlK,YAAWa,cAAagB,gBCrThM4I,EAAAA,SAASN"}
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { R as RUNTIME_VERSION } from '../chunks/registry-BIEjKngP.js';
|
|
2
|
+
import { g as getTicker } from '../chunks/ticker-CM_56oVy.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `motionary/runtime/scroll` (10.2) — scroll-linked scenes, an original
|
|
6
|
+
* implementation: start / end rules, scrubbing (direct or smoothed), pinning,
|
|
7
|
+
* debug markers, enter / leave callbacks and per-edge actions for a runtime
|
|
8
|
+
* tween or timeline.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { use, timeline } from 'motionary/runtime';
|
|
12
|
+
* import { scroll, scrollScene } from 'motionary/runtime/scroll';
|
|
13
|
+
* use(scroll);
|
|
14
|
+
* const tl = timeline({ paused: true }).to('.card', { to: { x: 200, rotate: 8 } });
|
|
15
|
+
* scrollScene({ trigger: '.section', start: 'top 80%', end: 'bottom 20%', scrub: 120, pin: true, markers: true, animation: tl });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Rules: `"<trigger edge> <viewport edge>"` where an edge is `top | center | bottom`,
|
|
19
|
+
* a percentage or pixels, optionally `+=N` / `-=N` (e.g. `"top top+=80"`);
|
|
20
|
+
* `end` may also be `"+=600"` (pixels after start). Horizontal scenes use
|
|
21
|
+
* `left | center | right`. Nothing touches `window` until a scene is created.
|
|
22
|
+
*/
|
|
23
|
+
const KEYWORDS = { top: 0, left: 0, center: 0.5, bottom: 1, right: 1 };
|
|
24
|
+
/** One side of a rule ('top', '80%', '120px', 'center+=40') → pixels within `size`. */
|
|
25
|
+
function parseEdge(spec, size) {
|
|
26
|
+
const m = /^([a-z]+|-?[\d.]+%|-?[\d.]+(?:px)?)?\s*(?:([+-])=\s*(-?[\d.]+)(px|%)?)?$/.exec(spec.trim());
|
|
27
|
+
if (!m)
|
|
28
|
+
throw new Error(`[motionary] scroll: bad edge "${spec}"`);
|
|
29
|
+
let v = 0;
|
|
30
|
+
const b = m[1] || '0';
|
|
31
|
+
if (b in KEYWORDS)
|
|
32
|
+
v = KEYWORDS[b] * size;
|
|
33
|
+
else if (b.endsWith('%'))
|
|
34
|
+
v = (parseFloat(b) / 100) * size;
|
|
35
|
+
else if (/^-?[\d.]/.test(b))
|
|
36
|
+
v = parseFloat(b);
|
|
37
|
+
else
|
|
38
|
+
throw new Error(`[motionary] scroll: bad edge "${spec}"`);
|
|
39
|
+
if (m[2]) {
|
|
40
|
+
const n = m[4] === '%' ? (parseFloat(m[3]) / 100) * size : parseFloat(m[3]);
|
|
41
|
+
v += m[2] === '+' ? n : -n;
|
|
42
|
+
}
|
|
43
|
+
return v;
|
|
44
|
+
}
|
|
45
|
+
/** Scroll offset at which `rule` is met: trigger edge (at `triggerStart`, size `triggerSize`) meets viewport edge. */
|
|
46
|
+
function resolveRule(rule, triggerStart, triggerSize, viewport) {
|
|
47
|
+
const parts = rule.trim().split(/\s+(?![+-]=)/);
|
|
48
|
+
const t = parts[0] ?? 'top';
|
|
49
|
+
const s = parts[1] ?? 'bottom';
|
|
50
|
+
return triggerStart + parseEdge(t, triggerSize) - parseEdge(s, viewport);
|
|
51
|
+
}
|
|
52
|
+
const scenes = new Set();
|
|
53
|
+
const q = (x) => {
|
|
54
|
+
const el = typeof x === 'string' ? document.querySelector(x) : x;
|
|
55
|
+
if (!el)
|
|
56
|
+
throw new Error(`[motionary] scroll: element "${x}" not found`);
|
|
57
|
+
return el;
|
|
58
|
+
};
|
|
59
|
+
/** A scroll scene (see `scrollScene()`). */
|
|
60
|
+
class ScrollScene {
|
|
61
|
+
constructor(o) {
|
|
62
|
+
/** Scroll offsets (px) of the start and end. */
|
|
63
|
+
this.start = 0;
|
|
64
|
+
this.end = 0;
|
|
65
|
+
/** 0 → 1 between start and end. */
|
|
66
|
+
this.progress = 0;
|
|
67
|
+
/** 1 scrolling forward, -1 back. */
|
|
68
|
+
this.direction = 1;
|
|
69
|
+
this.isActive = false;
|
|
70
|
+
this.shown = 0;
|
|
71
|
+
this.last = -1;
|
|
72
|
+
this.offs = [];
|
|
73
|
+
this.pinEl = null;
|
|
74
|
+
this.spacer = null;
|
|
75
|
+
this.marks = [];
|
|
76
|
+
this.killed = false;
|
|
77
|
+
this.refreshBound = () => this.refresh();
|
|
78
|
+
if (typeof window === 'undefined')
|
|
79
|
+
throw new Error('[motionary] scroll scenes need a browser window');
|
|
80
|
+
this.options = o;
|
|
81
|
+
this.trigger = q(o.trigger);
|
|
82
|
+
this.scroller = o.scroller ? q(o.scroller) : window;
|
|
83
|
+
if (o.pin)
|
|
84
|
+
this.setupPin(o.pin === true ? this.trigger : q(o.pin));
|
|
85
|
+
const target = this.scroller;
|
|
86
|
+
const onScroll = () => this.update();
|
|
87
|
+
target.addEventListener('scroll', onScroll, { passive: true });
|
|
88
|
+
window.addEventListener('resize', this.refreshBound);
|
|
89
|
+
this.offs.push(() => target.removeEventListener('scroll', onScroll), () => window.removeEventListener('resize', this.refreshBound));
|
|
90
|
+
if (o.scrub && typeof o.scrub === 'number')
|
|
91
|
+
this.offs.push(getTicker().add((_, dt) => this.smooth(dt)));
|
|
92
|
+
scenes.add(this);
|
|
93
|
+
this.refresh();
|
|
94
|
+
}
|
|
95
|
+
pos() {
|
|
96
|
+
const s = this.scroller;
|
|
97
|
+
if (s === window)
|
|
98
|
+
return this.options.horizontal ? window.scrollX : window.scrollY;
|
|
99
|
+
return this.options.horizontal ? s.scrollLeft : s.scrollTop;
|
|
100
|
+
}
|
|
101
|
+
viewport() {
|
|
102
|
+
const s = this.scroller;
|
|
103
|
+
if (s === window)
|
|
104
|
+
return this.options.horizontal ? window.innerWidth : window.innerHeight;
|
|
105
|
+
return this.options.horizontal ? s.clientWidth : s.clientHeight;
|
|
106
|
+
}
|
|
107
|
+
/** Re-measure start / end (after layout changes). */
|
|
108
|
+
refresh() {
|
|
109
|
+
if (this.killed)
|
|
110
|
+
return;
|
|
111
|
+
const h = !!this.options.horizontal;
|
|
112
|
+
const ref = (this.spacer || this.trigger);
|
|
113
|
+
const r = ref.getBoundingClientRect();
|
|
114
|
+
const sr = this.scroller === window ? { top: 0, left: 0 } : this.scroller.getBoundingClientRect();
|
|
115
|
+
const tStart = (h ? r.left - sr.left : r.top - sr.top) + this.pos();
|
|
116
|
+
const tSize = (this.pinEl ? (h ? this.pinEl.offsetWidth : this.pinEl.offsetHeight) : h ? r.width : r.height) || 0;
|
|
117
|
+
const vp = this.viewport();
|
|
118
|
+
this.start = resolveRule(this.options.start || (h ? 'left right' : 'top bottom'), tStart, tSize, vp);
|
|
119
|
+
const e = this.options.end || (h ? 'right left' : 'bottom top');
|
|
120
|
+
this.end = /^\+=/.test(e.trim()) ? this.start + parseFloat(e.trim().slice(2)) : resolveRule(e, tStart, tSize, vp);
|
|
121
|
+
if (this.end <= this.start)
|
|
122
|
+
this.end = this.start + 1;
|
|
123
|
+
if (this.spacer && this.pinEl)
|
|
124
|
+
this.spacer.style[h ? 'paddingRight' : 'paddingBottom'] = `${this.end - this.start}px`;
|
|
125
|
+
this.drawMarkers(vp);
|
|
126
|
+
this.last = -1;
|
|
127
|
+
this.update();
|
|
128
|
+
}
|
|
129
|
+
/** Read the scroll position and fire callbacks / drive the animation. */
|
|
130
|
+
update() {
|
|
131
|
+
if (this.killed)
|
|
132
|
+
return;
|
|
133
|
+
const y = this.pos();
|
|
134
|
+
const p = Math.min(1, Math.max(0, (y - this.start) / (this.end - this.start)));
|
|
135
|
+
if (y !== this.last && this.last >= 0)
|
|
136
|
+
this.direction = y > this.last ? 1 : -1;
|
|
137
|
+
const prev = this.progress;
|
|
138
|
+
const was = this.isActive;
|
|
139
|
+
this.progress = p;
|
|
140
|
+
this.isActive = y > this.start && y < this.end;
|
|
141
|
+
this.last = y;
|
|
142
|
+
const o = this.options;
|
|
143
|
+
if (this.isActive !== was || (prev === 0 && p === 1) || (prev === 1 && p === 0)) {
|
|
144
|
+
const toggles = [];
|
|
145
|
+
if (p > 0 && prev === 0)
|
|
146
|
+
toggles.push(['onEnter', 0]);
|
|
147
|
+
if (p === 1 && prev < 1)
|
|
148
|
+
toggles.push(['onLeave', 1]);
|
|
149
|
+
if (p < 1 && prev === 1)
|
|
150
|
+
toggles.push(['onEnterBack', 2]);
|
|
151
|
+
if (p === 0 && prev > 0)
|
|
152
|
+
toggles.push(['onLeaveBack', 3]);
|
|
153
|
+
for (const [cb, i] of toggles) {
|
|
154
|
+
o[cb]?.(this);
|
|
155
|
+
if (!o.scrub)
|
|
156
|
+
this.act(i);
|
|
157
|
+
}
|
|
158
|
+
if (o.toggleClass)
|
|
159
|
+
this.trigger.classList.toggle(o.toggleClass, this.isActive);
|
|
160
|
+
o.onToggle?.(this);
|
|
161
|
+
if (o.once && p === 1) {
|
|
162
|
+
this.applyPin(1);
|
|
163
|
+
this.kill(true);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
this.applyPin(p);
|
|
168
|
+
if (o.scrub === true && o.animation)
|
|
169
|
+
o.animation.progress = p;
|
|
170
|
+
o.onUpdate?.(this);
|
|
171
|
+
}
|
|
172
|
+
smooth(dt) {
|
|
173
|
+
const a = this.options.animation;
|
|
174
|
+
if (!a)
|
|
175
|
+
return;
|
|
176
|
+
const k = 1 - Math.exp(-dt / Math.max(1, this.options.scrub));
|
|
177
|
+
this.shown += (this.progress - this.shown) * k;
|
|
178
|
+
if (Math.abs(this.progress - this.shown) < 0.0005)
|
|
179
|
+
this.shown = this.progress;
|
|
180
|
+
a.progress = this.shown;
|
|
181
|
+
}
|
|
182
|
+
act(i) {
|
|
183
|
+
const a = this.options.animation;
|
|
184
|
+
if (!a)
|
|
185
|
+
return;
|
|
186
|
+
const act = ((this.options.actions || 'play none none reverse').split(/\s+/)[i] || 'none');
|
|
187
|
+
if (act === 'play')
|
|
188
|
+
a.play();
|
|
189
|
+
else if (act === 'pause')
|
|
190
|
+
a.pause();
|
|
191
|
+
else if (act === 'resume')
|
|
192
|
+
a.play();
|
|
193
|
+
else if (act === 'reverse')
|
|
194
|
+
a.reverse();
|
|
195
|
+
else if (act === 'restart')
|
|
196
|
+
a.restart();
|
|
197
|
+
else if (act === 'reset')
|
|
198
|
+
(a.pause(), a.seek(0));
|
|
199
|
+
else if (act === 'complete')
|
|
200
|
+
(a.pause(), (a.progress = 1));
|
|
201
|
+
}
|
|
202
|
+
setupPin(el) {
|
|
203
|
+
this.pinEl = el;
|
|
204
|
+
const sp = document.createElement('div');
|
|
205
|
+
sp.className = 'usa-pin-spacer';
|
|
206
|
+
sp.style.boxSizing = 'content-box';
|
|
207
|
+
el.parentNode?.insertBefore(sp, el);
|
|
208
|
+
sp.appendChild(el);
|
|
209
|
+
this.spacer = sp;
|
|
210
|
+
this.offs.push(() => {
|
|
211
|
+
el.style.position = el.style.top = el.style.left = el.style.width = el.style.transform = '';
|
|
212
|
+
sp.parentNode?.insertBefore(el, sp);
|
|
213
|
+
sp.remove();
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
applyPin(p) {
|
|
217
|
+
const el = this.pinEl, sp = this.spacer;
|
|
218
|
+
if (!el || !sp)
|
|
219
|
+
return;
|
|
220
|
+
const h = !!this.options.horizontal;
|
|
221
|
+
if (p > 0 && p < 1 && this.scroller === window) {
|
|
222
|
+
const r = sp.getBoundingClientRect();
|
|
223
|
+
const offset = this.start - (h ? r.left + window.scrollX : r.top + window.scrollY);
|
|
224
|
+
el.style.position = 'fixed';
|
|
225
|
+
el.style.width = `${r.width}px`;
|
|
226
|
+
el.style.left = h ? `${-offset}px` : `${r.left}px`;
|
|
227
|
+
el.style.top = h ? `${r.top}px` : `${-offset}px`;
|
|
228
|
+
el.style.transform = '';
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
el.style.position = el.style.top = el.style.left = el.style.width = '';
|
|
232
|
+
// inside a scroll container (or after the end) pinning is done with a transform
|
|
233
|
+
const d = p >= 1 ? this.end - this.start : p > 0 ? this.pos() - this.start : 0;
|
|
234
|
+
el.style.transform = d ? (h ? `translateX(${d}px)` : `translateY(${d}px)`) : '';
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
drawMarkers(vp) {
|
|
238
|
+
this.marks.forEach((m) => m.remove());
|
|
239
|
+
this.marks = [];
|
|
240
|
+
const mk = this.options.markers;
|
|
241
|
+
if (!mk)
|
|
242
|
+
return;
|
|
243
|
+
const color = (typeof mk === 'object' && mk.color) || '#e11d48';
|
|
244
|
+
const h = !!this.options.horizontal;
|
|
245
|
+
const host = this.scroller === window ? document.body : this.scroller;
|
|
246
|
+
const line = (label, at, fixed, c) => {
|
|
247
|
+
const d = document.createElement('div');
|
|
248
|
+
d.className = 'usa-scroll-marker';
|
|
249
|
+
d.textContent = label;
|
|
250
|
+
d.setAttribute('aria-hidden', 'true');
|
|
251
|
+
Object.assign(d.style, { position: fixed ? 'fixed' : 'absolute', [h ? 'left' : 'top']: `${at}px`, [h ? 'top' : 'right']: '0', [h ? 'height' : 'width']: '80px', [h ? 'borderLeft' : 'borderTop']: `2px solid ${c}`, color: c, font: '600 11px/1.2 ui-monospace,monospace', padding: '2px 4px', zIndex: '2147483646', pointerEvents: 'none', background: 'rgba(255,255,255,.7)' });
|
|
252
|
+
(fixed ? document.body : host).appendChild(d);
|
|
253
|
+
this.marks.push(d);
|
|
254
|
+
};
|
|
255
|
+
const rule = (r, d) => (r || d).trim().split(/\s+(?![+-]=)/)[1] ?? 'bottom';
|
|
256
|
+
line(`start ${this.start | 0}`, this.start, false, color);
|
|
257
|
+
line(`end ${this.end | 0}`, this.end, false, color);
|
|
258
|
+
line('scroller-start', parseEdge(rule(this.options.start, 'top bottom'), vp), true, '#16a34a');
|
|
259
|
+
if (!/^\+=/.test((this.options.end || '').trim()))
|
|
260
|
+
line('scroller-end', parseEdge(rule(this.options.end, 'bottom top'), vp), true, '#2563eb');
|
|
261
|
+
}
|
|
262
|
+
/** Remove listeners, pin spacer and markers (`keepState` keeps the animation where it is). */
|
|
263
|
+
kill(keepState = false) {
|
|
264
|
+
if (this.killed)
|
|
265
|
+
return;
|
|
266
|
+
this.killed = true;
|
|
267
|
+
if (!keepState)
|
|
268
|
+
this.options.animation?.pause();
|
|
269
|
+
this.offs.splice(0).forEach((f) => f());
|
|
270
|
+
this.marks.forEach((m) => m.remove());
|
|
271
|
+
scenes.delete(this);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/** Create a scroll scene. */
|
|
275
|
+
function scrollScene(o) {
|
|
276
|
+
return new ScrollScene(o);
|
|
277
|
+
}
|
|
278
|
+
/** Re-measure every scene (call after layout changes such as images loading). */
|
|
279
|
+
function refreshScenes() {
|
|
280
|
+
scenes.forEach((s) => s.refresh());
|
|
281
|
+
}
|
|
282
|
+
/** Kill every scene. */
|
|
283
|
+
function killScenes() {
|
|
284
|
+
Array.from(scenes).forEach((s) => s.kill());
|
|
285
|
+
}
|
|
286
|
+
/** The live scenes. */
|
|
287
|
+
const allScenes = () => Array.from(scenes);
|
|
288
|
+
/** The module object for `use(scroll)`. */
|
|
289
|
+
const scroll = { id: 'scroll', version: RUNTIME_VERSION, requires: ['core'], api: { scrollScene, refreshScenes, killScenes, allScenes, parseEdge, resolveRule, ScrollScene } };
|
|
290
|
+
|
|
291
|
+
export { ScrollScene, allScenes, killScenes, parseEdge, refreshScenes, resolveRule, scroll, scrollScene };
|
|
292
|
+
//# sourceMappingURL=scroll.js.map
|