@snowcone-app/loaders 0.0.0-stage → 0.3.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/CHANGELOG.md +21 -0
- package/README.md +67 -2
- package/dist/beam/index.cjs +438 -0
- package/dist/beam/index.cjs.map +1 -0
- package/dist/beam/index.d.cts +137 -0
- package/dist/beam/index.d.ts +137 -0
- package/dist/beam/index.js +426 -0
- package/dist/beam/index.js.map +1 -0
- package/dist/chunk-2TO7VCSK.js +238 -0
- package/dist/chunk-2TO7VCSK.js.map +1 -0
- package/dist/chunk-6IWUTOBS.js +16 -0
- package/dist/chunk-6IWUTOBS.js.map +1 -0
- package/dist/chunk-7N24M2SH.cjs +259 -0
- package/dist/chunk-7N24M2SH.cjs.map +1 -0
- package/dist/chunk-NCXRWPGN.cjs +19 -0
- package/dist/chunk-NCXRWPGN.cjs.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/mask.cjs +89 -0
- package/dist/mask.cjs.map +1 -0
- package/dist/mask.d.cts +90 -0
- package/dist/mask.d.ts +90 -0
- package/dist/mask.js +4 -0
- package/dist/mask.js.map +1 -0
- package/package.json +84 -3
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AA4DO,SAAS,QAAQ,QAAA,EAAkB,QAAA,EAAkB,IAAA,EAAc,IAAA,EAAc,MAAkB,OAAA,EAAe;AACvH,EAAA,IAAI,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,WAAW,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,IAAM,EAAE,OAAO,CAAA,CAAA,EAAI,OAAO,EAAE,CAAA,EAAG,CAAA,EAAG,GAAG,CAAA,EAAG,CAAA,EAAG,IAAA,EAAM,CAAA,EAAG,IAAA,EAAK;AAC5G,EAAA,MAAM,CAAA,GAAI,GAAA,KAAQ,OAAA,GAAU,IAAA,CAAK,IAAI,IAAA,GAAO,QAAA,EAAU,IAAA,GAAO,QAAQ,IAAI,IAAA,CAAK,GAAA,CAAI,IAAA,GAAO,QAAA,EAAU,OAAO,QAAQ,CAAA;AAClH,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,MAAM,IAAI,QAAA,GAAW,CAAA;AACrB,EAAA,OAAO,EAAE,CAAA,EAAA,CAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,IAAI,IAAA,GAAO,CAAA,IAAK,CAAA,EAAG,CAAA,EAAG,CAAA,EAAE;AACtD;AAGO,SAAS,YAAY,EAAA,EAA8C;AACxE,EAAA,IAAI,OAAO,gBAAA,KAAqB,WAAA,IAAe,EAAA,YAAc,gBAAA,EAAkB,OAAO,EAAE,CAAA,EAAG,EAAA,CAAG,YAAA,EAAc,CAAA,EAAG,EAAA,CAAG,aAAA,EAAc;AAChI,EAAA,OAAO,EAAE,CAAA,EAAI,EAAA,CAAuC,KAAA,EAAO,CAAA,EAAI,GAAuC,MAAA,EAAO;AAC/G","file":"chunk-NCXRWPGN.cjs","sourcesContent":["/**\n * @snowcone-app/loaders — the loader contract.\n *\n * A loader paints OVER a live product picture while its next render is on the way, and goes away once the picture\n * has caught up. This entry has no loader in it on purpose: each loader is its own import\n * (`@snowcone-app/loaders/beam`), so a site only downloads the loaders it uses. Write your own against\n * {@link LoaderProps} and any Snowcone surface can host it.\n */\n\n/** How the picture fills its frame: the `object-fit` of the `<img>` the loader paints over. */\nexport type PictureFit = \"cover\" | \"contain\";\n\n/** A picture element a loader can read: an `<img>`, a `<canvas>`, an `ImageBitmap`. */\nexport type PictureElement = HTMLImageElement | HTMLCanvasElement | ImageBitmap;\n\n/** What every Snowcone loader takes from its host. */\nexport interface LoaderProps {\n /**\n * `true` while the picture is behind (a render is in flight). Going `false` means caught up: the loader plays its\n * exit, then calls {@link LoaderProps.onExited}. Going `true` again before that turns it back, never a fresh start.\n */\n visible: boolean;\n /** Called once the exit has finished: unmount the loader then. */\n onExited?: () => void;\n /**\n * The picture on screen now (the topmost painted `<img>`). Called as often as the loader likes, so return the\n * current element, not a cached one. Lets a loader see what changes; without it the loader covers the whole frame.\n */\n picture?: () => PictureElement | null;\n /** A stable id of the scene (a mockup id). Whatever a loader learns about the picture is kept per key. */\n maskKey?: string;\n /** The `object-fit` of the picture's `<img>`. Defaults to `\"cover\"`. */\n fit?: PictureFit;\n /**\n * A known mask of the part that changes (alpha = changes), in the picture's own pixel space. Wins over anything\n * learned. Leave it out and loaders that need one learn it from the frames.\n */\n mask?: PictureElement | null;\n className?: string;\n style?: import(\"react\").CSSProperties;\n}\n\n/** Where a loader is in its life, stamped as `data-sc-loader-phase` (agents assert on it, never on pixels). */\nexport type LoaderPhase = \"loading\" | \"finishing\";\n\n/** Where the loader's area came from, stamped as `data-sc-loader-mask`. */\nexport type LoaderMaskSource = \"whole\" | \"learned\" | \"given\";\n\n/** A rectangle in frame pixels. */\nexport interface Rect {\n x: number;\n y: number;\n w: number;\n h: number;\n}\n\n/**\n * Where a picture of `naturalW × naturalH` lands in a `boxW × boxH` frame under `object-fit` (centred, like the\n * browser's default `object-position`). Loaders use it to lay anything in picture space (a mask) over the frame.\n */\nexport function fitRect(naturalW: number, naturalH: number, boxW: number, boxH: number, fit: PictureFit = \"cover\"): Rect {\n if (!(naturalW > 0) || !(naturalH > 0) || !(boxW > 0) || !(boxH > 0)) return { x: 0, y: 0, w: boxW, h: boxH };\n const s = fit === \"cover\" ? Math.max(boxW / naturalW, boxH / naturalH) : Math.min(boxW / naturalW, boxH / naturalH);\n const w = naturalW * s;\n const h = naturalH * s;\n return { x: (boxW - w) / 2, y: (boxH - h) / 2, w, h };\n}\n\n/** The natural size of a picture element (0 × 0 while an `<img>` has not loaded). */\nexport function naturalSize(el: PictureElement): { w: number; h: number } {\n if (typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement) return { w: el.naturalWidth, h: el.naturalHeight };\n return { w: (el as HTMLCanvasElement | ImageBitmap).width, h: (el as HTMLCanvasElement | ImageBitmap).height };\n}\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkNCXRWPGN_cjs = require('./chunk-NCXRWPGN.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "fitRect", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () { return chunkNCXRWPGN_cjs.fitRect; }
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(exports, "naturalSize", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function () { return chunkNCXRWPGN_cjs.naturalSize; }
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=index.cjs.map
|
|
16
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.cjs"}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @snowcone-app/loaders — the loader contract.
|
|
5
|
+
*
|
|
6
|
+
* A loader paints OVER a live product picture while its next render is on the way, and goes away once the picture
|
|
7
|
+
* has caught up. This entry has no loader in it on purpose: each loader is its own import
|
|
8
|
+
* (`@snowcone-app/loaders/beam`), so a site only downloads the loaders it uses. Write your own against
|
|
9
|
+
* {@link LoaderProps} and any Snowcone surface can host it.
|
|
10
|
+
*/
|
|
11
|
+
/** How the picture fills its frame: the `object-fit` of the `<img>` the loader paints over. */
|
|
12
|
+
type PictureFit = "cover" | "contain";
|
|
13
|
+
/** A picture element a loader can read: an `<img>`, a `<canvas>`, an `ImageBitmap`. */
|
|
14
|
+
type PictureElement = HTMLImageElement | HTMLCanvasElement | ImageBitmap;
|
|
15
|
+
/** What every Snowcone loader takes from its host. */
|
|
16
|
+
interface LoaderProps {
|
|
17
|
+
/**
|
|
18
|
+
* `true` while the picture is behind (a render is in flight). Going `false` means caught up: the loader plays its
|
|
19
|
+
* exit, then calls {@link LoaderProps.onExited}. Going `true` again before that turns it back, never a fresh start.
|
|
20
|
+
*/
|
|
21
|
+
visible: boolean;
|
|
22
|
+
/** Called once the exit has finished: unmount the loader then. */
|
|
23
|
+
onExited?: () => void;
|
|
24
|
+
/**
|
|
25
|
+
* The picture on screen now (the topmost painted `<img>`). Called as often as the loader likes, so return the
|
|
26
|
+
* current element, not a cached one. Lets a loader see what changes; without it the loader covers the whole frame.
|
|
27
|
+
*/
|
|
28
|
+
picture?: () => PictureElement | null;
|
|
29
|
+
/** A stable id of the scene (a mockup id). Whatever a loader learns about the picture is kept per key. */
|
|
30
|
+
maskKey?: string;
|
|
31
|
+
/** The `object-fit` of the picture's `<img>`. Defaults to `"cover"`. */
|
|
32
|
+
fit?: PictureFit;
|
|
33
|
+
/**
|
|
34
|
+
* A known mask of the part that changes (alpha = changes), in the picture's own pixel space. Wins over anything
|
|
35
|
+
* learned. Leave it out and loaders that need one learn it from the frames.
|
|
36
|
+
*/
|
|
37
|
+
mask?: PictureElement | null;
|
|
38
|
+
className?: string;
|
|
39
|
+
style?: react.CSSProperties;
|
|
40
|
+
}
|
|
41
|
+
/** Where a loader is in its life, stamped as `data-sc-loader-phase` (agents assert on it, never on pixels). */
|
|
42
|
+
type LoaderPhase = "loading" | "finishing";
|
|
43
|
+
/** Where the loader's area came from, stamped as `data-sc-loader-mask`. */
|
|
44
|
+
type LoaderMaskSource = "whole" | "learned" | "given";
|
|
45
|
+
/** A rectangle in frame pixels. */
|
|
46
|
+
interface Rect {
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
w: number;
|
|
50
|
+
h: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Where a picture of `naturalW × naturalH` lands in a `boxW × boxH` frame under `object-fit` (centred, like the
|
|
54
|
+
* browser's default `object-position`). Loaders use it to lay anything in picture space (a mask) over the frame.
|
|
55
|
+
*/
|
|
56
|
+
declare function fitRect(naturalW: number, naturalH: number, boxW: number, boxH: number, fit?: PictureFit): Rect;
|
|
57
|
+
/** The natural size of a picture element (0 × 0 while an `<img>` has not loaded). */
|
|
58
|
+
declare function naturalSize(el: PictureElement): {
|
|
59
|
+
w: number;
|
|
60
|
+
h: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { type LoaderMaskSource, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @snowcone-app/loaders — the loader contract.
|
|
5
|
+
*
|
|
6
|
+
* A loader paints OVER a live product picture while its next render is on the way, and goes away once the picture
|
|
7
|
+
* has caught up. This entry has no loader in it on purpose: each loader is its own import
|
|
8
|
+
* (`@snowcone-app/loaders/beam`), so a site only downloads the loaders it uses. Write your own against
|
|
9
|
+
* {@link LoaderProps} and any Snowcone surface can host it.
|
|
10
|
+
*/
|
|
11
|
+
/** How the picture fills its frame: the `object-fit` of the `<img>` the loader paints over. */
|
|
12
|
+
type PictureFit = "cover" | "contain";
|
|
13
|
+
/** A picture element a loader can read: an `<img>`, a `<canvas>`, an `ImageBitmap`. */
|
|
14
|
+
type PictureElement = HTMLImageElement | HTMLCanvasElement | ImageBitmap;
|
|
15
|
+
/** What every Snowcone loader takes from its host. */
|
|
16
|
+
interface LoaderProps {
|
|
17
|
+
/**
|
|
18
|
+
* `true` while the picture is behind (a render is in flight). Going `false` means caught up: the loader plays its
|
|
19
|
+
* exit, then calls {@link LoaderProps.onExited}. Going `true` again before that turns it back, never a fresh start.
|
|
20
|
+
*/
|
|
21
|
+
visible: boolean;
|
|
22
|
+
/** Called once the exit has finished: unmount the loader then. */
|
|
23
|
+
onExited?: () => void;
|
|
24
|
+
/**
|
|
25
|
+
* The picture on screen now (the topmost painted `<img>`). Called as often as the loader likes, so return the
|
|
26
|
+
* current element, not a cached one. Lets a loader see what changes; without it the loader covers the whole frame.
|
|
27
|
+
*/
|
|
28
|
+
picture?: () => PictureElement | null;
|
|
29
|
+
/** A stable id of the scene (a mockup id). Whatever a loader learns about the picture is kept per key. */
|
|
30
|
+
maskKey?: string;
|
|
31
|
+
/** The `object-fit` of the picture's `<img>`. Defaults to `"cover"`. */
|
|
32
|
+
fit?: PictureFit;
|
|
33
|
+
/**
|
|
34
|
+
* A known mask of the part that changes (alpha = changes), in the picture's own pixel space. Wins over anything
|
|
35
|
+
* learned. Leave it out and loaders that need one learn it from the frames.
|
|
36
|
+
*/
|
|
37
|
+
mask?: PictureElement | null;
|
|
38
|
+
className?: string;
|
|
39
|
+
style?: react.CSSProperties;
|
|
40
|
+
}
|
|
41
|
+
/** Where a loader is in its life, stamped as `data-sc-loader-phase` (agents assert on it, never on pixels). */
|
|
42
|
+
type LoaderPhase = "loading" | "finishing";
|
|
43
|
+
/** Where the loader's area came from, stamped as `data-sc-loader-mask`. */
|
|
44
|
+
type LoaderMaskSource = "whole" | "learned" | "given";
|
|
45
|
+
/** A rectangle in frame pixels. */
|
|
46
|
+
interface Rect {
|
|
47
|
+
x: number;
|
|
48
|
+
y: number;
|
|
49
|
+
w: number;
|
|
50
|
+
h: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Where a picture of `naturalW × naturalH` lands in a `boxW × boxH` frame under `object-fit` (centred, like the
|
|
54
|
+
* browser's default `object-position`). Loaders use it to lay anything in picture space (a mask) over the frame.
|
|
55
|
+
*/
|
|
56
|
+
declare function fitRect(naturalW: number, naturalH: number, boxW: number, boxH: number, fit?: PictureFit): Rect;
|
|
57
|
+
/** The natural size of a picture element (0 × 0 while an `<img>` has not loaded). */
|
|
58
|
+
declare function naturalSize(el: PictureElement): {
|
|
59
|
+
w: number;
|
|
60
|
+
h: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export { type LoaderMaskSource, type LoaderPhase, type LoaderProps, type PictureElement, type PictureFit, type Rect, fitRect, naturalSize };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
|
package/dist/mask.cjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk7N24M2SH_cjs = require('./chunk-7N24M2SH.cjs');
|
|
4
|
+
require('./chunk-NCXRWPGN.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, "CHANGE_THRESHOLD", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
get: function () { return chunk7N24M2SH_cjs.CHANGE_THRESHOLD; }
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "LEARN_SIDE", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () { return chunk7N24M2SH_cjs.LEARN_SIDE; }
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, "MAX_STORED_MASKS", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () { return chunk7N24M2SH_cjs.MAX_STORED_MASKS; }
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(exports, "UNRELIABLE_COVERAGE", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
get: function () { return chunk7N24M2SH_cjs.UNRELIABLE_COVERAGE; }
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "alphaBox", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () { return chunk7N24M2SH_cjs.alphaBox; }
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(exports, "alphaCanvas", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () { return chunk7N24M2SH_cjs.alphaCanvas; }
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(exports, "changedBits", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
get: function () { return chunk7N24M2SH_cjs.changedBits; }
|
|
35
|
+
});
|
|
36
|
+
Object.defineProperty(exports, "coverageOf", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
get: function () { return chunk7N24M2SH_cjs.coverageOf; }
|
|
39
|
+
});
|
|
40
|
+
Object.defineProperty(exports, "forgetMasks", {
|
|
41
|
+
enumerable: true,
|
|
42
|
+
get: function () { return chunk7N24M2SH_cjs.forgetMasks; }
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(exports, "gridSize", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
get: function () { return chunk7N24M2SH_cjs.gridSize; }
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(exports, "isLearnable", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
get: function () { return chunk7N24M2SH_cjs.isLearnable; }
|
|
51
|
+
});
|
|
52
|
+
Object.defineProperty(exports, "meanColour", {
|
|
53
|
+
enumerable: true,
|
|
54
|
+
get: function () { return chunk7N24M2SH_cjs.meanColour; }
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(exports, "meanLuma", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
get: function () { return chunk7N24M2SH_cjs.meanLuma; }
|
|
59
|
+
});
|
|
60
|
+
Object.defineProperty(exports, "orBits", {
|
|
61
|
+
enumerable: true,
|
|
62
|
+
get: function () { return chunk7N24M2SH_cjs.orBits; }
|
|
63
|
+
});
|
|
64
|
+
Object.defineProperty(exports, "packBits", {
|
|
65
|
+
enumerable: true,
|
|
66
|
+
get: function () { return chunk7N24M2SH_cjs.packBits; }
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(exports, "recallMask", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
get: function () { return chunk7N24M2SH_cjs.recallMask; }
|
|
71
|
+
});
|
|
72
|
+
Object.defineProperty(exports, "rememberMask", {
|
|
73
|
+
enumerable: true,
|
|
74
|
+
get: function () { return chunk7N24M2SH_cjs.rememberMask; }
|
|
75
|
+
});
|
|
76
|
+
Object.defineProperty(exports, "snapshotPicture", {
|
|
77
|
+
enumerable: true,
|
|
78
|
+
get: function () { return chunk7N24M2SH_cjs.snapshotPicture; }
|
|
79
|
+
});
|
|
80
|
+
Object.defineProperty(exports, "softAlpha", {
|
|
81
|
+
enumerable: true,
|
|
82
|
+
get: function () { return chunk7N24M2SH_cjs.softAlpha; }
|
|
83
|
+
});
|
|
84
|
+
Object.defineProperty(exports, "unpackBits", {
|
|
85
|
+
enumerable: true,
|
|
86
|
+
get: function () { return chunk7N24M2SH_cjs.unpackBits; }
|
|
87
|
+
});
|
|
88
|
+
//# sourceMappingURL=mask.cjs.map
|
|
89
|
+
//# sourceMappingURL=mask.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"mask.cjs"}
|
package/dist/mask.d.cts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PictureElement } from './index.cjs';
|
|
2
|
+
import 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Learned masks: where a product picture changes, measured from the picture itself.
|
|
6
|
+
*
|
|
7
|
+
* A loader snapshots the picture when a render starts, then compares each new frame with it. The pixels that
|
|
8
|
+
* differ are "what changed" (the print, the coloured body), so the loader can stay on that part instead of the
|
|
9
|
+
* whole frame. What it learns is kept per scene (a mockup id) in memory and, packed small, in localStorage, so
|
|
10
|
+
* the next edit of the same picture knows the area at once.
|
|
11
|
+
*
|
|
12
|
+
* Everything here works on small grids (at most {@link LEARN_SIDE} px on a side): a comparison is a few
|
|
13
|
+
* thousand pixel reads, cheap enough to run several times a second while a render is on the way.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The long side of the grid a picture is compared on. */
|
|
17
|
+
declare const LEARN_SIDE = 192;
|
|
18
|
+
/** A channel difference (0–255, after a light blur) above this is a change, not compression noise. */
|
|
19
|
+
declare const CHANGE_THRESHOLD = 30;
|
|
20
|
+
/**
|
|
21
|
+
* A comparison that finds more than this share of the picture changed is not trusted: it is another picture (a
|
|
22
|
+
* different crop, a first live frame against a differently encoded store picture), not an edit of this one.
|
|
23
|
+
*/
|
|
24
|
+
declare const UNRELIABLE_COVERAGE = 0.6;
|
|
25
|
+
/** A picture read down to a small grid. */
|
|
26
|
+
interface Snapshot {
|
|
27
|
+
w: number;
|
|
28
|
+
h: number;
|
|
29
|
+
rgba: Uint8ClampedArray;
|
|
30
|
+
}
|
|
31
|
+
/** The grid size for a picture of `srcW × srcH`: the long side is `side`, the aspect kept. */
|
|
32
|
+
declare function gridSize(srcW: number, srcH: number, side?: number): {
|
|
33
|
+
w: number;
|
|
34
|
+
h: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Reads a picture down to its comparison grid. `null` when it cannot be read: not loaded yet, no 2D canvas, or a
|
|
38
|
+
* cross-origin picture served without CORS (the canvas is tainted). A loader then just covers the whole frame.
|
|
39
|
+
*/
|
|
40
|
+
declare function snapshotPicture(el: PictureElement | null | undefined, side?: number): Snapshot | null;
|
|
41
|
+
/**
|
|
42
|
+
* Which grid cells changed between two snapshots of the same picture (1 = changed). `null` when they are not the
|
|
43
|
+
* same grid (another picture, another aspect): there is nothing to compare.
|
|
44
|
+
*/
|
|
45
|
+
declare function changedBits(a: Snapshot, b: Snapshot, threshold?: number): Uint8Array | null;
|
|
46
|
+
/** The share of cells set. */
|
|
47
|
+
declare function coverageOf(bits: Uint8Array): number;
|
|
48
|
+
/** Whether a comparison is worth learning from: something changed, and not so much that it is another picture. */
|
|
49
|
+
declare function isLearnable(bits: Uint8Array | null): bits is Uint8Array;
|
|
50
|
+
/** Cells set in either. */
|
|
51
|
+
declare function orBits(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
52
|
+
/**
|
|
53
|
+
* A soft alpha (0–1) from changed cells: grown by `grow` cells (a stroke of a letter becomes the letter's area),
|
|
54
|
+
* then blurred by `blur` cells, so the loader's edge fades out instead of stopping at a hard line. The inside stays
|
|
55
|
+
* fully opaque.
|
|
56
|
+
*/
|
|
57
|
+
declare function softAlpha(bits: Uint8Array, w: number, h: number, opts?: {
|
|
58
|
+
grow?: number;
|
|
59
|
+
blur?: number;
|
|
60
|
+
}): Float32Array;
|
|
61
|
+
/** The box (0–1, picture space) holding every cell above `threshold`, or `null` when there is none. */
|
|
62
|
+
declare function alphaBox(alpha: Float32Array, w: number, h: number, threshold?: number): {
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
w: number;
|
|
66
|
+
h: number;
|
|
67
|
+
} | null;
|
|
68
|
+
/** A soft alpha as a canvas (white, alpha = mask), ready to `drawImage` over a frame. `null` without a 2D canvas. */
|
|
69
|
+
declare function alphaCanvas(alpha: Float32Array, w: number, h: number): HTMLCanvasElement | null;
|
|
70
|
+
/** The mean colour of the cells set (from a snapshot), or `null` when none is. A loader tints its light with it. */
|
|
71
|
+
declare function meanColour(s: Snapshot, bits: Uint8Array): [number, number, number] | null;
|
|
72
|
+
/**
|
|
73
|
+
* The mean brightness (0–1) of a snapshot, only where `alpha` is above `threshold` when given. A loader uses it to
|
|
74
|
+
* know whether light can show on the surface (on white it can't).
|
|
75
|
+
*/
|
|
76
|
+
declare function meanLuma(s: Snapshot, alpha?: Float32Array | null, threshold?: number): number;
|
|
77
|
+
/** At most this many scenes are kept in localStorage (oldest out first); a scene is ~1 KB. */
|
|
78
|
+
declare const MAX_STORED_MASKS = 40;
|
|
79
|
+
/** Bits to base64, 8 cells a byte. */
|
|
80
|
+
declare function packBits(bits: Uint8Array): string;
|
|
81
|
+
/** base64 back to `n` bits; `null` if it does not hold `n`. */
|
|
82
|
+
declare function unpackBits(packed: string, n: number): Uint8Array | null;
|
|
83
|
+
/** What has been learned about a scene's picture on this grid, or `null`. */
|
|
84
|
+
declare function recallMask(key: string, w: number, h: number): Uint8Array | null;
|
|
85
|
+
/** Adds what changed this time to what a scene already knows; returns the union. */
|
|
86
|
+
declare function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array;
|
|
87
|
+
/** Forgets everything learned (tests; a "reset" in a demo). */
|
|
88
|
+
declare function forgetMasks(): void;
|
|
89
|
+
|
|
90
|
+
export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits };
|
package/dist/mask.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { PictureElement } from './index.js';
|
|
2
|
+
import 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Learned masks: where a product picture changes, measured from the picture itself.
|
|
6
|
+
*
|
|
7
|
+
* A loader snapshots the picture when a render starts, then compares each new frame with it. The pixels that
|
|
8
|
+
* differ are "what changed" (the print, the coloured body), so the loader can stay on that part instead of the
|
|
9
|
+
* whole frame. What it learns is kept per scene (a mockup id) in memory and, packed small, in localStorage, so
|
|
10
|
+
* the next edit of the same picture knows the area at once.
|
|
11
|
+
*
|
|
12
|
+
* Everything here works on small grids (at most {@link LEARN_SIDE} px on a side): a comparison is a few
|
|
13
|
+
* thousand pixel reads, cheap enough to run several times a second while a render is on the way.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/** The long side of the grid a picture is compared on. */
|
|
17
|
+
declare const LEARN_SIDE = 192;
|
|
18
|
+
/** A channel difference (0–255, after a light blur) above this is a change, not compression noise. */
|
|
19
|
+
declare const CHANGE_THRESHOLD = 30;
|
|
20
|
+
/**
|
|
21
|
+
* A comparison that finds more than this share of the picture changed is not trusted: it is another picture (a
|
|
22
|
+
* different crop, a first live frame against a differently encoded store picture), not an edit of this one.
|
|
23
|
+
*/
|
|
24
|
+
declare const UNRELIABLE_COVERAGE = 0.6;
|
|
25
|
+
/** A picture read down to a small grid. */
|
|
26
|
+
interface Snapshot {
|
|
27
|
+
w: number;
|
|
28
|
+
h: number;
|
|
29
|
+
rgba: Uint8ClampedArray;
|
|
30
|
+
}
|
|
31
|
+
/** The grid size for a picture of `srcW × srcH`: the long side is `side`, the aspect kept. */
|
|
32
|
+
declare function gridSize(srcW: number, srcH: number, side?: number): {
|
|
33
|
+
w: number;
|
|
34
|
+
h: number;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Reads a picture down to its comparison grid. `null` when it cannot be read: not loaded yet, no 2D canvas, or a
|
|
38
|
+
* cross-origin picture served without CORS (the canvas is tainted). A loader then just covers the whole frame.
|
|
39
|
+
*/
|
|
40
|
+
declare function snapshotPicture(el: PictureElement | null | undefined, side?: number): Snapshot | null;
|
|
41
|
+
/**
|
|
42
|
+
* Which grid cells changed between two snapshots of the same picture (1 = changed). `null` when they are not the
|
|
43
|
+
* same grid (another picture, another aspect): there is nothing to compare.
|
|
44
|
+
*/
|
|
45
|
+
declare function changedBits(a: Snapshot, b: Snapshot, threshold?: number): Uint8Array | null;
|
|
46
|
+
/** The share of cells set. */
|
|
47
|
+
declare function coverageOf(bits: Uint8Array): number;
|
|
48
|
+
/** Whether a comparison is worth learning from: something changed, and not so much that it is another picture. */
|
|
49
|
+
declare function isLearnable(bits: Uint8Array | null): bits is Uint8Array;
|
|
50
|
+
/** Cells set in either. */
|
|
51
|
+
declare function orBits(a: Uint8Array, b: Uint8Array): Uint8Array;
|
|
52
|
+
/**
|
|
53
|
+
* A soft alpha (0–1) from changed cells: grown by `grow` cells (a stroke of a letter becomes the letter's area),
|
|
54
|
+
* then blurred by `blur` cells, so the loader's edge fades out instead of stopping at a hard line. The inside stays
|
|
55
|
+
* fully opaque.
|
|
56
|
+
*/
|
|
57
|
+
declare function softAlpha(bits: Uint8Array, w: number, h: number, opts?: {
|
|
58
|
+
grow?: number;
|
|
59
|
+
blur?: number;
|
|
60
|
+
}): Float32Array;
|
|
61
|
+
/** The box (0–1, picture space) holding every cell above `threshold`, or `null` when there is none. */
|
|
62
|
+
declare function alphaBox(alpha: Float32Array, w: number, h: number, threshold?: number): {
|
|
63
|
+
x: number;
|
|
64
|
+
y: number;
|
|
65
|
+
w: number;
|
|
66
|
+
h: number;
|
|
67
|
+
} | null;
|
|
68
|
+
/** A soft alpha as a canvas (white, alpha = mask), ready to `drawImage` over a frame. `null` without a 2D canvas. */
|
|
69
|
+
declare function alphaCanvas(alpha: Float32Array, w: number, h: number): HTMLCanvasElement | null;
|
|
70
|
+
/** The mean colour of the cells set (from a snapshot), or `null` when none is. A loader tints its light with it. */
|
|
71
|
+
declare function meanColour(s: Snapshot, bits: Uint8Array): [number, number, number] | null;
|
|
72
|
+
/**
|
|
73
|
+
* The mean brightness (0–1) of a snapshot, only where `alpha` is above `threshold` when given. A loader uses it to
|
|
74
|
+
* know whether light can show on the surface (on white it can't).
|
|
75
|
+
*/
|
|
76
|
+
declare function meanLuma(s: Snapshot, alpha?: Float32Array | null, threshold?: number): number;
|
|
77
|
+
/** At most this many scenes are kept in localStorage (oldest out first); a scene is ~1 KB. */
|
|
78
|
+
declare const MAX_STORED_MASKS = 40;
|
|
79
|
+
/** Bits to base64, 8 cells a byte. */
|
|
80
|
+
declare function packBits(bits: Uint8Array): string;
|
|
81
|
+
/** base64 back to `n` bits; `null` if it does not hold `n`. */
|
|
82
|
+
declare function unpackBits(packed: string, n: number): Uint8Array | null;
|
|
83
|
+
/** What has been learned about a scene's picture on this grid, or `null`. */
|
|
84
|
+
declare function recallMask(key: string, w: number, h: number): Uint8Array | null;
|
|
85
|
+
/** Adds what changed this time to what a scene already knows; returns the union. */
|
|
86
|
+
declare function rememberMask(key: string, w: number, h: number, bits: Uint8Array): Uint8Array;
|
|
87
|
+
/** Forgets everything learned (tests; a "reset" in a demo). */
|
|
88
|
+
declare function forgetMasks(): void;
|
|
89
|
+
|
|
90
|
+
export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, type Snapshot, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits };
|
package/dist/mask.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CHANGE_THRESHOLD, LEARN_SIDE, MAX_STORED_MASKS, UNRELIABLE_COVERAGE, alphaBox, alphaCanvas, changedBits, coverageOf, forgetMasks, gridSize, isLearnable, meanColour, meanLuma, orBits, packBits, recallMask, rememberMask, snapshotPicture, softAlpha, unpackBits } from './chunk-2TO7VCSK.js';
|
|
2
|
+
import './chunk-6IWUTOBS.js';
|
|
3
|
+
//# sourceMappingURL=mask.js.map
|
|
4
|
+
//# sourceMappingURL=mask.js.map
|
package/dist/mask.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"mask.js"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,87 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@snowcone-app/loaders",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Render-in-flight loaders for live product mockups. Each loader is its own import (`@snowcone-app/loaders/beam`): a site downloads only the loaders it uses.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"snowcone",
|
|
7
|
+
"loader",
|
|
8
|
+
"mockup",
|
|
9
|
+
"react",
|
|
10
|
+
"print-on-demand"
|
|
11
|
+
],
|
|
12
|
+
"author": "Snowcone <dev@snowcone.app>",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "https://github.com/snowcone-app/snowcone-monorepo.git",
|
|
17
|
+
"directory": "packages/loaders"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/snowcone-app/snowcone-monorepo/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://developers.snowcone.app",
|
|
23
|
+
"private": false,
|
|
24
|
+
"type": "module",
|
|
25
|
+
"main": "./dist/index.cjs",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"import": "./dist/index.js",
|
|
31
|
+
"require": "./dist/index.cjs",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./mask": {
|
|
35
|
+
"types": "./dist/mask.d.ts",
|
|
36
|
+
"import": "./dist/mask.js",
|
|
37
|
+
"require": "./dist/mask.cjs",
|
|
38
|
+
"default": "./dist/mask.js"
|
|
39
|
+
},
|
|
40
|
+
"./beam": {
|
|
41
|
+
"types": "./dist/beam/index.d.ts",
|
|
42
|
+
"import": "./dist/beam/index.js",
|
|
43
|
+
"require": "./dist/beam/index.cjs",
|
|
44
|
+
"default": "./dist/beam/index.js"
|
|
45
|
+
},
|
|
46
|
+
"./package.json": "./package.json"
|
|
47
|
+
},
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
"mask": [
|
|
51
|
+
"./dist/mask.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"beam": [
|
|
54
|
+
"./dist/beam/index.d.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"dist",
|
|
60
|
+
"README.md",
|
|
61
|
+
"CHANGELOG.md"
|
|
62
|
+
],
|
|
63
|
+
"publishConfig": {
|
|
64
|
+
"access": "public"
|
|
65
|
+
},
|
|
66
|
+
"sideEffects": false,
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"react": "^18 || ^19"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@testing-library/react": "^16.0.0",
|
|
72
|
+
"@types/react": "^19.2.2",
|
|
73
|
+
"jsdom": "^24.1.0",
|
|
74
|
+
"react": "^18.3.1",
|
|
75
|
+
"react-dom": "^18.3.1",
|
|
76
|
+
"tsup": "^8.0.1",
|
|
77
|
+
"typescript": "^5.4.5",
|
|
78
|
+
"vitest": "^2.1.9"
|
|
79
|
+
},
|
|
80
|
+
"scripts": {
|
|
81
|
+
"build": "tsup",
|
|
82
|
+
"clean": "rm -rf dist",
|
|
83
|
+
"typecheck": "tsc --noEmit",
|
|
84
|
+
"test": "vitest run --environment=jsdom"
|
|
85
|
+
},
|
|
86
|
+
"module": "./dist/index.js"
|
|
6
87
|
}
|