@patrick91/stickers 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,117 @@
1
+ export type StickerFinishName =
2
+ | 'gloss' | 'vinyl' | 'matte'
3
+ | 'holo' | 'transparent' | 'glitter' | 'mirror' | 'pixie' | 'brushed' | 'prismatic' | 'reflective' | 'kraft' | 'glow'
4
+ | 'low-tack' | 'hi-tack';
5
+
6
+ /** A named material, or one of them adjusted: `{ base: 'mirror', gloss: 0.6 }`. */
7
+ export type StickerFinish =
8
+ | StickerFinishName
9
+ | {
10
+ base?: StickerFinishName;
11
+ /** Strength of the laminate's reflections. 1 is gloss vinyl. */
12
+ gloss?: number;
13
+ grain?: number;
14
+ /** 0 lifts easily and leaves no mark, 0.5 is ordinary vinyl, 1 clings and leaves more behind. */
15
+ tack?: number;
16
+ };
17
+
18
+ /** `contour` follows the artwork's own outline; the others fill the artwork's box. */
19
+ export type StickerCut = 'contour' | 'circle' | 'rect' | 'rounded';
20
+
21
+ /** A ready image or frame, served from the same origin or readable via CORS. */
22
+ export type MirrorSource = HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageBitmap;
23
+
24
+ export interface MirrorSourceOptions {
25
+ /** Flip the source left to right, as for a front-facing camera. Defaults to false. */
26
+ flipX?: boolean;
27
+ }
28
+
29
+ export interface StickerOptions {
30
+ /** A loaded image, canvas or bitmap with a transparent background. */
31
+ image: HTMLImageElement | HTMLCanvasElement | ImageBitmap;
32
+ /** Width of the artwork in CSS px. Also decides the texture resolution. */
33
+ width: number;
34
+ /** Element whose centre is the sticker's home. Its rendered width sets the sticker's scale. */
35
+ anchor?: HTMLElement | null;
36
+ /** Home position in document px, when there is no anchor. */
37
+ x?: number;
38
+ y?: number;
39
+ /** Degrees. */
40
+ rotation?: number;
41
+ /** Width of the white die-cut border in px. Default 7; 0 for artwork that brings its own. */
42
+ border?: number;
43
+ finish?: StickerFinish;
44
+ cut?: StickerCut;
45
+ /** Keep enclosed transparent areas backed by stock for a contour cut. Default false. */
46
+ fillHoles?: boolean;
47
+ /** Needed for `persist`: stickers with an id remember where the visitor left them. */
48
+ id?: string;
49
+ /** Starting offset from home: [dx, dy, extra rotation in degrees]. */
50
+ offset?: [number, number, number?];
51
+ /** RGB 0..1 of the vinyl and of the adhesive side. */
52
+ paper?: [number, number, number];
53
+ back?: [number, number, number];
54
+ }
55
+
56
+ export interface EngineOptions {
57
+ /** Direction to the light: x right, y down, z towards the viewer. */
58
+ light?: [number, number, number];
59
+ /** Shadow offset in px per px of height. */
60
+ shadowSlope?: [number, number];
61
+ /** Shadow opacity, 0..1. Raise it on dark pages. Default 0.55. */
62
+ shadow?: number;
63
+ maxDpr?: number;
64
+ /** Fraction of a sticker that must be peeled before it comes off. Default 0.74. */
65
+ detachAt?: number;
66
+ liftHeight?: number;
67
+ /** An edge lifts towards a hovering mouse. Default true. */
68
+ hoverPeel?: boolean;
69
+ /** ms a finger must rest on a sticker before it grabs, so swipes still scroll. Default 170. */
70
+ touchHold?: number;
71
+ zIndex?: number;
72
+ /** px the canvas extends above and below the viewport. Default 240. */
73
+ overscan?: number;
74
+ /** localStorage key under which sticker positions are remembered. */
75
+ persist?: string | null;
76
+ /** Strength of the faint ghost a sticker leaves where it was first stuck. Default 1; 0 turns it off. */
77
+ residue?: number;
78
+ /** Highlights slide as a phone or tablet tilts. iOS asks for permission on the first tap. Default false. */
79
+ gyroscope?: boolean;
80
+ }
81
+
82
+ export class Sticker {
83
+ readonly id: string | null;
84
+ readonly state: 'idle' | 'peel' | 'held' | 'fly';
85
+ /** Centre in document px. */
86
+ readonly x: number;
87
+ readonly y: number;
88
+ /** Offset from home: [dx, dy, extra rotation in degrees]. */
89
+ readonly offset: [number, number, number];
90
+ setImage(image: StickerOptions['image']): void;
91
+ }
92
+
93
+ export class StickerEngine {
94
+ constructor(options?: EngineOptions);
95
+ /** False when WebGL2 is unavailable; nothing is drawn and add() must not be called. */
96
+ readonly ok: boolean;
97
+ readonly stickers: Sticker[];
98
+ readonly opts: Required<EngineOptions>;
99
+ onHover: ((sticker: Sticker | null) => void) | null;
100
+ /** Called after a visitor moves a sticker. */
101
+ onChange: ((sticker: Sticker) => void) | null;
102
+ add(options: StickerOptions): Sticker;
103
+ remove(sticker: Sticker): void;
104
+ /** Fly one sticker, or all of them, back home. */
105
+ reset(sticker?: Sticker): void;
106
+ /** Tip every sticker back and separate its layers. */
107
+ setExploded(on: boolean): void;
108
+ /**
109
+ * Upload a frame for the mirror finish. Call again when a video or canvas changes,
110
+ * or pass null to restore the built-in reflection. Returns false if the frame
111
+ * could not be uploaded. The caller owns playback, permissions and cleanup.
112
+ */
113
+ setMirrorSource(source: MirrorSource | null, options?: MirrorSourceOptions): boolean;
114
+ /** Ask for a redraw, e.g. after changing `opts.shadow`. */
115
+ invalidate(): void;
116
+ destroy(): void;
117
+ }