img-auto-parallax 0.0.1 → 0.1.1

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/README.md CHANGED
@@ -1,14 +1,10 @@
1
1
  # img-auto-parallax
2
2
 
3
- A drop-in `<img>` replacement that estimates depth on the GPU and shifts the scene's layers as you hover, or as you tilt your phone. Without WebGPU, or with `disabled`, it is simply the image.
3
+ A drop-in `<img>` that estimates depth on the GPU and gives any photo parallax as you hover or tilt your phone. Without WebGPU, it is simply the image.
4
4
 
5
- - **Drop-in**: forwards every `<img>` attribute (`src`, `srcset`, `alt`, `loading`, `width`/`height`…), sizes like an image, and honours `object-fit` (`fill` · `contain` · `cover`).
6
- - **Graceful**: plain image until depth is ready; stays a plain image without WebGPU or for non-CORS images.
7
- - **Self-contained**: ships Depth Anything V2 small and FastDepth, so no third-party model host is needed by default.
8
- - **GPU-resident**: ONNX Runtime Web on WebGPU; depth never round-trips through the CPU ([vgpu](https://vgpu.sh) wraps the output buffer).
9
- - **Configurable**: strength, focus depth, depth-of-field, smoothing, trigger (self-hover / page-hover / gyro / manual), idle sway, invert, resolution, three built-in models or your own.
5
+ [![img-auto-parallax: a plain image beside the same photo with depth and parallax](https://img-auto-parallax.vercel.app/og.gif)](https://img-auto-parallax.vercel.app)
10
6
 
11
- ## Install
7
+ **[Documentation & playground →](https://img-auto-parallax.vercel.app)** · **[Watch the intro video ▶](https://img-auto-parallax.vercel.app/intro.mp4)**
12
8
 
13
9
  ```sh
14
10
  pnpm add img-auto-parallax
@@ -19,89 +15,15 @@ import 'img-auto-parallax/define'
19
15
  ```
20
16
 
21
17
  ```html
22
- <img-auto-parallax src="/photo.jpg" alt="A mountain lake" width="1280" height="960"></img-auto-parallax>
18
+ <img-auto-parallax src="/photo.jpg" alt="A mountain lake"></img-auto-parallax>
23
19
  ```
24
20
 
25
- With Vite, keep the package out of dependency pre-bundling so the bundled model and ONNX Runtime's `.wasm` resolve:
21
+ Powered by [vgpu](https://vgpu.sh) by Vercel.
26
22
 
27
- ```ts
28
- export default defineConfig({
29
- optimizeDeps: {
30
- exclude: ['img-auto-parallax', 'onnxruntime-web']
31
- },
32
- })
33
- ```
34
-
35
- ## Attributes
36
-
37
- | Attribute | Default | Description |
38
- | --- | --- | --- |
39
- | `disabled` | `false` | Behave exactly like `<img>`. |
40
- | `strength` | `0.03` | Max displacement, fraction of image height (0–0.3). |
41
- | `focus-depth` | `0.5` | Depth that stays still: 0 background … 1 foreground. |
42
- | `smoothing` | `150` | Easing time constant in ms. |
43
- | `trigger` | `auto` | `self-hover` (pointer over the image) · `page-hover` (pointer anywhere on the page) · `gyro` · `both` (self-hover + gyro) · `manual` · `auto` (gyro on touch, else self-hover). |
44
- | `idle` | `none` | `sway` drifts gently while idle. |
45
- | `invert` | `false` | Reverse the motion. |
46
- | `resolution` | `518` | Long side of the model input in px. |
47
- | `src-depth` | — | Precompiled depth map; skips the model entirely. |
48
- | `model` | `configure()` | `depth-anything` (bundled), `midas` (on demand), `fastdepth` (bundled), or a `registerModel()` name. |
49
- | `view` | `image` | `depth` shows the depth map. |
50
- | `defocus` | `0` | Depth of field (0–1); focus follows what is under the pointer. |
51
- | `refocus` | `false` | With `defocus`, bring the whole image back into focus while the pointer is not over it. |
52
-
53
- The smoothed pointer is exposed as `--img-auto-parallax-x` / `--img-auto-parallax-y` (−1…1) for custom effects, plus `data-active` while someone is steering. Style the inner image and canvas with `::part(img | canvas)`.
54
-
55
- Methods: `setPointer(x, y)` / `setPointer(null)`, `refresh()`. State: `state` / `data-state` (`static` · `loading` · `ready` · `unsupported` · `error`). Events: `load`, `error`, `parallax-ready`, `parallax-error`.
56
-
57
- ## Precompiled depth maps
58
-
59
- Skip the model in the browser entirely: generate depth maps at build time and point each image at its map. Nothing but the photo and a small PNG is downloaded, and no ONNX Runtime or wasm loads.
60
-
61
- ```html
62
- <img-auto-parallax src="/fox.jpg" src-depth="/fox.depth.png"></img-auto-parallax>
63
- ```
64
-
65
- Generate them with the Vite plugin (runs at dev and build start, cached by content):
66
-
67
- ```ts
68
- import { depthMaps } from 'img-auto-parallax/vite'
69
-
70
- export default defineConfig({
71
- plugins: [
72
- depthMaps({ include: 'public/images/*.jpg' })
73
- ],
74
- })
75
- ```
76
-
77
- or the CLI, or the Node API (`img-auto-parallax/node`: `precompute()`, `generateDepthMap()`):
78
-
79
- ```sh
80
- npx img-auto-parallax precompute "public/images/*.jpg" [--model depth-anything] [--resolution 518] [--force]
81
- ```
82
-
83
- Each `photo.jpg` gets a `photo.depth.png` (grayscale, near = bright), 1:1 with the photo up to 2048 px, with depth edges snapped to the photo's edges by joint bilateral upsampling (the same refinement the GPU applies live). Maps of other sizes also work; they are upsampled on the GPU. Inference runs on the CPU in Node (≈4 s per image); JPEG and PNG inputs are supported. Requires Node 22+.
84
-
85
- ## Configuration
86
-
87
- ```ts
88
- import { configure, defineImgAutoParallax, preload } from 'img-auto-parallax'
89
-
90
- configure({
91
- model: 'depth-anything', // default for elements without `model`; can change any time
92
- onProgress: ({ loaded, total }) => {},
93
- })
94
- defineImgAutoParallax() // or defineImgAutoParallax('my-tag')
95
- preload() // optional: start loading before an image is visible
96
- ```
97
-
98
- Built-in models:
23
+ ## Requirements
99
24
 
100
- | Name | Model | Size | Source |
101
- | --- | --- | --- | --- |
102
- | `depth-anything` | Depth Anything V2 small (Apache-2.0) | 19 MB | bundled (q4f16); GPUs without `shader-f16` load the 27 MB q4 build from Hugging Face |
103
- | `midas` | MiDaS v2.1 small (MIT) | 64 MB | loaded on demand from vgpu.sh |
104
- | `fastdepth` | FastDepth (MIT) | 5 MB | bundled |
25
+ - **HTTPS**: WebGPU (and motion events) only exist in secure contexts. Serve over `https://`, or `localhost` in development; otherwise images stay plain.
26
+ - **Gyro permission**: iOS Safari asks before sharing device motion, and only from a user gesture. A tap on an image asks automatically; to ask yourself, use `getGyroPermission()` and `requestGyroPermission()` from a click handler.
105
27
 
106
28
  ## Development
107
29
 
package/dist/define.mjs CHANGED
@@ -1,3 +1,3 @@
1
- import { n as defineImgAutoParallax } from "./src-Bd-5bi4S.mjs";
1
+ import { n as defineImgAutoParallax } from "./src-BiTfnOJ8.mjs";
2
2
  // src/define.ts
3
3
  defineImgAutoParallax();
@@ -0,0 +1,292 @@
1
+ import "vgpu";
2
+ /** Describes an ONNX depth model the runtime can drive. */
3
+ interface DepthModelSpec {
4
+ /** Location of the `.onnx` file. */
5
+ url: string | URL;
6
+ /** Graph input name (NCHW float32). */
7
+ inputName: string;
8
+ /** Graph output name (float32, `[1, H, W]` or `[1, 1, H, W]`). */
9
+ outputName: string;
10
+ /**
11
+ * Input sizes are rounded to a multiple of this (the ViT patch size for
12
+ * Depth Anything). Ignored when `fixedSize` is set.
13
+ */
14
+ multiple?: number;
15
+ /** Fixed `[width, height]` input. When omitted, the input follows each image's aspect ratio. */
16
+ fixedSize?: readonly [number, number];
17
+ /** How RGB is normalised before inference: ImageNet mean/std, or plain 0–1. */
18
+ normalization: 'imagenet' | 'unit';
19
+ /**
20
+ * How to read the output. `relative`: larger values are nearer (inverse
21
+ * depth, auto-ranged per image). `metric`: metres, mapped with a log scale.
22
+ */
23
+ output: {
24
+ kind: 'relative';
25
+ } | {
26
+ kind: 'metric';
27
+ near: number;
28
+ far: number;
29
+ };
30
+ /** Needs the WebGPU `shader-f16` feature (fp16 weights). */
31
+ requiresF16?: boolean;
32
+ /** Used instead when `requiresF16` is set and the GPU lacks `shader-f16`. */
33
+ fallback?: DepthModelSpec;
34
+ /** Human-readable name, for pickers and logs. */
35
+ label?: string;
36
+ /** Download size in bytes, when known. */
37
+ bytes?: number;
38
+ }
39
+ type DepthModelPreset = 'depth-anything' | 'midas' | 'fastdepth';
40
+ /**
41
+ * The built-in models. Bundlers that understand `new URL(…, import.meta.url)`
42
+ * (Vite, webpack 5, Rollup) emit the bundled files as assets automatically.
43
+ */
44
+ declare const MODEL_PRESETS: Record<DepthModelPreset, DepthModelSpec>;
45
+ declare const DEFAULT_MODEL: DepthModelPreset;
46
+ /**
47
+ * Register a custom model under a name, so elements can pick it with
48
+ * `model="name"`. Re-registering a name replaces it for future estimates.
49
+ */
50
+ declare function registerModel(name: string, spec: DepthModelSpec): void;
51
+ /** Names usable in the `model` attribute: the presets and registered models. */
52
+ declare function listModels(): string[];
53
+ /** A preset or registered name, or a full spec. */
54
+ type DepthModelOption = DepthModelPreset | (string & {}) | DepthModelSpec;
55
+ /** True on devices without a hovering pointer that expose orientation events. */
56
+ declare function prefersGyro(): boolean;
57
+ /** Called when `getGyroPermission()` changes, e.g. after a grant or once readings arrive. */
58
+ declare function onGyroPermissionChange(listener: (permission: GyroPermission) => void): () => void;
59
+ /**
60
+ * Gyro access on this device:
61
+ * - `unavailable`: no orientation events (most desktops)
62
+ * - `prompt`: the browser asks first (iOS Safari); call `requestGyroPermission()` from a tap
63
+ * - `granted` / `denied`: the answer, for this page load
64
+ * - `not-required`: events flow without asking (Android, most others)
65
+ */
66
+ type GyroPermission = 'unavailable' | 'prompt' | 'granted' | 'denied' | 'not-required';
67
+ declare function getGyroPermission(): GyroPermission;
68
+ /**
69
+ * Ask for gyro access. Must run inside a user gesture (a tap or click handler),
70
+ * or iOS rejects it. Elements start following the tilt as soon as it is granted.
71
+ */
72
+ declare function requestGyroPermission(): Promise<GyroPermission>;
73
+ interface ModelProgress {
74
+ /** URL of the model being downloaded. */
75
+ url: string;
76
+ /** Bytes downloaded so far. */
77
+ loaded: number;
78
+ /** Total bytes, when the server reports it. */
79
+ total?: number;
80
+ }
81
+ interface ImgAutoParallaxConfig {
82
+ /**
83
+ * Default model for elements without a `model` attribute:
84
+ * `'depth-anything'` (default), `'midas'`, `'fastdepth'`, a registered name, or a spec.
85
+ */
86
+ model: DepthModelOption;
87
+ /** Override where ONNX Runtime loads its `.wasm` files from. Only read before the first load. */
88
+ wasmPaths?: string;
89
+ /** Model download progress. */
90
+ onProgress?: (progress: ModelProgress) => void;
91
+ }
92
+ /**
93
+ * Configure the shared runtime. Can be called at any time; changing `model`
94
+ * re-estimates every element that has no `model` attribute of its own.
95
+ */
96
+ declare function configure(options: Partial<ImgAutoParallaxConfig>): void;
97
+ /** Synchronous check: does this browser expose WebGPU at all? */
98
+ declare function hasWebGpu(): boolean;
99
+ /** Resolves `true` once the given (or default) model runs on this device, `false` if it cannot. */
100
+ declare function isSupported(model?: DepthModelOption): Promise<boolean>;
101
+ /** Start downloading a model and creating its GPU session ahead of time. */
102
+ declare function preload(model?: DepthModelOption): Promise<void>;
103
+ /**
104
+ * - `static` plain image: disabled, or out of view (GPU resources released)
105
+ * - `loading` estimating depth for the current image (or loading the model)
106
+ * - `ready` parallax active
107
+ * - `unsupported` no WebGPU, or the model failed to load: plain image
108
+ * - `error` this image could not be used (e.g. cross-origin without CORS): plain image
109
+ */
110
+ type ParallaxState = 'static' | 'loading' | 'ready' | 'unsupported' | 'error';
111
+ /**
112
+ * What drives the parallax:
113
+ * - `self-hover`: the pointer over this image
114
+ * - `page-hover`: the pointer anywhere on the page, relative to this image
115
+ * - `gyro`: device tilt
116
+ * - `both`: self-hover and gyro
117
+ * - `manual`: only `setPointer()`
118
+ * - `auto` (default): gyro on touch devices, self-hover elsewhere
119
+ *
120
+ * Combine them with spaces, e.g. `trigger="page-hover gyro"`.
121
+ */
122
+ type ParallaxTrigger = 'auto' | 'self-hover' | 'page-hover' | 'gyro' | 'both' | 'manual' | (string & {});
123
+ /** Motion when nobody is interacting. */
124
+ type ParallaxIdle = 'none' | 'sway';
125
+ /** What the canvas shows: the photo, or the estimated depth (near = bright). */
126
+ type ParallaxView = 'image' | 'depth';
127
+ /**
128
+ * What the element draws:
129
+ * - `image` (default): the photo with depth parallax
130
+ * - `foreground`: only what is nearer than `cutoff`, transparent elsewhere
131
+ * - `flat`: the whole photo moving rigidly with a `foreground` copy, as the plate behind it
132
+ * - `background`: only what is farther than `cutoff`, the foreground filled in from
133
+ * around it, moving at its own depth so a `foreground` copy slides over it
134
+ */
135
+ type ParallaxLayer = 'image' | 'foreground' | 'flat' | 'background';
136
+ /**
137
+ * The parallax options of `<img-auto-parallax>`, as its camelCase properties.
138
+ * Each is also an attribute in kebab-case (`focusDepth` → `focus-depth`).
139
+ * Everything is optional; leaving one out uses the default (see `DEFAULTS`).
140
+ */
141
+ interface ParallaxOptions {
142
+ /** Plain image when true: no model, no GPU work. */
143
+ disabled?: boolean;
144
+ /**
145
+ * A precompiled depth map (grayscale, near = bright, any size), e.g. from
146
+ * `img-auto-parallax precompute`. When set, no model or ONNX Runtime is loaded.
147
+ */
148
+ srcDepth?: string | null;
149
+ /** Model for this element: a preset or a `registerModel()` name. Defaults to `configure({ model })`. */
150
+ model?: DepthModelPreset | (string & {}) | null;
151
+ /** Maximum displacement as a fraction of the image height (0–0.3, default 0.03). */
152
+ strength?: number;
153
+ /** Nearness of the plane that stays still: 0 = background, 1 = foreground (default 0.5). */
154
+ focusDepth?: number;
155
+ /** Easing time constant in ms; 0 follows the input immediately (default 150). */
156
+ smoothing?: number;
157
+ /** Long side of the depth model input in px; higher is sharper and slower (default 518). */
158
+ resolution?: number;
159
+ /** What drives the effect (default `auto`). */
160
+ trigger?: ParallaxTrigger;
161
+ /** Reverse the direction of motion. */
162
+ invert?: boolean;
163
+ /** Motion while nobody interacts (default `none`). */
164
+ idle?: ParallaxIdle;
165
+ /** `depth` shows the estimated depth map instead of the photo (default `image`). */
166
+ view?: ParallaxView;
167
+ /** Depth of field (0–1, default 0 = off). Focus follows the pointer; at rest it falls back to `focusDepth`. */
168
+ defocus?: number;
169
+ /** Bring the whole image back into focus while the pointer is not over it. */
170
+ refocus?: boolean;
171
+ /** Which part of the photo to draw (default `image`); stack layers for a lock-screen depth effect. */
172
+ layer?: ParallaxLayer;
173
+ /** Nearness threshold between `layer="foreground"` and `layer="background"` (0–1, default 0.6). */
174
+ cutoff?: number;
175
+ }
176
+ /** `detail` of the `parallax-error` event. */
177
+ interface ParallaxErrorDetail {
178
+ /** The state the element fell back to. */
179
+ state: 'unsupported' | 'error';
180
+ error: unknown;
181
+ }
182
+ /** Events `<img-auto-parallax>` dispatches, besides the standard ones. */
183
+ interface ImgAutoParallaxEventMap extends HTMLElementEventMap {
184
+ /** Depth is ready and the effect is live. */
185
+ 'parallax-ready': Event;
186
+ /** Fell back to a plain image. */
187
+ 'parallax-error': CustomEvent<ParallaxErrorDetail>;
188
+ }
189
+ declare const DEFAULTS: {
190
+ strength: number;
191
+ focusDepth: number;
192
+ smoothing: number;
193
+ resolution: number;
194
+ trigger: ParallaxTrigger;
195
+ idle: ParallaxIdle;
196
+ view: ParallaxView;
197
+ defocus: number;
198
+ layer: ParallaxLayer;
199
+ cutoff: number;
200
+ };
201
+ interface ImgAutoParallaxElement {
202
+ addEventListener: (<K extends keyof ImgAutoParallaxEventMap>(type: K, listener: (this: ImgAutoParallaxElement, event: ImgAutoParallaxEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions) => void) & ((type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions) => void);
203
+ removeEventListener: (<K extends keyof ImgAutoParallaxEventMap>(type: K, listener: (this: ImgAutoParallaxElement, event: ImgAutoParallaxEventMap[K]) => unknown, options?: boolean | EventListenerOptions) => void) & ((type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions) => void);
204
+ }
205
+ /**
206
+ * `<img-auto-parallax>`: an `<img>` that estimates depth on the GPU and
207
+ * shifts its layers with the pointer or device tilt. Falls back to the plain
208
+ * image when disabled, without WebGPU, or when the image cannot be read.
209
+ */
210
+ declare class ImgAutoParallaxElement extends HTMLElement implements Required<ParallaxOptions> {
211
+ #private;
212
+ static readonly observedAttributes: string[];
213
+ constructor();
214
+ get src(): string;
215
+ set src(value: string);
216
+ get alt(): string;
217
+ set alt(value: string);
218
+ get currentSrc(): string;
219
+ get complete(): boolean;
220
+ get naturalWidth(): number;
221
+ get naturalHeight(): number;
222
+ decode(): Promise<void>;
223
+ /** Plain image when true: no model, no GPU work. */
224
+ get disabled(): boolean;
225
+ set disabled(value: boolean);
226
+ /** Model for this element: `auto`, a preset (`q4f16`, `q4`, `fp16`, `fp32`) or a registered name. Defaults to `configure({ model })`. */
227
+ /**
228
+ * A precompiled depth map (grayscale, near = bright, any size), e.g. from
229
+ * `img-auto-parallax precompute`. When set, no model or ONNX Runtime is loaded.
230
+ */
231
+ get srcDepth(): string | null;
232
+ set srcDepth(value: string | null | undefined);
233
+ get model(): string | null;
234
+ set model(value: string | null);
235
+ /** Maximum displacement as a fraction of the image height (0–0.3, default 0.03). */
236
+ get strength(): number;
237
+ set strength(value: number);
238
+ /** Nearness of the plane that stays still: 0 = background, 1 = foreground (default 0.5). */
239
+ get focusDepth(): number;
240
+ set focusDepth(value: number);
241
+ /** Easing time constant in ms; 0 follows the input immediately (default 150). */
242
+ get smoothing(): number;
243
+ set smoothing(value: number);
244
+ /** Long side of the depth model input in px; higher is sharper and slower (default 518). */
245
+ get resolution(): number;
246
+ set resolution(value: number);
247
+ /** What drives the effect (default `auto`). */
248
+ get trigger(): ParallaxTrigger;
249
+ set trigger(value: ParallaxTrigger);
250
+ /** Reverse the direction of motion. */
251
+ get invert(): boolean;
252
+ set invert(value: boolean);
253
+ /** Motion while nobody interacts (default `none`). */
254
+ get idle(): ParallaxIdle;
255
+ set idle(value: ParallaxIdle);
256
+ /** `depth` shows the estimated depth map instead of the photo (default `image`). */
257
+ get view(): ParallaxView;
258
+ set view(value: ParallaxView);
259
+ /**
260
+ * Depth of field (0–1, default 0 = off). Focus follows whatever is under the
261
+ * pointer (or the idle sway); at rest it falls back to `focus-depth`.
262
+ */
263
+ get defocus(): number;
264
+ set defocus(value: number);
265
+ /** Bring the whole image back into focus while the pointer is not over it (depth of field only while hovering). */
266
+ get refocus(): boolean;
267
+ set refocus(value: boolean);
268
+ /**
269
+ * `foreground` draws only what is nearer than `cutoff`; `flat` draws the whole photo
270
+ * moving in lockstep with it; `background` draws the rest, with the foreground filled
271
+ * in, moving less, so the subject separates from it. Stack `flat` or `background`, your
272
+ * content, then `foreground` to tuck content behind the subject, like a lock-screen depth effect.
273
+ */
274
+ get layer(): ParallaxLayer;
275
+ set layer(value: ParallaxLayer);
276
+ /** Nearness threshold between `layer="foreground"` and `layer="background"` (0–1, default 0.6). */
277
+ get cutoff(): number;
278
+ set cutoff(value: number);
279
+ /** Current lifecycle state; also reflected as the `data-state` attribute. */
280
+ get state(): ParallaxState;
281
+ /**
282
+ * Steer the effect yourself, with `x`/`y` in -1..1. Pass `null` to release
283
+ * control. Works with any trigger; use `trigger="manual"` to ignore others.
284
+ */
285
+ setPointer(x: number | null, y?: number): void;
286
+ /** Re-run depth estimation for the current image, bypassing the shared cache. */
287
+ refresh(): void;
288
+ connectedCallback(): void;
289
+ disconnectedCallback(): void;
290
+ attributeChangedCallback(name: string, old: string | null, value: string | null): void;
291
+ }
292
+ export { DepthModelOption as C, listModels as D, MODEL_PRESETS as E, registerModel as O, DEFAULT_MODEL as S, DepthModelSpec as T, GyroPermission as _, ParallaxIdle as a, prefersGyro as b, ParallaxState as c, ImgAutoParallaxConfig as d, ModelProgress as f, preload as g, isSupported as h, ParallaxErrorDetail as i, ParallaxTrigger as l, hasWebGpu as m, ImgAutoParallaxElement as n, ParallaxLayer as o, configure as p, ImgAutoParallaxEventMap as r, ParallaxOptions as s, DEFAULTS as t, ParallaxView as u, getGyroPermission as v, DepthModelPreset as w, requestGyroPermission as x, onGyroPermissionChange as y };
package/dist/index.d.mts CHANGED
@@ -1,214 +1,4 @@
1
- import "vgpu";
2
- /**
3
- * - `static` plain image: disabled, or not scrolled into view yet
4
- * - `loading` estimating depth for the current image (or loading the model)
5
- * - `ready` parallax active
6
- * - `unsupported` no WebGPU, or the model failed to load: plain image
7
- * - `error` this image could not be used (e.g. cross-origin without CORS): plain image
8
- */
9
- type ParallaxState = 'static' | 'loading' | 'ready' | 'unsupported' | 'error';
10
- /**
11
- * What drives the parallax:
12
- * - `self-hover`: the pointer over this image
13
- * - `page-hover`: the pointer anywhere on the page, relative to this image
14
- * - `gyro`: device tilt
15
- * - `both`: self-hover and gyro
16
- * - `manual`: only `setPointer()`
17
- * - `auto` (default): gyro on touch devices, self-hover elsewhere
18
- */
19
- type ParallaxTrigger = 'auto' | 'self-hover' | 'page-hover' | 'gyro' | 'both' | 'manual';
20
- /** Motion when nobody is interacting. */
21
- type ParallaxIdle = 'none' | 'sway';
22
- /** What the canvas shows: the photo, or the estimated depth (near = bright). */
23
- type ParallaxView = 'image' | 'depth';
24
- /**
25
- * What the element draws:
26
- * - `image` (default): the photo with depth parallax
27
- * - `foreground`: only what is nearer than `cutoff`, transparent elsewhere
28
- * - `flat`: the whole photo moving rigidly with a `foreground` copy, as the plate behind it
29
- */
30
- type ParallaxLayer = 'image' | 'foreground' | 'flat';
31
- export declare const DEFAULTS: {
32
- strength: number;
33
- focusDepth: number;
34
- smoothing: number;
35
- resolution: number;
36
- trigger: ParallaxTrigger;
37
- idle: ParallaxIdle;
38
- view: ParallaxView;
39
- defocus: number;
40
- layer: ParallaxLayer;
41
- cutoff: number;
42
- };
43
- /**
44
- * `<img-auto-parallax>`: an `<img>` that estimates depth on the GPU and
45
- * shifts its layers with the pointer or device tilt. Falls back to the plain
46
- * image when disabled, without WebGPU, or when the image cannot be read.
47
- */
48
- export declare class ImgAutoParallaxElement extends HTMLElement {
49
- #private;
50
- static readonly observedAttributes: string[];
51
- constructor();
52
- get src(): string;
53
- set src(value: string);
54
- get alt(): string;
55
- set alt(value: string);
56
- get currentSrc(): string;
57
- get complete(): boolean;
58
- get naturalWidth(): number;
59
- get naturalHeight(): number;
60
- decode(): Promise<void>;
61
- /** Plain image when true: no model, no GPU work. */
62
- get disabled(): boolean;
63
- set disabled(value: boolean);
64
- /** Model for this element: `auto`, a preset (`q4f16`, `q4`, `fp16`, `fp32`) or a registered name. Defaults to `configure({ model })`. */
65
- /**
66
- * A precompiled depth map (grayscale, near = bright, any size), e.g. from
67
- * `img-auto-parallax precompute`. When set, no model or ONNX Runtime is loaded.
68
- */
69
- get srcDepth(): string | null;
70
- set srcDepth(value: string | null | undefined);
71
- get model(): string | null;
72
- set model(value: string | null);
73
- /** Maximum displacement as a fraction of the image height (0–0.3, default 0.03). */
74
- get strength(): number;
75
- set strength(value: number);
76
- /** Nearness of the plane that stays still: 0 = background, 1 = foreground (default 0.5). */
77
- get focusDepth(): number;
78
- set focusDepth(value: number);
79
- /** Easing time constant in ms; 0 follows the input immediately (default 150). */
80
- get smoothing(): number;
81
- set smoothing(value: number);
82
- /** Long side of the depth model input in px; higher is sharper and slower (default 518). */
83
- get resolution(): number;
84
- set resolution(value: number);
85
- /** What drives the effect (default `auto`). */
86
- get trigger(): ParallaxTrigger;
87
- set trigger(value: ParallaxTrigger);
88
- /** Reverse the direction of motion. */
89
- get invert(): boolean;
90
- set invert(value: boolean);
91
- /** Motion while nobody interacts (default `none`). */
92
- get idle(): ParallaxIdle;
93
- set idle(value: ParallaxIdle);
94
- /** `depth` shows the estimated depth map instead of the photo (default `image`). */
95
- get view(): ParallaxView;
96
- set view(value: ParallaxView);
97
- /**
98
- * Depth of field (0–1, default 0 = off). Focus follows whatever is under the
99
- * pointer (or the idle sway); at rest it falls back to `focus-depth`.
100
- */
101
- get defocus(): number;
102
- set defocus(value: number);
103
- /** Bring the whole image back into focus while the pointer is not over it (depth of field only while hovering). */
104
- get refocus(): boolean;
105
- set refocus(value: boolean);
106
- /**
107
- * `foreground` draws only what is nearer than `cutoff`; `flat` draws the whole photo
108
- * moving in lockstep with it. Stack `flat`, your content, then `foreground` to tuck
109
- * content behind the subject, like a lock-screen depth effect.
110
- */
111
- get layer(): ParallaxLayer;
112
- set layer(value: ParallaxLayer);
113
- /** Nearness threshold for `layer="foreground"` (0–1, default 0.6). */
114
- get cutoff(): number;
115
- set cutoff(value: number);
116
- /** Current lifecycle state; also reflected as the `data-state` attribute. */
117
- get state(): ParallaxState;
118
- /**
119
- * Steer the effect yourself, with `x`/`y` in -1..1. Pass `null` to release
120
- * control. Works with any trigger; use `trigger="manual"` to ignore others.
121
- */
122
- setPointer(x: number | null, y?: number): void;
123
- /** Re-run depth estimation for the current image. */
124
- refresh(): void;
125
- connectedCallback(): void;
126
- disconnectedCallback(): void;
127
- attributeChangedCallback(name: string, old: string | null, value: string | null): void;
128
- }
129
- /** Describes an ONNX depth model the runtime can drive. */
130
- interface DepthModelSpec {
131
- /** Location of the `.onnx` file. */
132
- url: string | URL;
133
- /** Graph input name (NCHW float32). */
134
- inputName: string;
135
- /** Graph output name (float32, `[1, H, W]` or `[1, 1, H, W]`). */
136
- outputName: string;
137
- /**
138
- * Input sizes are rounded to a multiple of this (the ViT patch size for
139
- * Depth Anything). Ignored when `fixedSize` is set.
140
- */
141
- multiple?: number;
142
- /** Fixed `[width, height]` input. When omitted, the input follows each image's aspect ratio. */
143
- fixedSize?: readonly [number, number];
144
- /** How RGB is normalised before inference: ImageNet mean/std, or plain 0–1. */
145
- normalization: 'imagenet' | 'unit';
146
- /**
147
- * How to read the output. `relative`: larger values are nearer (inverse
148
- * depth, auto-ranged per image). `metric`: metres, mapped with a log scale.
149
- */
150
- output: {
151
- kind: 'relative';
152
- } | {
153
- kind: 'metric';
154
- near: number;
155
- far: number;
156
- };
157
- /** Needs the WebGPU `shader-f16` feature (fp16 weights). */
158
- requiresF16?: boolean;
159
- /** Used instead when `requiresF16` is set and the GPU lacks `shader-f16`. */
160
- fallback?: DepthModelSpec;
161
- /** Human-readable name, for pickers and logs. */
162
- label?: string;
163
- /** Download size in bytes, when known. */
164
- bytes?: number;
165
- }
166
- type DepthModelPreset = 'depth-anything' | 'midas' | 'fastdepth';
167
- /**
168
- * The built-in models. Bundlers that understand `new URL(…, import.meta.url)`
169
- * (Vite, webpack 5, Rollup) emit the bundled files as assets automatically.
170
- */
171
- export declare const MODEL_PRESETS: Record<DepthModelPreset, DepthModelSpec>;
172
- export declare const DEFAULT_MODEL: DepthModelPreset;
173
- /**
174
- * Register a custom model under a name, so elements can pick it with
175
- * `model="name"`. Re-registering a name replaces it for future estimates.
176
- */
177
- export declare function registerModel(name: string, spec: DepthModelSpec): void;
178
- /** Names usable in the `model` attribute: the presets and registered models. */
179
- export declare function listModels(): string[];
180
- /** A preset or registered name, or a full spec. */
181
- type DepthModelOption = DepthModelPreset | (string & {}) | DepthModelSpec;
182
- interface ModelProgress {
183
- /** URL of the model being downloaded. */
184
- url: string;
185
- /** Bytes downloaded so far. */
186
- loaded: number;
187
- /** Total bytes, when the server reports it. */
188
- total?: number;
189
- }
190
- interface ImgAutoParallaxConfig {
191
- /**
192
- * Default model for elements without a `model` attribute:
193
- * `'depth-anything'` (default), `'midas'`, `'fastdepth'`, a registered name, or a spec.
194
- */
195
- model: DepthModelOption;
196
- /** Override where ONNX Runtime loads its `.wasm` files from. Only read before the first load. */
197
- wasmPaths?: string;
198
- /** Model download progress. */
199
- onProgress?: (progress: ModelProgress) => void;
200
- }
201
- /**
202
- * Configure the shared runtime. Can be called at any time; changing `model`
203
- * re-estimates every element that has no `model` attribute of its own.
204
- */
205
- export declare function configure(options: Partial<ImgAutoParallaxConfig>): void;
206
- /** Synchronous check: does this browser expose WebGPU at all? */
207
- export declare function hasWebGpu(): boolean;
208
- /** Resolves `true` once the given (or default) model runs on this device, `false` if it cannot. */
209
- export declare function isSupported(model?: DepthModelOption): Promise<boolean>;
210
- /** Start downloading a model and creating its GPU session ahead of time. */
211
- export declare function preload(model?: DepthModelOption): Promise<void>;
1
+ import { C as DepthModelOption, D as listModels, E as MODEL_PRESETS, O as registerModel, S as DEFAULT_MODEL, T as DepthModelSpec, _ as GyroPermission, a as ParallaxIdle, b as prefersGyro, c as ParallaxState, d as ImgAutoParallaxConfig, f as ModelProgress, g as preload, h as isSupported, i as ParallaxErrorDetail, l as ParallaxTrigger, m as hasWebGpu, n as ImgAutoParallaxElement, o as ParallaxLayer, p as configure, r as ImgAutoParallaxEventMap, s as ParallaxOptions, t as DEFAULTS, u as ParallaxView, v as getGyroPermission, w as DepthModelPreset, x as requestGyroPermission, y as onGyroPermissionChange } from "./element-C2AqZorC.mjs";
212
2
  export declare const DEFAULT_TAG = "img-auto-parallax";
213
3
  /**
214
4
  * Register the custom element (idempotent). Pass a different tag to avoid
@@ -220,4 +10,4 @@ declare global {
220
10
  'img-auto-parallax': ImgAutoParallaxElement;
221
11
  }
222
12
  }
223
- export type { DepthModelOption, DepthModelPreset, DepthModelSpec, ImgAutoParallaxConfig, ModelProgress, ParallaxIdle, ParallaxLayer, ParallaxState, ParallaxTrigger, ParallaxView };
13
+ export { DEFAULTS, DEFAULT_MODEL, type DepthModelOption, type DepthModelPreset, type DepthModelSpec, type GyroPermission, type ImgAutoParallaxConfig, ImgAutoParallaxElement, type ImgAutoParallaxEventMap, MODEL_PRESETS, type ModelProgress, type ParallaxErrorDetail, type ParallaxIdle, type ParallaxLayer, type ParallaxOptions, type ParallaxState, type ParallaxTrigger, type ParallaxView, configure, getGyroPermission, hasWebGpu, isSupported, listModels, onGyroPermissionChange, prefersGyro, preload, registerModel, requestGyroPermission };
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- export { r as DEFAULTS, l as DEFAULT_MODEL, t as DEFAULT_TAG, i as ImgAutoParallaxElement, u as MODEL_PRESETS, a as configure, n as defineImgAutoParallax, o as hasWebGpu, s as isSupported, d as listModels, c as preload, f as registerModel } from "./src-Bd-5bi4S.mjs";
1
+ export { r as DEFAULTS, l as DEFAULT_MODEL, t as DEFAULT_TAG, i as ImgAutoParallaxElement, u as MODEL_PRESETS, a as configure, n as defineImgAutoParallax, p as getGyroPermission, o as hasWebGpu, s as isSupported, d as listModels, m as onGyroPermissionChange, h as prefersGyro, c as preload, f as registerModel, g as requestGyroPermission } from "./src-BiTfnOJ8.mjs";