img-auto-parallax 0.0.0 → 0.0.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 +34 -4
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +57 -0
- package/dist/define.mjs +1 -1
- package/dist/index.d.mts +40 -4
- package/dist/index.mjs +1 -1
- package/dist/node-C8vJqZAm.d.mts +95 -0
- package/dist/node-Cgcvci4U.mjs +400 -0
- package/dist/node.d.mts +6 -0
- package/dist/node.mjs +1 -0
- package/dist/{src-DHNTwA54.mjs → src-Bd-5bi4S.mjs} +3060 -1087
- package/dist/vite.d.mts +11 -0
- package/dist/vite.mjs +29 -0
- package/package.json +23 -2
package/README.md
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
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.
|
|
4
4
|
|
|
5
|
-
- **Drop-in**: forwards every `<img>` attribute (`src`, `srcset`, `alt`, `loading`, `width`/`height`…)
|
|
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
6
|
- **Graceful**: plain image until depth is ready; stays a plain image without WebGPU or for non-CORS images.
|
|
7
7
|
- **Self-contained**: ships Depth Anything V2 small and FastDepth, so no third-party model host is needed by default.
|
|
8
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 (hover / gyro / manual), idle sway, invert, resolution, three built-in models or your own.
|
|
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.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -37,21 +37,51 @@ export default defineConfig({
|
|
|
37
37
|
| Attribute | Default | Description |
|
|
38
38
|
| --- | --- | --- |
|
|
39
39
|
| `disabled` | `false` | Behave exactly like `<img>`. |
|
|
40
|
-
| `strength` | `0.
|
|
40
|
+
| `strength` | `0.03` | Max displacement, fraction of image height (0–0.3). |
|
|
41
41
|
| `focus-depth` | `0.5` | Depth that stays still: 0 background … 1 foreground. |
|
|
42
42
|
| `smoothing` | `150` | Easing time constant in ms. |
|
|
43
|
-
| `trigger` | `auto` | `
|
|
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
44
|
| `idle` | `none` | `sway` drifts gently while idle. |
|
|
45
45
|
| `invert` | `false` | Reverse the motion. |
|
|
46
46
|
| `resolution` | `518` | Long side of the model input in px. |
|
|
47
|
+
| `src-depth` | — | Precompiled depth map; skips the model entirely. |
|
|
47
48
|
| `model` | `configure()` | `depth-anything` (bundled), `midas` (on demand), `fastdepth` (bundled), or a `registerModel()` name. |
|
|
48
49
|
| `view` | `image` | `depth` shows the depth map. |
|
|
49
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. |
|
|
50
52
|
|
|
51
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)`.
|
|
52
54
|
|
|
53
55
|
Methods: `setPointer(x, y)` / `setPointer(null)`, `refresh()`. State: `state` / `data-state` (`static` · `loading` · `ready` · `unsupported` · `error`). Events: `load`, `error`, `parallax-ready`, `parallax-error`.
|
|
54
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
|
+
|
|
55
85
|
## Configuration
|
|
56
86
|
|
|
57
87
|
```ts
|
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
import { parseArgs } from "node:util";
|
|
4
|
+
import { r as precompute } from "./node-Cgcvci4U.mjs";
|
|
5
|
+
// src/cli.ts
|
|
6
|
+
/**
|
|
7
|
+
* img-auto-parallax precompute <patterns...> [--model name] [--resolution n|full] [--suffix s] [--force]
|
|
8
|
+
*/
|
|
9
|
+
const HELP = `Usage: img-auto-parallax precompute <patterns...> [options]
|
|
10
|
+
|
|
11
|
+
Generate depth maps (photo.jpg -> photo.depth.png) for <img-auto-parallax src-depth>.
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--model <name> depth-anything (default) | midas | fastdepth
|
|
15
|
+
--resolution <px> model input long side (default 518), or "full" (slow; output is 1:1 regardless)
|
|
16
|
+
--suffix <suffix> output suffix (default .depth.png)
|
|
17
|
+
--force regenerate even when up to date
|
|
18
|
+
-h, --help show this help
|
|
19
|
+
`;
|
|
20
|
+
async function main() {
|
|
21
|
+
const { values, positionals } = parseArgs({
|
|
22
|
+
allowPositionals: true,
|
|
23
|
+
options: {
|
|
24
|
+
model: { type: "string" },
|
|
25
|
+
resolution: { type: "string" },
|
|
26
|
+
suffix: { type: "string" },
|
|
27
|
+
force: { type: "boolean" },
|
|
28
|
+
help: {
|
|
29
|
+
type: "boolean",
|
|
30
|
+
short: "h"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const [command, ...patterns] = positionals;
|
|
35
|
+
if (values.help || command !== "precompute" || patterns.length === 0) {
|
|
36
|
+
process.stdout.write(HELP);
|
|
37
|
+
process.exitCode = values.help ? 0 : 1;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const started = performance.now();
|
|
41
|
+
const result = await precompute(patterns, {
|
|
42
|
+
model: values.model,
|
|
43
|
+
resolution: values.resolution && values.resolution !== "full" ? Number(values.resolution) : void 0,
|
|
44
|
+
suffix: values.suffix,
|
|
45
|
+
force: values.force,
|
|
46
|
+
onProgress: ({ output, status, index, total }) => {
|
|
47
|
+
process.stdout.write(`[${index + 1}/${total}] ${status === "cached" ? "cached " : "generated"} ${output}\n`);
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
const seconds = ((performance.now() - started) / 1e3).toFixed(1);
|
|
51
|
+
process.stdout.write(`${result.generated.length} generated, ${result.cached.length} up to date, in ${seconds}s\n`);
|
|
52
|
+
}
|
|
53
|
+
main().catch((error) => {
|
|
54
|
+
console.error(error);
|
|
55
|
+
process.exitCode = 1;
|
|
56
|
+
});
|
|
57
|
+
export {};
|
package/dist/define.mjs
CHANGED
package/dist/index.d.mts
CHANGED
|
@@ -7,12 +7,27 @@ import "vgpu";
|
|
|
7
7
|
* - `error` this image could not be used (e.g. cross-origin without CORS): plain image
|
|
8
8
|
*/
|
|
9
9
|
type ParallaxState = 'static' | 'loading' | 'ready' | 'unsupported' | 'error';
|
|
10
|
-
/**
|
|
11
|
-
|
|
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';
|
|
12
20
|
/** Motion when nobody is interacting. */
|
|
13
21
|
type ParallaxIdle = 'none' | 'sway';
|
|
14
22
|
/** What the canvas shows: the photo, or the estimated depth (near = bright). */
|
|
15
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';
|
|
16
31
|
export declare const DEFAULTS: {
|
|
17
32
|
strength: number;
|
|
18
33
|
focusDepth: number;
|
|
@@ -22,6 +37,8 @@ export declare const DEFAULTS: {
|
|
|
22
37
|
idle: ParallaxIdle;
|
|
23
38
|
view: ParallaxView;
|
|
24
39
|
defocus: number;
|
|
40
|
+
layer: ParallaxLayer;
|
|
41
|
+
cutoff: number;
|
|
25
42
|
};
|
|
26
43
|
/**
|
|
27
44
|
* `<img-auto-parallax>`: an `<img>` that estimates depth on the GPU and
|
|
@@ -45,9 +62,15 @@ export declare class ImgAutoParallaxElement extends HTMLElement {
|
|
|
45
62
|
get disabled(): boolean;
|
|
46
63
|
set disabled(value: boolean);
|
|
47
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);
|
|
48
71
|
get model(): string | null;
|
|
49
72
|
set model(value: string | null);
|
|
50
|
-
/** Maximum displacement as a fraction of the image height (0–0.3, default 0.
|
|
73
|
+
/** Maximum displacement as a fraction of the image height (0–0.3, default 0.03). */
|
|
51
74
|
get strength(): number;
|
|
52
75
|
set strength(value: number);
|
|
53
76
|
/** Nearness of the plane that stays still: 0 = background, 1 = foreground (default 0.5). */
|
|
@@ -77,6 +100,19 @@ export declare class ImgAutoParallaxElement extends HTMLElement {
|
|
|
77
100
|
*/
|
|
78
101
|
get defocus(): number;
|
|
79
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);
|
|
80
116
|
/** Current lifecycle state; also reflected as the `data-state` attribute. */
|
|
81
117
|
get state(): ParallaxState;
|
|
82
118
|
/**
|
|
@@ -184,4 +220,4 @@ declare global {
|
|
|
184
220
|
'img-auto-parallax': ImgAutoParallaxElement;
|
|
185
221
|
}
|
|
186
222
|
}
|
|
187
|
-
export type { DepthModelOption, DepthModelPreset, DepthModelSpec, ImgAutoParallaxConfig, ModelProgress, ParallaxIdle, ParallaxState, ParallaxTrigger, ParallaxView };
|
|
223
|
+
export type { DepthModelOption, DepthModelPreset, DepthModelSpec, 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-
|
|
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";
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** Describes an ONNX depth model the runtime can drive. */
|
|
2
|
+
interface DepthModelSpec {
|
|
3
|
+
/** Location of the `.onnx` file. */
|
|
4
|
+
url: string | URL;
|
|
5
|
+
/** Graph input name (NCHW float32). */
|
|
6
|
+
inputName: string;
|
|
7
|
+
/** Graph output name (float32, `[1, H, W]` or `[1, 1, H, W]`). */
|
|
8
|
+
outputName: string;
|
|
9
|
+
/**
|
|
10
|
+
* Input sizes are rounded to a multiple of this (the ViT patch size for
|
|
11
|
+
* Depth Anything). Ignored when `fixedSize` is set.
|
|
12
|
+
*/
|
|
13
|
+
multiple?: number;
|
|
14
|
+
/** Fixed `[width, height]` input. When omitted, the input follows each image's aspect ratio. */
|
|
15
|
+
fixedSize?: readonly [number, number];
|
|
16
|
+
/** How RGB is normalised before inference: ImageNet mean/std, or plain 0–1. */
|
|
17
|
+
normalization: 'imagenet' | 'unit';
|
|
18
|
+
/**
|
|
19
|
+
* How to read the output. `relative`: larger values are nearer (inverse
|
|
20
|
+
* depth, auto-ranged per image). `metric`: metres, mapped with a log scale.
|
|
21
|
+
*/
|
|
22
|
+
output: {
|
|
23
|
+
kind: 'relative';
|
|
24
|
+
} | {
|
|
25
|
+
kind: 'metric';
|
|
26
|
+
near: number;
|
|
27
|
+
far: number;
|
|
28
|
+
};
|
|
29
|
+
/** Needs the WebGPU `shader-f16` feature (fp16 weights). */
|
|
30
|
+
requiresF16?: boolean;
|
|
31
|
+
/** Used instead when `requiresF16` is set and the GPU lacks `shader-f16`. */
|
|
32
|
+
fallback?: DepthModelSpec;
|
|
33
|
+
/** Human-readable name, for pickers and logs. */
|
|
34
|
+
label?: string;
|
|
35
|
+
/** Download size in bytes, when known. */
|
|
36
|
+
bytes?: number;
|
|
37
|
+
}
|
|
38
|
+
type DepthModelPreset = 'depth-anything' | 'midas' | 'fastdepth';
|
|
39
|
+
/** A preset or registered name, or a full spec. */
|
|
40
|
+
type DepthModelOption = DepthModelPreset | (string & {}) | DepthModelSpec;
|
|
41
|
+
/** Conventional depth map path for an image: `photo.jpg` → `photo.depth.png`. */
|
|
42
|
+
declare function depthMapPath(image: string, suffix?: string): string;
|
|
43
|
+
interface DepthMapOptions {
|
|
44
|
+
/** Model to use (default `depth-anything`). Must match what looks right for your photos. */
|
|
45
|
+
model?: DepthModelOption;
|
|
46
|
+
/**
|
|
47
|
+
* Long side of the model input in px (default 518, what Depth Anything is
|
|
48
|
+
* trained at), or `'full'` for the image's own size up to 2048 px. The output
|
|
49
|
+
* map is always 1:1 with the photo either way; `'full'` is ~60× slower, needs
|
|
50
|
+
* gigabytes of memory, and usually comes out softer. Ignored by fixed-size models.
|
|
51
|
+
*/
|
|
52
|
+
resolution?: number | 'full';
|
|
53
|
+
}
|
|
54
|
+
interface DepthMap {
|
|
55
|
+
width: number;
|
|
56
|
+
height: number;
|
|
57
|
+
/** Grayscale PNG, near = bright. */
|
|
58
|
+
png: Uint8Array;
|
|
59
|
+
}
|
|
60
|
+
interface DecodedImage {
|
|
61
|
+
width: number;
|
|
62
|
+
height: number;
|
|
63
|
+
data: Uint8Array;
|
|
64
|
+
}
|
|
65
|
+
/** Decode a JPEG or PNG to RGBA. */
|
|
66
|
+
declare function decodeImage(bytes: Uint8Array): DecodedImage;
|
|
67
|
+
/** Run the depth model on an image (JPEG/PNG bytes) on the CPU, and encode the depth map. */
|
|
68
|
+
declare function generateDepthMap(image: Uint8Array, options?: DepthMapOptions): Promise<DepthMap>;
|
|
69
|
+
interface PrecomputeOptions extends DepthMapOptions {
|
|
70
|
+
/** Base directory for relative patterns (default `process.cwd()`). */
|
|
71
|
+
cwd?: string;
|
|
72
|
+
/** Output suffix replacing the extension (default `.depth.png`). */
|
|
73
|
+
suffix?: string;
|
|
74
|
+
/** Regenerate even when the depth map is up to date. */
|
|
75
|
+
force?: boolean;
|
|
76
|
+
/** Where to keep the cache manifest (default `node_modules/.cache/img-auto-parallax.json` under `cwd`). */
|
|
77
|
+
cacheFile?: string;
|
|
78
|
+
onProgress?: (event: {
|
|
79
|
+
file: string;
|
|
80
|
+
output: string;
|
|
81
|
+
status: 'generated' | 'cached';
|
|
82
|
+
index: number;
|
|
83
|
+
total: number;
|
|
84
|
+
}) => void;
|
|
85
|
+
}
|
|
86
|
+
interface PrecomputeResult {
|
|
87
|
+
generated: string[];
|
|
88
|
+
cached: string[];
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Generate `photo.depth.png` next to every matching image. Unchanged images
|
|
92
|
+
* (same content, model and resolution) are skipped.
|
|
93
|
+
*/
|
|
94
|
+
declare function precompute(patterns: string | string[], options?: PrecomputeOptions): Promise<PrecomputeResult>;
|
|
95
|
+
export { decodeImage as a, depthMapPath as c, PrecomputeResult as i, DepthMapOptions as n, generateDepthMap as o, PrecomputeOptions as r, precompute as s, DepthMap as t };
|