img-auto-parallax 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/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-CLDV9piW.mjs";
2
2
  // src/define.ts
3
3
  defineImgAutoParallax();
package/dist/index.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import "vgpu";
2
2
  /**
3
- * - `static` plain image: disabled, or not scrolled into view yet
3
+ * - `static` plain image: disabled, or out of view (GPU resources released)
4
4
  * - `loading` estimating depth for the current image (or loading the model)
5
5
  * - `ready` parallax active
6
6
  * - `unsupported` no WebGPU, or the model failed to load: plain image
@@ -15,8 +15,10 @@ type ParallaxState = 'static' | 'loading' | 'ready' | 'unsupported' | 'error';
15
15
  * - `both`: self-hover and gyro
16
16
  * - `manual`: only `setPointer()`
17
17
  * - `auto` (default): gyro on touch devices, self-hover elsewhere
18
+ *
19
+ * Combine them with spaces, e.g. `trigger="page-hover gyro"`.
18
20
  */
19
- type ParallaxTrigger = 'auto' | 'self-hover' | 'page-hover' | 'gyro' | 'both' | 'manual';
21
+ type ParallaxTrigger = 'auto' | 'self-hover' | 'page-hover' | 'gyro' | 'both' | 'manual' | (string & {});
20
22
  /** Motion when nobody is interacting. */
21
23
  type ParallaxIdle = 'none' | 'sway';
22
24
  /** What the canvas shows: the photo, or the estimated depth (near = bright). */
@@ -26,8 +28,10 @@ type ParallaxView = 'image' | 'depth';
26
28
  * - `image` (default): the photo with depth parallax
27
29
  * - `foreground`: only what is nearer than `cutoff`, transparent elsewhere
28
30
  * - `flat`: the whole photo moving rigidly with a `foreground` copy, as the plate behind it
31
+ * - `background`: only what is farther than `cutoff`, the foreground filled in from
32
+ * around it, moving at its own depth so a `foreground` copy slides over it
29
33
  */
30
- type ParallaxLayer = 'image' | 'foreground' | 'flat';
34
+ type ParallaxLayer = 'image' | 'foreground' | 'flat' | 'background';
31
35
  export declare const DEFAULTS: {
32
36
  strength: number;
33
37
  focusDepth: number;
@@ -105,12 +109,13 @@ export declare class ImgAutoParallaxElement extends HTMLElement {
105
109
  set refocus(value: boolean);
106
110
  /**
107
111
  * `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.
112
+ * moving in lockstep with it; `background` draws the rest, with the foreground filled
113
+ * in, moving less, so the subject separates from it. Stack `flat` or `background`, your
114
+ * content, then `foreground` to tuck content behind the subject, like a lock-screen depth effect.
110
115
  */
111
116
  get layer(): ParallaxLayer;
112
117
  set layer(value: ParallaxLayer);
113
- /** Nearness threshold for `layer="foreground"` (0–1, default 0.6). */
118
+ /** Nearness threshold between `layer="foreground"` and `layer="background"` (0–1, default 0.6). */
114
119
  get cutoff(): number;
115
120
  set cutoff(value: number);
116
121
  /** Current lifecycle state; also reflected as the `data-state` attribute. */
@@ -126,6 +131,24 @@ export declare class ImgAutoParallaxElement extends HTMLElement {
126
131
  disconnectedCallback(): void;
127
132
  attributeChangedCallback(name: string, old: string | null, value: string | null): void;
128
133
  }
134
+ /** True on devices without a hovering pointer that expose orientation events. */
135
+ export declare function prefersGyro(): boolean;
136
+ /** Called when `getGyroPermission()` changes, e.g. after a grant or once readings arrive. */
137
+ export declare function onGyroPermissionChange(listener: (permission: GyroPermission) => void): () => void;
138
+ /**
139
+ * Gyro access on this device:
140
+ * - `unavailable`: no orientation events (most desktops)
141
+ * - `prompt`: the browser asks first (iOS Safari); call `requestGyroPermission()` from a tap
142
+ * - `granted` / `denied`: the answer, for this page load
143
+ * - `not-required`: events flow without asking (Android, most others)
144
+ */
145
+ type GyroPermission = 'unavailable' | 'prompt' | 'granted' | 'denied' | 'not-required';
146
+ export declare function getGyroPermission(): GyroPermission;
147
+ /**
148
+ * Ask for gyro access. Must run inside a user gesture (a tap or click handler),
149
+ * or iOS rejects it. Elements start following the tilt as soon as it is granted.
150
+ */
151
+ export declare function requestGyroPermission(): Promise<GyroPermission>;
129
152
  /** Describes an ONNX depth model the runtime can drive. */
130
153
  interface DepthModelSpec {
131
154
  /** Location of the `.onnx` file. */
@@ -220,4 +243,4 @@ declare global {
220
243
  'img-auto-parallax': ImgAutoParallaxElement;
221
244
  }
222
245
  }
223
- export type { DepthModelOption, DepthModelPreset, DepthModelSpec, ImgAutoParallaxConfig, ModelProgress, ParallaxIdle, ParallaxLayer, ParallaxState, ParallaxTrigger, ParallaxView };
246
+ export type { DepthModelOption, DepthModelPreset, DepthModelSpec, GyroPermission, ImgAutoParallaxConfig, ModelProgress, ParallaxIdle, ParallaxLayer, ParallaxState, ParallaxTrigger, ParallaxView };
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-CLDV9piW.mjs";