@blazediff/ui 1.6.6 → 2.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
@@ -7,8 +7,14 @@
7
7
 
8
8
  </div>
9
9
 
10
+ Headless engine and a framework-agnostic renderer for building image-diff UIs, with four comparison modes: swipe, difference, two-up, and onion skin.
10
11
 
11
- Unstyled web components for displaying image differences with multiple comparison modes: swipe, difference, two-up, and onion skin.
12
+ The package has two layers:
13
+
14
+ - **`@blazediff/ui`** — a tiny pure-JS renderer. `mount*` functions create the DOM, wire events, and update it for you. No web components, no framework.
15
+ - **`@blazediff/ui/engine`** — the headless engine. All state, calculations, and handlers live here with zero rendering, so you can drive any framework (React, Vue, Svelte, …) from it. Browser APIs only; the sole dependency is `@blazediff/core`.
16
+
17
+ > Using React? Reach for [`@blazediff/react`](https://www.npmjs.com/package/@blazediff/react) — it renders from this engine for you.
12
18
 
13
19
  ## Installation
14
20
 
@@ -16,222 +22,153 @@ Unstyled web components for displaying image differences with multiple compariso
16
22
  npm install @blazediff/ui
17
23
  ```
18
24
 
19
- ## Usage
25
+ ## Renderer
20
26
 
21
- ### Swipe Mode
27
+ Every mode exposes a `mount*(target, options)` function. It appends the UI into `target` and returns a handle:
22
28
 
23
- The swipe mode allows users to drag a divider to compare two images side by side.
24
-
25
- ```html
26
- <script type="module">
27
- import '@blazediff/ui';
28
- </script>
29
-
30
- <blazediff-swipe
31
- src1="path/to/image1.png"
32
- src2="path/to/image2.png"
33
- alt1="Before"
34
- alt2="After"
35
- class-container="swipe-container"
36
- class-image1="image-before"
37
- class-image2="image-after"
38
- class-divider="divider"
39
- ></blazediff-swipe>
29
+ ```ts
30
+ interface MountHandle<Options> {
31
+ update(options: Partial<Options>): void; // change sources/options in place
32
+ destroy(): void; // remove DOM + listeners, abort in-flight loads
33
+ }
40
34
  ```
41
35
 
42
- #### Attributes
36
+ Layout needed for a mode to function (overlay, side-by-side, etc.) is built in — classes are purely for theming.
43
37
 
44
- - `src1`: URL of the first image (before)
45
- - `src2`: URL of the second image (after)
46
- - `alt1`: Alt text for the first image (default: "Before")
47
- - `alt2`: Alt text for the second image (default: "After")
48
- - `class-container`: CSS class for the container element
49
- - `class-image1`: CSS class for the first image
50
- - `class-image2`: CSS class for the second image
51
- - `class-divider`: CSS class for the divider
38
+ `src1` and `src2` accept image URLs or `Uint8Array` values containing encoded image bytes. Both forms work in every mode.
52
39
 
53
- #### Events
40
+ ### Swipe Mode
54
41
 
55
- - `position-change`: Fired when the divider position changes
56
- - `detail.position`: The divider position as a percentage (0-100)
42
+ Drag a divider to compare two images.
57
43
 
58
- ### Difference Mode
44
+ ```ts
45
+ import { mountSwipe } from "@blazediff/ui";
59
46
 
60
- The difference mode uses the blazediff algorithm to highlight pixel differences between two images.
61
-
62
- ```html
63
- <script type="module">
64
- import '@blazediff/ui';
65
- </script>
66
-
67
- <blazediff-difference
68
- src1="path/to/image1.png"
69
- src2="path/to/image2.png"
70
- threshold="0.1"
71
- include-aa="false"
72
- alpha="0.1"
73
- class-container="diff-container"
74
- class-canvas="diff-canvas"
75
- ></blazediff-difference>
47
+ const handle = mountSwipe(document.getElementById("app")!, {
48
+ src1: "before.png",
49
+ src2: "after.png",
50
+ alt1: "Before",
51
+ alt2: "After",
52
+ onPositionChange: (position) => console.log(position), // 0–100
53
+ });
76
54
  ```
77
55
 
78
- #### Attributes
56
+ **Options:** `src1`, `src2`, `alt1` (`"Before"`), `alt2` (`"After"`), `initialPosition` (`50`), `className`, `containerClassName`, `image1ClassName`, `image2ClassName`, `dividerClassName`, `onPositionChange(position)`.
79
57
 
80
- - `src1`: URL of the first image
81
- - `src2`: URL of the second image
82
- - `threshold`: Matching threshold (0-1, default: 0.1)
83
- - `include-aa`: Include anti-aliasing in diff ("true"/"false", default: "false")
84
- - `alpha`: Blending factor for unchanged pixels (0-1, default: 0.1)
85
- - `class-container`: CSS class for the container element
86
- - `class-canvas`: CSS class for the canvas element
58
+ ### Difference Mode
87
59
 
88
- #### Events
60
+ Highlights pixel differences using the BlazeDiff algorithm and paints them to a canvas.
61
+ Pass encoded diff image bytes through `diff` to render a precomputed diff instead. When `diff` is present, the browser does not run BlazeDiff.
62
+
63
+ ```ts
64
+ import { mountDifference } from "@blazediff/ui";
65
+
66
+ mountDifference(document.getElementById("app")!, {
67
+ src1: "before.png",
68
+ src2: "after.png",
69
+ threshold: 0.1,
70
+ includeAA: false,
71
+ alpha: 0.1,
72
+ onDiffComplete: ({ diffCount, totalPixels, percentage }) =>
73
+ console.log(diffCount, percentage),
74
+ onDiffError: (error) => console.error(error),
75
+ });
76
+ ```
89
77
 
90
- - `diff-complete`: Fired when the diff calculation is complete
91
- - `detail.diffCount`: Number of different pixels
92
- - `detail.totalPixels`: Total number of pixels
93
- - `detail.percentage`: Percentage of different pixels
94
- - `diff-error`: Fired when an error occurs
95
- - `detail.error`: The error object
78
+ **Options:** `src1`, `src2`, `diff` (encoded diff image `Uint8Array`), `threshold` (`0.1`), `includeAA` (`false`), `alpha` (`0.1`), `crossOrigin` (`"anonymous"`), `className`, `containerClassName`, `canvasClassName`, `onDiffComplete({ diffCount, totalPixels, percentage })`, `onDiffError(error)`.
96
79
 
97
80
  ### Two-Up Mode
98
81
 
99
- The two-up mode displays two images side by side for quick comparison, with automatic dimension change detection.
100
-
101
- ```html
102
- <script type="module">
103
- import '@blazediff/ui';
104
- </script>
105
-
106
- <blazediff-twoup
107
- src1="path/to/image1.png"
108
- src2="path/to/image2.png"
109
- class-container="twoup-container"
110
- class-container-inner="twoup-inner"
111
- class-panel="twoup-panel"
112
- class-image="twoup-image"
113
- class-dimension-info="dimension-info"
114
- ></blazediff-twoup>
115
- ```
116
-
117
- #### Attributes
82
+ Two images side by side, with automatic dimension-change detection.
118
83
 
119
- - `src1`: URL of the first image
120
- - `src2`: URL of the second image
121
- - `class-container`: CSS class for the outer container
122
- - `class-container-inner`: CSS class for the inner container holding the panels
123
- - `class-panel`: CSS class for each image panel
124
- - `class-image`: CSS class for the images
125
- - `class-dimension-info`: CSS class for the dimension info display
84
+ ```ts
85
+ import { mountTwoUp } from "@blazediff/ui";
126
86
 
127
- #### Events
87
+ mountTwoUp(document.getElementById("app")!, {
88
+ src1: "before.png",
89
+ src2: "after.png",
90
+ onImagesLoaded: ({ image1, image2 }) => console.log(image1, image2),
91
+ onLoadError: (error) => console.error(error),
92
+ });
93
+ ```
128
94
 
129
- - `images-loaded`: Fired when both images are loaded
130
- - `detail.image1`: Object with width and height of first image
131
- - `detail.image2`: Object with width and height of second image
132
- - `load-error`: Fired when an error occurs loading images
133
- - `detail.error`: The error object
95
+ **Options:** `src1`, `src2`, `crossOrigin` (`"anonymous"`), `className`, `containerClassName`, `containerInnerClassName`, `panelClassName`, `imageClassName`, `dimensionInfoClassName`, `onImagesLoaded({ image1, image2 })`, `onLoadError(error)`.
134
96
 
135
97
  ### Onion Skin Mode
136
98
 
137
- The onion skin mode overlays two images with adjustable opacity, perfect for detecting small pixel shifts.
138
-
139
- ```html
140
- <script type="module">
141
- import '@blazediff/ui';
142
- </script>
143
-
144
- <blazediff-onionskin
145
- src1="path/to/image1.png"
146
- src2="path/to/image2.png"
147
- opacity="50"
148
- class-container="onionskin-container"
149
- class-image-container="onionskin-images"
150
- class-image="onionskin-image"
151
- class-slider-container="slider-container"
152
- class-slider="opacity-slider"
153
- class-slider-label="slider-label"
154
- ></blazediff-onionskin>
155
- ```
156
-
157
- #### Attributes
99
+ Overlays two images with an adjustable-opacity slider — great for spotting small pixel shifts.
158
100
 
159
- - `src1`: URL of the first image (bottom layer)
160
- - `src2`: URL of the second image (top layer)
161
- - `opacity`: Initial opacity of the top image (0-100, default: 50)
162
- - `class-container`: CSS class for the main container
163
- - `class-image-container`: CSS class for the image container
164
- - `class-image`: CSS class for the images
165
- - `class-slider-container`: CSS class for the slider container
166
- - `class-slider`: CSS class for the opacity slider
167
- - `class-slider-label`: CSS class for the slider label
101
+ ```ts
102
+ import { mountOnionSkin } from "@blazediff/ui";
168
103
 
169
- #### Events
104
+ mountOnionSkin(document.getElementById("app")!, {
105
+ src1: "before.png",
106
+ src2: "after.png",
107
+ opacity: 50,
108
+ sliderLabelText: "Opacity:",
109
+ onOpacityChange: (opacity) => console.log(opacity), // 0–100
110
+ });
111
+ ```
170
112
 
171
- - `opacity-change`: Fired when the opacity slider changes
172
- - `detail.opacity`: The opacity value (0-100)
173
- - `images-loaded`: Fired when both images are loaded
174
- - `detail.image1`: Object with width and height of first image
175
- - `detail.image2`: Object with width and height of second image
176
- - `load-error`: Fired when an error occurs loading images
177
- - `detail.error`: The error object
113
+ **Options:** `src1`, `src2`, `opacity` (`50`), `crossOrigin` (`"anonymous"`), `sliderLabelText` (`"Opacity:"`), `className`, `containerClassName`, `imageContainerClassName`, `imageClassName`, `sliderContainerClassName`, `sliderClassName`, `sliderLabelClassName`, `onOpacityChange(opacity)`, `onImagesLoaded({ image1, image2 })`, `onLoadError(error)`.
178
114
 
179
115
  ## Styling
180
116
 
181
- All components are unstyled by default. You can apply your own styles using the `class-*` attributes:
117
+ Renderers are unstyled beyond the layout each mode needs. Pass any class strings via the `*ClassName` options — Tailwind, CSS modules, plain CSS, anything:
182
118
 
183
- ```css
184
- .swipe-container {
185
- width: 100%;
186
- height: 500px;
187
- }
119
+ ```ts
120
+ mountSwipe(target, {
121
+ src1,
122
+ src2,
123
+ containerClassName: "h-[500px] w-full",
124
+ dividerClassName: "w-1 bg-blue-500",
125
+ });
126
+ ```
188
127
 
189
- .divider {
190
- background-color: #3b82f6;
191
- width: 4px;
192
- box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
193
- }
128
+ ## Headless engine
194
129
 
195
- .diff-container {
196
- display: flex;
197
- justify-content: center;
198
- }
130
+ When you need full control — or you're wiring up a framework other than React — drive the engine directly from `@blazediff/ui/engine`. Each factory returns a controller:
199
131
 
200
- .diff-canvas {
201
- max-width: 100%;
202
- height: auto;
132
+ ```ts
133
+ interface Engine<State, Config, Actions> {
134
+ getState(): State;
135
+ subscribe(listener: () => void): () => void;
136
+ setConfig(config: Partial<Config>): void; // reloads only on real changes
137
+ actions: Actions;
138
+ destroy(): void;
203
139
  }
204
140
  ```
205
141
 
206
- ## Example with Event Handling
207
-
208
- ```html
209
- <blazediff-swipe
210
- id="swipe-viewer"
211
- src1="image1.png"
212
- src2="image2.png"
213
- ></blazediff-swipe>
214
-
215
- <script>
216
- const viewer = document.getElementById('swipe-viewer');
217
- viewer.addEventListener('position-change', (e) => {
218
- console.log('Position:', e.detail.position);
219
- });
220
- </script>
221
- ```
142
+ ```ts
143
+ import { createSwipeEngine } from "@blazediff/ui/engine";
144
+
145
+ const engine = createSwipeEngine(50);
146
+ const unsubscribe = engine.subscribe(() => {
147
+ const { position, isDragging } = engine.getState();
148
+ // render position (0–100) however your framework wants
149
+ });
222
150
 
223
- ```html
224
- <blazediff-difference
225
- id="diff-viewer"
226
- src1="image1.png"
227
- src2="image2.png"
228
- ></blazediff-difference>
229
-
230
- <script>
231
- const viewer = document.getElementById('diff-viewer');
232
- viewer.addEventListener('diff-complete', (e) => {
233
- console.log('Diff pixels:', e.detail.diffCount);
234
- console.log('Diff percentage:', e.detail.percentage + '%');
235
- });
236
- </script>
151
+ // feed it the already-computed percentage; the engine clamps + guards dragging
152
+ engine.actions.start(40);
153
+ engine.actions.move(55);
154
+ engine.actions.end();
155
+
156
+ unsubscribe();
157
+ engine.destroy();
237
158
  ```
159
+
160
+ Factories and helpers:
161
+
162
+ - `createDifferenceEngine(config)` — state `{ status, diff?: { output, width, height, diffCount, totalPixels, percentage }, error? }`. Computes the diff buffer; you paint it.
163
+ - `createSwipeEngine(initialPosition = 50)` — state `{ position, isDragging }`; actions `start`/`move`/`end`/`setPosition` (positions are 0–100 percentages).
164
+ - `createTwoUpEngine(config)` — state `{ status, dims1, dims2, dimensionLabel, changed, error }`.
165
+ - `createOnionSkinEngine(config, initialOpacity = 50)` — state `{ status, opacity, dims1, dims2, error }`; action `setOpacity`.
166
+ - Helpers: `formatDimensionLabel`, `normalizedOpacity`, `loadImageElement`, `getImageData`, `createStore`.
167
+
168
+ The engine uses browser APIs (`Image`, a throwaway `<canvas>` for pixel extraction) but never touches the surface you render to — that boundary is what keeps it framework-agnostic.
169
+
170
+ ## Links
171
+
172
+ - [GitHub Repository](https://github.com/teimurjan/blazediff)
173
+ - [NPM Package](https://www.npmjs.com/package/@blazediff/ui)
174
+ - [Examples →](https://blazediff.dev/examples/vanilla-components)
@@ -0,0 +1,37 @@
1
+ import { b as DifferenceConfig, c as DifferenceEngine, I as ImageSource, O as OnionSkinConfig, d as OnionSkinEngine, S as SwipeEngine, T as TwoUpConfig, e as TwoUpEngine, D as Dimensions } from './types-HyqeYjLD.js';
2
+ export { a as DiffStats, f as DifferenceState, E as Engine, g as OnionSkinActions, h as OnionSkinState, i as Status, j as SwipeActions, k as SwipeState, l as TwoUpState } from './types-HyqeYjLD.js';
3
+
4
+ declare function createDifferenceEngine(config: DifferenceConfig): DifferenceEngine;
5
+
6
+ interface ImageSourceUrl {
7
+ url: string;
8
+ revoke(): void;
9
+ }
10
+ interface ImageSourceBinding {
11
+ update(source?: ImageSource): void;
12
+ destroy(): void;
13
+ }
14
+ declare function createImageSourceUrl(source: ImageSource): ImageSourceUrl;
15
+ declare function bindImageSource(image: HTMLImageElement, initialSource?: ImageSource): ImageSourceBinding;
16
+ interface LoadImageOptions {
17
+ crossOrigin?: string | null;
18
+ }
19
+ declare function loadImageElement(source: ImageSource, options?: LoadImageOptions): Promise<HTMLImageElement>;
20
+ declare function getImageData(img: HTMLImageElement): ImageData;
21
+
22
+ declare function normalizedOpacity(opacity: number): number;
23
+ declare function createOnionSkinEngine(config: OnionSkinConfig, initialOpacity?: number): OnionSkinEngine;
24
+
25
+ interface Store<S> {
26
+ get(): S;
27
+ set(next: S | ((prev: S) => S)): void;
28
+ subscribe(listener: () => void): () => void;
29
+ }
30
+ declare function createStore<S>(initial: S): Store<S>;
31
+
32
+ declare function createSwipeEngine(initialPosition?: number): SwipeEngine;
33
+
34
+ declare function formatDimensionLabel(dims1: Dimensions, dims2: Dimensions): string;
35
+ declare function createTwoUpEngine(config: TwoUpConfig): TwoUpEngine;
36
+
37
+ export { DifferenceConfig, DifferenceEngine, Dimensions, ImageSource, type ImageSourceBinding, type ImageSourceUrl, type LoadImageOptions, OnionSkinConfig, OnionSkinEngine, type Store, SwipeEngine, TwoUpConfig, TwoUpEngine, bindImageSource, createDifferenceEngine, createImageSourceUrl, createOnionSkinEngine, createStore, createSwipeEngine, createTwoUpEngine, formatDimensionLabel, getImageData, loadImageElement, normalizedOpacity };
package/dist/engine.js ADDED
@@ -0,0 +1 @@
1
+ 'use strict';var A=require('@blazediff/core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var A__default=/*#__PURE__*/_interopDefault(A);function v(n){return n.buffer instanceof ArrayBuffer?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):Uint8Array.from(n)}function O(n){if(typeof n=="string")return {url:n,revoke(){}};let t=URL.createObjectURL(new Blob([v(n)]));return {url:t,revoke(){URL.revokeObjectURL(t);}}}function L(n,t){let r=t,e,i=a=>{let o=e;e=a===void 0?void 0:O(a),e?n.src=e.url:n.removeAttribute("src"),o?.revoke();};return i(t),{update(a){a!==r&&(r=a,i(a));},destroy(){e?.revoke(),e=void 0;}}}function l(n,t={}){let{crossOrigin:r="anonymous"}=t,e=O(n);return new Promise((i,a)=>{let o=new Image;r!==null&&(o.crossOrigin=r),o.onload=()=>i(o),o.onerror=a,o.src=e.url;}).finally(e.revoke)}function y(n){let t=document.createElement("canvas");t.width=n.naturalWidth,t.height=n.naturalHeight;let r=t.getContext("2d");if(!r)throw new Error("Could not get canvas context");return r.drawImage(n,0,0),r.getImageData(0,0,n.naturalWidth,n.naturalHeight)}function d(n){let t=n,r=new Set;return {get(){return t},set(e){t=typeof e=="function"?e(t):e;for(let i of r)i();},subscribe(e){return r.add(e),()=>{r.delete(e);}}}}var T=["src1","src2","threshold","includeAA","alpha","crossOrigin","enabled"];function k(n){let t=d({status:"idle"}),r={...n},e=0,i=false;async function a(){let o=++e,{src1:c,src2:s,threshold:g=.1,includeAA:f=false,alpha:u=.1,enabled:p=true}=r;if(!p||!c||!s){t.set({status:"idle"});return}t.set({status:"loading"});try{let[m,h]=await Promise.all([l(c,{crossOrigin:r.crossOrigin}),l(s,{crossOrigin:r.crossOrigin})]);if(i||o!==e)return;if(m.naturalWidth!==h.naturalWidth||m.naturalHeight!==h.naturalHeight)throw new Error(`Image dimensions do not match. Image 1: ${m.naturalWidth}x${m.naturalHeight}, Image 2: ${h.naturalWidth}x${h.naturalHeight}`);let S=m.naturalWidth,w=m.naturalHeight,x=y(m).data,C=y(h).data;if(i||o!==e)return;let D=new Uint8ClampedArray(S*w*4),I=A__default.default(x,C,D,S,w,{threshold:g,includeAA:f,alpha:u});if(i||o!==e)return;let E=S*w;t.set({status:"ready",diff:{output:D,width:S,height:w,diffCount:I,totalPixels:E,percentage:I/E*100}});}catch(m){if(i||o!==e)return;t.set({status:"error",error:m});}}return a(),{getState:t.get,subscribe:t.subscribe,setConfig(o){let c=T.some(s=>s in o&&o[s]!==r[s]);r={...r,...o},c&&a();},actions:{},destroy(){i=true,e++;}}}var H=["src1","src2","crossOrigin"];function $(n){return n/100}function W(n,t=50){let r=d({status:"idle",opacity:Math.max(0,Math.min(100,t))}),e={...n},i=0,a=false;async function o(){let c=++i,{src1:s,src2:g}=e;if(!s||!g){r.set(f=>({...f,status:"idle"}));return}r.set(f=>({...f,status:"loading"}));try{let[f,u]=await Promise.all([l(s,{crossOrigin:e.crossOrigin}),l(g,{crossOrigin:e.crossOrigin})]);if(a||c!==i)return;let p={width:f.naturalWidth,height:f.naturalHeight},m={width:u.naturalWidth,height:u.naturalHeight};r.set(h=>({...h,status:"ready",dims1:p,dims2:m}));}catch(f){if(a||c!==i)return;r.set(u=>({...u,status:"error",error:f}));}}return o(),{getState:r.get,subscribe:r.subscribe,setConfig(c){let s=H.some(g=>g in c&&c[g]!==e[g]);e={...e,...c},s&&o();},actions:{setOpacity(c){r.set(s=>({...s,opacity:Math.max(0,Math.min(100,c))}));}},destroy(){a=true,i++;}}}function b(n){return Math.max(0,Math.min(100,n))}function M(n=50){let t=d({position:b(n),isDragging:false});function r(e){t.set(i=>({...i,position:b(e)}));}return {getState:t.get,subscribe:t.subscribe,setConfig(){},actions:{start(e){t.set({position:b(e),isDragging:true});},move(e){t.get().isDragging&&r(e);},end(){t.set(e=>({...e,isDragging:false}));},setPosition:r},destroy(){}}}var P=["src1","src2","crossOrigin"];function U(n,t){if(n.width===t.width&&n.height===t.height)return `${n.width}\xD7${n.height}`;let r=t.width-n.width,e=t.height-n.height,i=a=>a>0?"+":"";return `Dimensions changed: ${n.width}\xD7${n.height} \u2192 ${t.width}\xD7${t.height} (${i(r)}${r}\xD7${i(e)}${e})`}function B(n){let t=d({status:"idle"}),r={...n},e=0,i=false;async function a(){let o=++e,{src1:c,src2:s}=r;if(!c||!s){t.set({status:"idle"});return}t.set({status:"loading"});try{let[g,f]=await Promise.all([l(c,{crossOrigin:r.crossOrigin}),l(s,{crossOrigin:r.crossOrigin})]);if(i||o!==e)return;let u={width:g.naturalWidth,height:g.naturalHeight},p={width:f.naturalWidth,height:f.naturalHeight};t.set({status:"ready",dims1:u,dims2:p,dimensionLabel:U(u,p),changed:u.width!==p.width||u.height!==p.height});}catch(g){if(i||o!==e)return;t.set({status:"error",error:g});}}return a(),{getState:t.get,subscribe:t.subscribe,setConfig(o){let c=P.some(s=>s in o&&o[s]!==r[s]);r={...r,...o},c&&a();},actions:{},destroy(){i=true,e++;}}}exports.bindImageSource=L;exports.createDifferenceEngine=k;exports.createImageSourceUrl=O;exports.createOnionSkinEngine=W;exports.createStore=d;exports.createSwipeEngine=M;exports.createTwoUpEngine=B;exports.formatDimensionLabel=U;exports.getImageData=y;exports.loadImageElement=l;exports.normalizedOpacity=$;
@@ -0,0 +1 @@
1
+ import A from'@blazediff/core';function v(n){return n.buffer instanceof ArrayBuffer?new Uint8Array(n.buffer,n.byteOffset,n.byteLength):Uint8Array.from(n)}function O(n){if(typeof n=="string")return {url:n,revoke(){}};let t=URL.createObjectURL(new Blob([v(n)]));return {url:t,revoke(){URL.revokeObjectURL(t);}}}function L(n,t){let r=t,e,i=a=>{let o=e;e=a===void 0?void 0:O(a),e?n.src=e.url:n.removeAttribute("src"),o?.revoke();};return i(t),{update(a){a!==r&&(r=a,i(a));},destroy(){e?.revoke(),e=void 0;}}}function l(n,t={}){let{crossOrigin:r="anonymous"}=t,e=O(n);return new Promise((i,a)=>{let o=new Image;r!==null&&(o.crossOrigin=r),o.onload=()=>i(o),o.onerror=a,o.src=e.url;}).finally(e.revoke)}function y(n){let t=document.createElement("canvas");t.width=n.naturalWidth,t.height=n.naturalHeight;let r=t.getContext("2d");if(!r)throw new Error("Could not get canvas context");return r.drawImage(n,0,0),r.getImageData(0,0,n.naturalWidth,n.naturalHeight)}function d(n){let t=n,r=new Set;return {get(){return t},set(e){t=typeof e=="function"?e(t):e;for(let i of r)i();},subscribe(e){return r.add(e),()=>{r.delete(e);}}}}var T=["src1","src2","threshold","includeAA","alpha","crossOrigin","enabled"];function k(n){let t=d({status:"idle"}),r={...n},e=0,i=false;async function a(){let o=++e,{src1:c,src2:s,threshold:g=.1,includeAA:f=false,alpha:u=.1,enabled:p=true}=r;if(!p||!c||!s){t.set({status:"idle"});return}t.set({status:"loading"});try{let[m,h]=await Promise.all([l(c,{crossOrigin:r.crossOrigin}),l(s,{crossOrigin:r.crossOrigin})]);if(i||o!==e)return;if(m.naturalWidth!==h.naturalWidth||m.naturalHeight!==h.naturalHeight)throw new Error(`Image dimensions do not match. Image 1: ${m.naturalWidth}x${m.naturalHeight}, Image 2: ${h.naturalWidth}x${h.naturalHeight}`);let S=m.naturalWidth,w=m.naturalHeight,x=y(m).data,C=y(h).data;if(i||o!==e)return;let D=new Uint8ClampedArray(S*w*4),I=A(x,C,D,S,w,{threshold:g,includeAA:f,alpha:u});if(i||o!==e)return;let E=S*w;t.set({status:"ready",diff:{output:D,width:S,height:w,diffCount:I,totalPixels:E,percentage:I/E*100}});}catch(m){if(i||o!==e)return;t.set({status:"error",error:m});}}return a(),{getState:t.get,subscribe:t.subscribe,setConfig(o){let c=T.some(s=>s in o&&o[s]!==r[s]);r={...r,...o},c&&a();},actions:{},destroy(){i=true,e++;}}}var H=["src1","src2","crossOrigin"];function $(n){return n/100}function W(n,t=50){let r=d({status:"idle",opacity:Math.max(0,Math.min(100,t))}),e={...n},i=0,a=false;async function o(){let c=++i,{src1:s,src2:g}=e;if(!s||!g){r.set(f=>({...f,status:"idle"}));return}r.set(f=>({...f,status:"loading"}));try{let[f,u]=await Promise.all([l(s,{crossOrigin:e.crossOrigin}),l(g,{crossOrigin:e.crossOrigin})]);if(a||c!==i)return;let p={width:f.naturalWidth,height:f.naturalHeight},m={width:u.naturalWidth,height:u.naturalHeight};r.set(h=>({...h,status:"ready",dims1:p,dims2:m}));}catch(f){if(a||c!==i)return;r.set(u=>({...u,status:"error",error:f}));}}return o(),{getState:r.get,subscribe:r.subscribe,setConfig(c){let s=H.some(g=>g in c&&c[g]!==e[g]);e={...e,...c},s&&o();},actions:{setOpacity(c){r.set(s=>({...s,opacity:Math.max(0,Math.min(100,c))}));}},destroy(){a=true,i++;}}}function b(n){return Math.max(0,Math.min(100,n))}function M(n=50){let t=d({position:b(n),isDragging:false});function r(e){t.set(i=>({...i,position:b(e)}));}return {getState:t.get,subscribe:t.subscribe,setConfig(){},actions:{start(e){t.set({position:b(e),isDragging:true});},move(e){t.get().isDragging&&r(e);},end(){t.set(e=>({...e,isDragging:false}));},setPosition:r},destroy(){}}}var P=["src1","src2","crossOrigin"];function U(n,t){if(n.width===t.width&&n.height===t.height)return `${n.width}\xD7${n.height}`;let r=t.width-n.width,e=t.height-n.height,i=a=>a>0?"+":"";return `Dimensions changed: ${n.width}\xD7${n.height} \u2192 ${t.width}\xD7${t.height} (${i(r)}${r}\xD7${i(e)}${e})`}function B(n){let t=d({status:"idle"}),r={...n},e=0,i=false;async function a(){let o=++e,{src1:c,src2:s}=r;if(!c||!s){t.set({status:"idle"});return}t.set({status:"loading"});try{let[g,f]=await Promise.all([l(c,{crossOrigin:r.crossOrigin}),l(s,{crossOrigin:r.crossOrigin})]);if(i||o!==e)return;let u={width:g.naturalWidth,height:g.naturalHeight},p={width:f.naturalWidth,height:f.naturalHeight};t.set({status:"ready",dims1:u,dims2:p,dimensionLabel:U(u,p),changed:u.width!==p.width||u.height!==p.height});}catch(g){if(i||o!==e)return;t.set({status:"error",error:g});}}return a(),{getState:t.get,subscribe:t.subscribe,setConfig(o){let c=P.some(s=>s in o&&o[s]!==r[s]);r={...r,...o},c&&a();},actions:{},destroy(){i=true,e++;}}}export{L as bindImageSource,k as createDifferenceEngine,O as createImageSourceUrl,W as createOnionSkinEngine,d as createStore,M as createSwipeEngine,B as createTwoUpEngine,U as formatDimensionLabel,y as getImageData,l as loadImageElement,$ as normalizedOpacity};
package/dist/index.d.ts CHANGED
@@ -1,5 +1,83 @@
1
- export { BaseElement } from './base-element.js';
2
- export { DifferenceMode } from './difference-mode.js';
3
- export { OnionSkinMode } from './onion-skin-mode.js';
4
- export { SwipeMode } from './swipe-mode.js';
5
- export { TwoUpMode } from './two-up-mode.js';
1
+ import { D as Dimensions } from './types-HyqeYjLD.js';
2
+ export { a as DiffStats } from './types-HyqeYjLD.js';
3
+
4
+ type ImageSource = string | Uint8Array;
5
+ interface MountHandle<O> {
6
+ update(options: Partial<O>): void;
7
+ destroy(): void;
8
+ }
9
+ interface ImagesLoadedDetail {
10
+ image1: Dimensions;
11
+ image2: Dimensions;
12
+ }
13
+ interface DifferenceMountOptions {
14
+ src1: ImageSource;
15
+ src2: ImageSource;
16
+ diff?: Uint8Array;
17
+ threshold?: number;
18
+ includeAA?: boolean;
19
+ alpha?: number;
20
+ crossOrigin?: string | null;
21
+ className?: string;
22
+ containerClassName?: string;
23
+ canvasClassName?: string;
24
+ onDiffComplete?: (detail: {
25
+ diffCount: number;
26
+ totalPixels: number;
27
+ percentage: number;
28
+ }) => void;
29
+ onDiffError?: (error: unknown) => void;
30
+ }
31
+ interface SwipeMountOptions {
32
+ src1: ImageSource;
33
+ src2: ImageSource;
34
+ alt1?: string;
35
+ alt2?: string;
36
+ initialPosition?: number;
37
+ className?: string;
38
+ containerClassName?: string;
39
+ image1ClassName?: string;
40
+ image2ClassName?: string;
41
+ dividerClassName?: string;
42
+ onPositionChange?: (position: number) => void;
43
+ }
44
+ interface TwoUpMountOptions {
45
+ src1: ImageSource;
46
+ src2: ImageSource;
47
+ crossOrigin?: string | null;
48
+ className?: string;
49
+ containerClassName?: string;
50
+ containerInnerClassName?: string;
51
+ panelClassName?: string;
52
+ imageClassName?: string;
53
+ dimensionInfoClassName?: string;
54
+ onImagesLoaded?: (detail: ImagesLoadedDetail) => void;
55
+ onLoadError?: (error: unknown) => void;
56
+ }
57
+ interface OnionSkinMountOptions {
58
+ src1: ImageSource;
59
+ src2: ImageSource;
60
+ opacity?: number;
61
+ crossOrigin?: string | null;
62
+ sliderLabelText?: string;
63
+ className?: string;
64
+ containerClassName?: string;
65
+ imageContainerClassName?: string;
66
+ imageClassName?: string;
67
+ sliderContainerClassName?: string;
68
+ sliderClassName?: string;
69
+ sliderLabelClassName?: string;
70
+ onOpacityChange?: (opacity: number) => void;
71
+ onImagesLoaded?: (detail: ImagesLoadedDetail) => void;
72
+ onLoadError?: (error: unknown) => void;
73
+ }
74
+
75
+ declare function mountDifference(target: HTMLElement, options: DifferenceMountOptions): MountHandle<DifferenceMountOptions>;
76
+
77
+ declare function mountOnionSkin(target: HTMLElement, options: OnionSkinMountOptions): MountHandle<OnionSkinMountOptions>;
78
+
79
+ declare function mountSwipe(target: HTMLElement, options: SwipeMountOptions): MountHandle<SwipeMountOptions>;
80
+
81
+ declare function mountTwoUp(target: HTMLElement, options: TwoUpMountOptions): MountHandle<TwoUpMountOptions>;
82
+
83
+ export { type DifferenceMountOptions, Dimensions, type ImageSource, type ImagesLoadedDetail, type MountHandle, type OnionSkinMountOptions, type SwipeMountOptions, type TwoUpMountOptions, mountDifference, mountOnionSkin, mountSwipe, mountTwoUp };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- 'use strict';var f=require('@blazediff/core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var f__default=/*#__PURE__*/_interopDefault(f);var r=class extends HTMLElement{getClassName(o){return this.getAttribute(`class-${o}`)||""}applyClassName(o,t){let i=this.getClassName(t);i&&(o.className=i);}};var m=class extends r{constructor(){super(...arguments);this.container=null;this.canvas=null;this.ctx=null;}static get observedAttributes(){return ["src1","src2","threshold","include-aa","alpha","class-container","class-canvas"]}connectedCallback(){this.render(),this.loadAndCompare();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":case "threshold":case "include-aa":case "alpha":this.loadAndCompare();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-canvas":this.canvas&&this.applyClassName(this.canvas,"canvas");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.canvas=document.createElement("canvas"),this.applyClassName(this.canvas,"canvas"),this.ctx=this.canvas.getContext("2d"),this.container.appendChild(this.canvas),this.appendChild(this.container);}async loadAndCompare(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.canvas||!this.ctx))try{let[e,s]=await Promise.all([this.loadImage(t),this.loadImage(i)]);if(e.width!==s.width||e.height!==s.height)throw new Error(`Image dimensions do not match. Image 1: ${e.width}x${e.height}, Image 2: ${s.width}x${s.height}`);let a=e.width,n=e.height;this.canvas.width=a,this.canvas.height=n;let h=this.getImageData(e),l=this.getImageData(s),u=new Uint8ClampedArray(a*n*4),C={threshold:Number(this.getAttribute("threshold"))||.1,includeAA:this.getAttribute("include-aa")==="true",alpha:Number(this.getAttribute("alpha"))||.1},v=f__default.default(h.data,l.data,u,a,n,C),b=new ImageData(u,a,n);this.ctx.putImageData(b,0,0),this.dispatchEvent(new CustomEvent("diff-complete",{detail:{diffCount:v,totalPixels:a*n,percentage:v/(a*n)*100}}));}catch(e){this.dispatchEvent(new CustomEvent("diff-error",{detail:{error:e}}));}}loadImage(t){return new Promise((i,e)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>i(s),s.onerror=e,s.src=t;})}getImageData(t){let i=document.createElement("canvas");i.width=t.width,i.height=t.height;let e=i.getContext("2d");if(!e)throw new Error("Could not get canvas context");return e.drawImage(t,0,0),e.getImageData(0,0,t.width,t.height)}};customElements.define("blazediff-difference",m);var d=class extends r{constructor(){super(...arguments);this.container=null;this.imageContainer=null;this.img1=null;this.img2=null;this.sliderContainer=null;this.slider=null;this.sliderLabel=null;}static get observedAttributes(){return ["src1","src2","opacity","class-container","class-image-container","class-image","class-slider-container","class-slider","class-slider-label","text-slider-label"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":this.loadImages();break;case "opacity":this.updateOpacity();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image-container":this.imageContainer&&this.applyClassName(this.imageContainer,"image-container");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-slider-container":this.sliderContainer&&this.applyClassName(this.sliderContainer,"slider-container");break;case "class-slider":this.slider&&this.applyClassName(this.slider,"slider");break;case "class-slider-label":this.sliderLabel&&this.applyClassName(this.sliderLabel,"slider-label");break;case "text-slider-label":this.sliderLabel&&(this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity: ");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.imageContainer=document.createElement("div"),this.applyClassName(this.imageContainer,"image-container"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.img2.style.position="absolute",this.img2.style.top="0",this.img2.style.left="0",this.imageContainer.style.position="relative",this.imageContainer.appendChild(this.img1),this.imageContainer.appendChild(this.img2),this.sliderContainer=document.createElement("div"),this.applyClassName(this.sliderContainer,"slider-container"),this.sliderLabel=document.createElement("label"),this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity:",this.applyClassName(this.sliderLabel,"slider-label"),this.slider=document.createElement("input"),this.slider.type="range",this.slider.min="0",this.slider.max="100",this.slider.value=this.getAttribute("opacity")||"50",this.applyClassName(this.slider,"slider"),this.slider.addEventListener("input",t=>{let i=t.target.value;this.setAttribute("opacity",i),this.dispatchEvent(new CustomEvent("opacity-change",{detail:{opacity:Number(i)}}));}),this.sliderContainer.appendChild(this.sliderLabel),this.sliderContainer.appendChild(this.slider),this.container.appendChild(this.imageContainer),this.container.appendChild(this.sliderContainer),this.appendChild(this.container),this.updateOpacity();}updateOpacity(){if(!this.img2||!this.slider)return;let t=this.getAttribute("opacity")||"50";this.img2.style.opacity=(Number(t)/100).toString(),this.slider.value!==t&&(this.slider.value=t);}async loadImages(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.img1||!this.img2))try{this.img1.src=t,this.img2.src=i,await Promise.all([new Promise((e,s)=>{if(!this.img1)return s();this.img1.onload=e,this.img1.onerror=s;}),new Promise((e,s)=>{if(!this.img2)return s();this.img2.onload=e,this.img2.onerror=s;})]),this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:this.img1.naturalWidth,height:this.img1.naturalHeight},image2:{width:this.img2.naturalWidth,height:this.img2.naturalHeight}}}));}catch(e){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:e}}));}}};customElements.define("blazediff-onionskin",d);var g=class extends r{constructor(){super(...arguments);this.container=null;this.image1=null;this.image2=null;this.divider=null;this.isDragging=false;this.currentPosition=50;}static get observedAttributes(){return ["src1","src2","alt1","alt2","class-container","class-image1","class-image2","class-divider"]}connectedCallback(){this.render(),this.setupEventListeners();}disconnectedCallback(){this.removeEventListeners();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":this.image1&&(this.image1.src=e);break;case "src2":this.image2&&(this.image2.src=e);break;case "alt1":this.image1&&(this.image1.alt=e);break;case "alt2":this.image2&&(this.image2.alt=e);break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image1":this.image1&&this.applyClassName(this.image1,"image1");break;case "class-image2":this.image2&&this.applyClassName(this.image2,"image2");break;case "class-divider":this.divider&&this.applyClassName(this.divider,"divider");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.container.style.position="relative",this.container.style.overflow="hidden",this.container.style.cursor="ew-resize",this.container.style.webkitTapHighlightColor="transparent",this.container.style.webkitTouchCallout="none",this.container.style.webkitUserSelect="none",this.container.style.khtmlUserSelect="none",this.container.style.mozUserSelect="none",this.container.style.msUserSelect="none",this.container.style.userSelect="none",this.image1=document.createElement("img"),this.applyClassName(this.image1,"image1"),this.image1.src=this.getAttribute("src1")||"",this.image1.alt=this.getAttribute("alt1")||"Before",this.image1.style.width="100%",this.image1.style.height="100%",this.image1.style.objectFit="contain",this.image1.style.pointerEvents="none",this.image2=document.createElement("img"),this.applyClassName(this.image2,"image2"),this.image2.src=this.getAttribute("src2")||"",this.image2.alt=this.getAttribute("alt2")||"After",this.image2.style.position="absolute",this.image2.style.top="0",this.image2.style.left="0",this.image2.style.width="100%",this.image2.style.height="100%",this.image2.style.objectFit="contain",this.image2.style.clipPath=`inset(0 0 0 ${this.currentPosition}%)`,this.image2.style.pointerEvents="none",this.divider=document.createElement("div"),this.applyClassName(this.divider,"divider"),this.divider.style.position="absolute",this.divider.style.top="0",this.divider.style.bottom="0",this.divider.style.left=`${this.currentPosition}%`,this.divider.style.width="2px",this.divider.style.backgroundColor="white",this.divider.style.cursor="ew-resize",this.divider.style.zIndex="10",this.container.appendChild(this.image1),this.container.appendChild(this.image2),this.container.appendChild(this.divider),this.appendChild(this.container);}setupEventListeners(){this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.handleTouchStart=this.handleTouchStart.bind(this),this.handleTouchMove=this.handleTouchMove.bind(this),this.handleTouchEnd=this.handleTouchEnd.bind(this),this.container?.addEventListener("mousedown",this.handleMouseDown),this.container?.addEventListener("touchstart",this.handleTouchStart);}removeEventListeners(){this.container?.removeEventListener("mousedown",this.handleMouseDown),this.container?.removeEventListener("touchstart",this.handleTouchStart),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp),document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}handleMouseDown(t){this.isDragging=true,document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp),this.updatePosition(t.clientX);}handleMouseMove(t){this.isDragging&&this.updatePosition(t.clientX);}handleMouseUp(){this.isDragging=false,document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp);}handleTouchStart(t){this.isDragging=true,document.addEventListener("touchmove",this.handleTouchMove),document.addEventListener("touchend",this.handleTouchEnd);let i=t.touches[0];this.updatePosition(i.clientX);}handleTouchMove(t){if(!this.isDragging)return;let i=t.touches[0];this.updatePosition(i.clientX);}handleTouchEnd(){this.isDragging=false,document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}updatePosition(t){if(!this.container||!this.image2||!this.divider)return;let i=this.container.getBoundingClientRect(),e=t-i.left,s=Math.max(0,Math.min(100,e/i.width*100));this.currentPosition=s,this.image2.style.clipPath=`inset(0 0 0 ${s}%)`,this.divider.style.left=`${s}%`,this.dispatchEvent(new CustomEvent("position-change",{detail:{position:s}}));}};customElements.define("blazediff-swipe",g);var p=class extends r{constructor(){super(...arguments);this.container=null;this.containerInner=null;this.leftPanel=null;this.rightPanel=null;this.img1=null;this.img2=null;this.dimensionInfo=null;}static get observedAttributes(){return ["src1","src2","class-container","class-container-inner","class-panel","class-image","class-dimension-info"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":this.loadImages();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-container-inner":this.containerInner&&this.applyClassName(this.containerInner,"container-inner");break;case "class-panel":this.leftPanel&&this.applyClassName(this.leftPanel,"panel"),this.rightPanel&&this.applyClassName(this.rightPanel,"panel");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-dimension-info":this.dimensionInfo&&this.applyClassName(this.dimensionInfo,"dimension-info");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.containerInner=document.createElement("div"),this.applyClassName(this.containerInner,"container-inner"),this.leftPanel=document.createElement("div"),this.applyClassName(this.leftPanel,"panel"),this.rightPanel=document.createElement("div"),this.applyClassName(this.rightPanel,"panel"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.dimensionInfo=document.createElement("div"),this.applyClassName(this.dimensionInfo,"dimension-info"),this.leftPanel.appendChild(this.img1),this.rightPanel.appendChild(this.img2),this.containerInner.appendChild(this.leftPanel),this.containerInner.appendChild(this.rightPanel),this.container.appendChild(this.containerInner),this.container.appendChild(this.dimensionInfo),this.appendChild(this.container);}async loadImages(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.img1||!this.img2||!this.dimensionInfo))try{this.img1.src=t,this.img2.src=i,await Promise.all([new Promise((h,l)=>{if(!this.img1)return l();this.img1.onload=h,this.img1.onerror=l;}),new Promise((h,l)=>{if(!this.img2)return l();this.img2.onload=h,this.img2.onerror=l;})]);let e=this.img1.naturalWidth,s=this.img1.naturalHeight,a=this.img2.naturalWidth,n=this.img2.naturalHeight;if(e!==a||s!==n){let h=a-e,l=n-s;this.dimensionInfo.textContent=`Dimensions changed: ${e}\xD7${s} \u2192 ${a}\xD7${n} (${h>0?"+":""}${h}\xD7${l>0?"+":""}${l})`,this.dimensionInfo.style.display="block";}else this.dimensionInfo.textContent=`${e}\xD7${s}`,this.dimensionInfo.style.display="block";this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:e,height:s},image2:{width:a,height:n}}}));}catch(e){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:e}}));}}};customElements.define("blazediff-twoup",p);exports.BaseElement=r;exports.DifferenceMode=m;exports.OnionSkinMode=d;exports.SwipeMode=g;exports.TwoUpMode=p;
1
+ 'use strict';var B=require('@blazediff/core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var B__default=/*#__PURE__*/_interopDefault(B);function W(s){return s.buffer instanceof ArrayBuffer?new Uint8Array(s.buffer,s.byteOffset,s.byteLength):Uint8Array.from(s)}function H(s){if(typeof s=="string")return {url:s,revoke(){}};let n=URL.createObjectURL(new Blob([W(s)]));return {url:n,revoke(){URL.revokeObjectURL(n);}}}function N(s,n){let e=n,t,i=c=>{let o=t;t=c===void 0?void 0:H(c),t?s.src=t.url:s.removeAttribute("src"),o?.revoke();};return i(n),{update(c){c!==e&&(e=c,i(c));},destroy(){t?.revoke(),t=void 0;}}}function b(s,n={}){let{crossOrigin:e="anonymous"}=n,t=H(s);return new Promise((i,c)=>{let o=new Image;e!==null&&(o.crossOrigin=e),o.onload=()=>i(o),o.onerror=c,o.src=t.url;}).finally(t.revoke)}function x(s){let n=document.createElement("canvas");n.width=s.naturalWidth,n.height=s.naturalHeight;let e=n.getContext("2d");if(!e)throw new Error("Could not get canvas context");return e.drawImage(s,0,0),e.getImageData(0,0,s.naturalWidth,s.naturalHeight)}function E(s){let n=s,e=new Set;return {get(){return n},set(t){n=typeof t=="function"?t(n):t;for(let i of e)i();},subscribe(t){return e.add(t),()=>{e.delete(t);}}}}var R=["src1","src2","threshold","includeAA","alpha","crossOrigin","enabled"];function A(s){let n=E({status:"idle"}),e={...s},t=0,i=false;async function c(){let o=++t,{src1:r,src2:m,threshold:d=.1,includeAA:f=false,alpha:l=.1,enabled:S=true}=e;if(!S||!r||!m){n.set({status:"idle"});return}n.set({status:"loading"});try{let[u,a]=await Promise.all([b(r,{crossOrigin:e.crossOrigin}),b(m,{crossOrigin:e.crossOrigin})]);if(i||o!==t)return;if(u.naturalWidth!==a.naturalWidth||u.naturalHeight!==a.naturalHeight)throw new Error(`Image dimensions do not match. Image 1: ${u.naturalWidth}x${u.naturalHeight}, Image 2: ${a.naturalWidth}x${a.naturalHeight}`);let v=u.naturalWidth,C=u.naturalHeight,h=x(u).data,w=x(a).data;if(i||o!==t)return;let y=new Uint8ClampedArray(v*C*4),L=B__default.default(h,w,y,v,C,{threshold:d,includeAA:f,alpha:l});if(i||o!==t)return;let I=v*C;n.set({status:"ready",diff:{output:y,width:v,height:C,diffCount:L,totalPixels:I,percentage:L/I*100}});}catch(u){if(i||o!==t)return;n.set({status:"error",error:u});}}return c(),{getState:n.get,subscribe:n.subscribe,setConfig(o){let r=R.some(m=>m in o&&o[m]!==e[m]);e={...e,...o},r&&c();},actions:{},destroy(){i=true,t++;}}}function p(s,n){s.className=n??"";}function g(s,n={}){let e=document.createElement(s);return n.className&&(e.className=n.className),n.style&&Object.assign(e.style,n.style),e}function z(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("canvas",{className:e.canvasClassName}),o=c.getContext("2d"),r=g("img",{className:e.canvasClassName});r.alt="";let m=N(r,e.diff),d=e.diff!==void 0;c.hidden=d,r.hidden=!d;let f=a=>{e.diff!==void 0&&e.onDiffError?.(a);};r.addEventListener("error",f),i.append(c,r),t.appendChild(i),s.appendChild(t);let l=A({src1:e.src1,src2:e.src2,threshold:e.threshold,includeAA:e.includeAA,alpha:e.alpha,crossOrigin:e.crossOrigin,enabled:!d}),S=null,u=l.subscribe(()=>{let a=l.getState();if(a.status==="ready"&&a.diff&&o){let{output:v,width:C,height:h}=a.diff;c.width=C,c.height=h,o.putImageData(new ImageData(v,C,h),0,0);}a.status!==S&&(S=a.status,a.status==="ready"&&a.diff?e.onDiffComplete?.({diffCount:a.diff.diffCount,totalPixels:a.diff.totalPixels,percentage:a.diff.percentage}):a.status==="error"&&e.onDiffError?.(a.error));});return {update(a){e={...e,...a},p(t,e.className),p(i,e.containerClassName),p(c,e.canvasClassName),p(r,e.canvasClassName);let v=e.diff!==void 0;c.hidden=v,r.hidden=!v,m.update(e.diff),l.setConfig({src1:e.src1,src2:e.src2,threshold:e.threshold,includeAA:e.includeAA,alpha:e.alpha,crossOrigin:e.crossOrigin,enabled:!v});},destroy(){u(),l.destroy(),r.removeEventListener("error",f),m.destroy(),t.remove();}}}var X=["src1","src2","crossOrigin"];function T(s){return s/100}function k(s,n=50){let e=E({status:"idle",opacity:Math.max(0,Math.min(100,n))}),t={...s},i=0,c=false;async function o(){let r=++i,{src1:m,src2:d}=t;if(!m||!d){e.set(f=>({...f,status:"idle"}));return}e.set(f=>({...f,status:"loading"}));try{let[f,l]=await Promise.all([b(m,{crossOrigin:t.crossOrigin}),b(d,{crossOrigin:t.crossOrigin})]);if(c||r!==i)return;let S={width:f.naturalWidth,height:f.naturalHeight},u={width:l.naturalWidth,height:l.naturalHeight};e.set(a=>({...a,status:"ready",dims1:S,dims2:u}));}catch(f){if(c||r!==i)return;e.set(l=>({...l,status:"error",error:f}));}}return o(),{getState:e.get,subscribe:e.subscribe,setConfig(r){let m=X.some(d=>d in r&&r[d]!==t[d]);t={...t,...r},m&&o();},actions:{setOpacity(r){e.set(m=>({...m,opacity:Math.max(0,Math.min(100,r))}));}},destroy(){c=true,i++;}}}function j(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("div",{className:e.imageContainerClassName,style:{position:"relative"}}),o=g("img",{className:e.imageClassName}),r=g("img",{className:e.imageClassName,style:{position:"absolute",top:"0",left:"0"}}),m=e.crossOrigin===void 0?"anonymous":e.crossOrigin;o.crossOrigin=m,r.crossOrigin=m,o.alt="",r.alt="";let d=N(o,e.src1),f=N(r,e.src2);c.append(o,r);let l=g("div",{className:e.sliderContainerClassName}),S=g("label",{className:e.sliderLabelClassName});S.textContent=e.sliderLabelText??"Opacity:";let u=g("input",{className:e.sliderClassName});u.type="range",u.min="0",u.max="100",l.append(S,u),i.append(c,l),t.appendChild(i),s.appendChild(t);let a=k({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin},e.opacity??50),v=null,C=a.getState().opacity;u.value=String(C),r.style.opacity=String(T(C));let h=()=>a.actions.setOpacity(Number(u.value));u.addEventListener("input",h);let w=a.subscribe(()=>{let y=a.getState();r.style.opacity=String(T(y.opacity)),u.value!==String(y.opacity)&&(u.value=String(y.opacity)),y.opacity!==C&&(C=y.opacity,e.onOpacityChange?.(y.opacity)),y.status!==v&&(v=y.status,y.status==="ready"&&y.dims1&&y.dims2?e.onImagesLoaded?.({image1:y.dims1,image2:y.dims2}):y.status==="error"&&e.onLoadError?.(y.error));});return {update(y){e={...e,...y},p(t,e.className),p(i,e.containerClassName),p(c,e.imageContainerClassName),p(o,e.imageClassName),p(r,e.imageClassName),p(l,e.sliderContainerClassName),p(S,e.sliderLabelClassName),p(u,e.sliderClassName),S.textContent=e.sliderLabelText??"Opacity:",d.update(e.src1),f.update(e.src2),e.opacity!==void 0&&e.opacity!==a.getState().opacity&&a.actions.setOpacity(e.opacity),a.setConfig({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin});},destroy(){w(),u.removeEventListener("input",h),a.destroy(),d.destroy(),f.destroy(),t.remove();}}}function U(s){return Math.max(0,Math.min(100,s))}function P(s=50){let n=E({position:U(s),isDragging:false});function e(t){n.set(i=>({...i,position:U(t)}));}return {getState:n.get,subscribe:n.subscribe,setConfig(){},actions:{start(t){n.set({position:U(t),isDragging:true});},move(t){n.get().isDragging&&e(t);},end(){n.set(t=>({...t,isDragging:false}));},setPosition:e},destroy(){}}}function K(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName,style:{position:"relative",overflow:"hidden",cursor:"ew-resize",userSelect:"none",WebkitUserSelect:"none"}}),c={width:"100%",height:"100%",objectFit:"contain",pointerEvents:"none"},o=g("img",{className:e.image1ClassName,style:{...c}}),r=N(o,e.src1);o.alt=e.alt1??"Before";let m=g("img",{className:e.image2ClassName,style:{...c,position:"absolute",top:"0",left:"0"}}),d=N(m,e.src2);m.alt=e.alt2??"After";let f=g("div",{className:e.dividerClassName,style:{position:"absolute",top:"0",bottom:"0",width:"2px",backgroundColor:"white",cursor:"ew-resize",zIndex:"10"}});i.append(o,m,f),t.appendChild(i),s.appendChild(t);let l=P(e.initialPosition??50),S=l.getState().position;function u(){let{position:O}=l.getState();m.style.clipPath=`inset(0 0 0 ${O}%)`,f.style.left=`${O}%`,O!==S&&(S=O,e.onPositionChange?.(O));}u();let a=l.subscribe(u),v=O=>{let M=i.getBoundingClientRect();return M.width===0?0:(O-M.left)/M.width*100},C=O=>l.actions.move(v(O.clientX)),h=()=>{l.actions.end(),document.removeEventListener("mousemove",C),document.removeEventListener("mouseup",h);},w=O=>{l.actions.start(v(O.clientX)),document.addEventListener("mousemove",C),document.addEventListener("mouseup",h);},y=O=>l.actions.move(v(O.touches[0].clientX)),D=()=>{l.actions.end(),document.removeEventListener("touchmove",y),document.removeEventListener("touchend",D);},L=O=>{l.actions.start(v(O.touches[0].clientX)),document.addEventListener("touchmove",y),document.addEventListener("touchend",D);};i.addEventListener("mousedown",w),i.addEventListener("touchstart",L);let I=()=>{document.removeEventListener("mousemove",C),document.removeEventListener("mouseup",h),document.removeEventListener("touchmove",y),document.removeEventListener("touchend",D);};return {update(O){e={...e,...O},p(t,e.className),p(i,e.containerClassName),p(o,e.image1ClassName),p(m,e.image2ClassName),p(f,e.dividerClassName),r.update(e.src1),d.update(e.src2),o.alt=e.alt1??"Before",m.alt=e.alt2??"After";},destroy(){a(),i.removeEventListener("mousedown",w),i.removeEventListener("touchstart",L),I(),r.destroy(),d.destroy(),t.remove();}}}var V=["src1","src2","crossOrigin"];function _(s,n){if(s.width===n.width&&s.height===n.height)return `${s.width}\xD7${s.height}`;let e=n.width-s.width,t=n.height-s.height,i=c=>c>0?"+":"";return `Dimensions changed: ${s.width}\xD7${s.height} \u2192 ${n.width}\xD7${n.height} (${i(e)}${e}\xD7${i(t)}${t})`}function $(s){let n=E({status:"idle"}),e={...s},t=0,i=false;async function c(){let o=++t,{src1:r,src2:m}=e;if(!r||!m){n.set({status:"idle"});return}n.set({status:"loading"});try{let[d,f]=await Promise.all([b(r,{crossOrigin:e.crossOrigin}),b(m,{crossOrigin:e.crossOrigin})]);if(i||o!==t)return;let l={width:d.naturalWidth,height:d.naturalHeight},S={width:f.naturalWidth,height:f.naturalHeight};n.set({status:"ready",dims1:l,dims2:S,dimensionLabel:_(l,S),changed:l.width!==S.width||l.height!==S.height});}catch(d){if(i||o!==t)return;n.set({status:"error",error:d});}}return c(),{getState:n.get,subscribe:n.subscribe,setConfig(o){let r=V.some(m=>m in o&&o[m]!==e[m]);e={...e,...o},r&&c();},actions:{},destroy(){i=true,t++;}}}function F(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("div",{className:e.containerInnerClassName,style:{display:"flex"}}),o=g("div",{className:e.panelClassName}),r=g("div",{className:e.panelClassName}),m=g("img",{className:e.imageClassName}),d=g("img",{className:e.imageClassName}),f=e.crossOrigin===void 0?"anonymous":e.crossOrigin;m.crossOrigin=f,d.crossOrigin=f,m.alt="",d.alt="";let l=N(m,e.src1),S=N(d,e.src2),u=g("div",{className:e.dimensionInfoClassName});o.appendChild(m),r.appendChild(d),c.append(o,r),i.append(c,u),t.appendChild(i),s.appendChild(t);let a=$({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin}),v=null,C=a.subscribe(()=>{let h=a.getState();h.status==="ready"&&h.dimensionLabel!==void 0&&(u.textContent=h.dimensionLabel),h.status!==v&&(v=h.status,h.status==="ready"&&h.dims1&&h.dims2?e.onImagesLoaded?.({image1:h.dims1,image2:h.dims2}):h.status==="error"&&e.onLoadError?.(h.error));});return {update(h){e={...e,...h},p(t,e.className),p(i,e.containerClassName),p(c,e.containerInnerClassName),p(o,e.panelClassName),p(r,e.panelClassName),p(m,e.imageClassName),p(d,e.imageClassName),p(u,e.dimensionInfoClassName),l.update(e.src1),S.update(e.src2),a.setConfig({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin});},destroy(){C(),a.destroy(),l.destroy(),S.destroy(),t.remove();}}}exports.mountDifference=z;exports.mountOnionSkin=j;exports.mountSwipe=K;exports.mountTwoUp=F;
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- import f from'@blazediff/core';var r=class extends HTMLElement{getClassName(o){return this.getAttribute(`class-${o}`)||""}applyClassName(o,t){let i=this.getClassName(t);i&&(o.className=i);}};var m=class extends r{constructor(){super(...arguments);this.container=null;this.canvas=null;this.ctx=null;}static get observedAttributes(){return ["src1","src2","threshold","include-aa","alpha","class-container","class-canvas"]}connectedCallback(){this.render(),this.loadAndCompare();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":case "threshold":case "include-aa":case "alpha":this.loadAndCompare();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-canvas":this.canvas&&this.applyClassName(this.canvas,"canvas");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.canvas=document.createElement("canvas"),this.applyClassName(this.canvas,"canvas"),this.ctx=this.canvas.getContext("2d"),this.container.appendChild(this.canvas),this.appendChild(this.container);}async loadAndCompare(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.canvas||!this.ctx))try{let[e,s]=await Promise.all([this.loadImage(t),this.loadImage(i)]);if(e.width!==s.width||e.height!==s.height)throw new Error(`Image dimensions do not match. Image 1: ${e.width}x${e.height}, Image 2: ${s.width}x${s.height}`);let a=e.width,n=e.height;this.canvas.width=a,this.canvas.height=n;let h=this.getImageData(e),l=this.getImageData(s),u=new Uint8ClampedArray(a*n*4),C={threshold:Number(this.getAttribute("threshold"))||.1,includeAA:this.getAttribute("include-aa")==="true",alpha:Number(this.getAttribute("alpha"))||.1},v=f(h.data,l.data,u,a,n,C),b=new ImageData(u,a,n);this.ctx.putImageData(b,0,0),this.dispatchEvent(new CustomEvent("diff-complete",{detail:{diffCount:v,totalPixels:a*n,percentage:v/(a*n)*100}}));}catch(e){this.dispatchEvent(new CustomEvent("diff-error",{detail:{error:e}}));}}loadImage(t){return new Promise((i,e)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>i(s),s.onerror=e,s.src=t;})}getImageData(t){let i=document.createElement("canvas");i.width=t.width,i.height=t.height;let e=i.getContext("2d");if(!e)throw new Error("Could not get canvas context");return e.drawImage(t,0,0),e.getImageData(0,0,t.width,t.height)}};customElements.define("blazediff-difference",m);var d=class extends r{constructor(){super(...arguments);this.container=null;this.imageContainer=null;this.img1=null;this.img2=null;this.sliderContainer=null;this.slider=null;this.sliderLabel=null;}static get observedAttributes(){return ["src1","src2","opacity","class-container","class-image-container","class-image","class-slider-container","class-slider","class-slider-label","text-slider-label"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":this.loadImages();break;case "opacity":this.updateOpacity();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image-container":this.imageContainer&&this.applyClassName(this.imageContainer,"image-container");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-slider-container":this.sliderContainer&&this.applyClassName(this.sliderContainer,"slider-container");break;case "class-slider":this.slider&&this.applyClassName(this.slider,"slider");break;case "class-slider-label":this.sliderLabel&&this.applyClassName(this.sliderLabel,"slider-label");break;case "text-slider-label":this.sliderLabel&&(this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity: ");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.imageContainer=document.createElement("div"),this.applyClassName(this.imageContainer,"image-container"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.img2.style.position="absolute",this.img2.style.top="0",this.img2.style.left="0",this.imageContainer.style.position="relative",this.imageContainer.appendChild(this.img1),this.imageContainer.appendChild(this.img2),this.sliderContainer=document.createElement("div"),this.applyClassName(this.sliderContainer,"slider-container"),this.sliderLabel=document.createElement("label"),this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity:",this.applyClassName(this.sliderLabel,"slider-label"),this.slider=document.createElement("input"),this.slider.type="range",this.slider.min="0",this.slider.max="100",this.slider.value=this.getAttribute("opacity")||"50",this.applyClassName(this.slider,"slider"),this.slider.addEventListener("input",t=>{let i=t.target.value;this.setAttribute("opacity",i),this.dispatchEvent(new CustomEvent("opacity-change",{detail:{opacity:Number(i)}}));}),this.sliderContainer.appendChild(this.sliderLabel),this.sliderContainer.appendChild(this.slider),this.container.appendChild(this.imageContainer),this.container.appendChild(this.sliderContainer),this.appendChild(this.container),this.updateOpacity();}updateOpacity(){if(!this.img2||!this.slider)return;let t=this.getAttribute("opacity")||"50";this.img2.style.opacity=(Number(t)/100).toString(),this.slider.value!==t&&(this.slider.value=t);}async loadImages(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.img1||!this.img2))try{this.img1.src=t,this.img2.src=i,await Promise.all([new Promise((e,s)=>{if(!this.img1)return s();this.img1.onload=e,this.img1.onerror=s;}),new Promise((e,s)=>{if(!this.img2)return s();this.img2.onload=e,this.img2.onerror=s;})]),this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:this.img1.naturalWidth,height:this.img1.naturalHeight},image2:{width:this.img2.naturalWidth,height:this.img2.naturalHeight}}}));}catch(e){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:e}}));}}};customElements.define("blazediff-onionskin",d);var g=class extends r{constructor(){super(...arguments);this.container=null;this.image1=null;this.image2=null;this.divider=null;this.isDragging=false;this.currentPosition=50;}static get observedAttributes(){return ["src1","src2","alt1","alt2","class-container","class-image1","class-image2","class-divider"]}connectedCallback(){this.render(),this.setupEventListeners();}disconnectedCallback(){this.removeEventListeners();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":this.image1&&(this.image1.src=e);break;case "src2":this.image2&&(this.image2.src=e);break;case "alt1":this.image1&&(this.image1.alt=e);break;case "alt2":this.image2&&(this.image2.alt=e);break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image1":this.image1&&this.applyClassName(this.image1,"image1");break;case "class-image2":this.image2&&this.applyClassName(this.image2,"image2");break;case "class-divider":this.divider&&this.applyClassName(this.divider,"divider");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.container.style.position="relative",this.container.style.overflow="hidden",this.container.style.cursor="ew-resize",this.container.style.webkitTapHighlightColor="transparent",this.container.style.webkitTouchCallout="none",this.container.style.webkitUserSelect="none",this.container.style.khtmlUserSelect="none",this.container.style.mozUserSelect="none",this.container.style.msUserSelect="none",this.container.style.userSelect="none",this.image1=document.createElement("img"),this.applyClassName(this.image1,"image1"),this.image1.src=this.getAttribute("src1")||"",this.image1.alt=this.getAttribute("alt1")||"Before",this.image1.style.width="100%",this.image1.style.height="100%",this.image1.style.objectFit="contain",this.image1.style.pointerEvents="none",this.image2=document.createElement("img"),this.applyClassName(this.image2,"image2"),this.image2.src=this.getAttribute("src2")||"",this.image2.alt=this.getAttribute("alt2")||"After",this.image2.style.position="absolute",this.image2.style.top="0",this.image2.style.left="0",this.image2.style.width="100%",this.image2.style.height="100%",this.image2.style.objectFit="contain",this.image2.style.clipPath=`inset(0 0 0 ${this.currentPosition}%)`,this.image2.style.pointerEvents="none",this.divider=document.createElement("div"),this.applyClassName(this.divider,"divider"),this.divider.style.position="absolute",this.divider.style.top="0",this.divider.style.bottom="0",this.divider.style.left=`${this.currentPosition}%`,this.divider.style.width="2px",this.divider.style.backgroundColor="white",this.divider.style.cursor="ew-resize",this.divider.style.zIndex="10",this.container.appendChild(this.image1),this.container.appendChild(this.image2),this.container.appendChild(this.divider),this.appendChild(this.container);}setupEventListeners(){this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.handleTouchStart=this.handleTouchStart.bind(this),this.handleTouchMove=this.handleTouchMove.bind(this),this.handleTouchEnd=this.handleTouchEnd.bind(this),this.container?.addEventListener("mousedown",this.handleMouseDown),this.container?.addEventListener("touchstart",this.handleTouchStart);}removeEventListeners(){this.container?.removeEventListener("mousedown",this.handleMouseDown),this.container?.removeEventListener("touchstart",this.handleTouchStart),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp),document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}handleMouseDown(t){this.isDragging=true,document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp),this.updatePosition(t.clientX);}handleMouseMove(t){this.isDragging&&this.updatePosition(t.clientX);}handleMouseUp(){this.isDragging=false,document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp);}handleTouchStart(t){this.isDragging=true,document.addEventListener("touchmove",this.handleTouchMove),document.addEventListener("touchend",this.handleTouchEnd);let i=t.touches[0];this.updatePosition(i.clientX);}handleTouchMove(t){if(!this.isDragging)return;let i=t.touches[0];this.updatePosition(i.clientX);}handleTouchEnd(){this.isDragging=false,document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}updatePosition(t){if(!this.container||!this.image2||!this.divider)return;let i=this.container.getBoundingClientRect(),e=t-i.left,s=Math.max(0,Math.min(100,e/i.width*100));this.currentPosition=s,this.image2.style.clipPath=`inset(0 0 0 ${s}%)`,this.divider.style.left=`${s}%`,this.dispatchEvent(new CustomEvent("position-change",{detail:{position:s}}));}};customElements.define("blazediff-swipe",g);var p=class extends r{constructor(){super(...arguments);this.container=null;this.containerInner=null;this.leftPanel=null;this.rightPanel=null;this.img1=null;this.img2=null;this.dimensionInfo=null;}static get observedAttributes(){return ["src1","src2","class-container","class-container-inner","class-panel","class-image","class-dimension-info"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(t,i,e){if(i!==e)switch(t){case "src1":case "src2":this.loadImages();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-container-inner":this.containerInner&&this.applyClassName(this.containerInner,"container-inner");break;case "class-panel":this.leftPanel&&this.applyClassName(this.leftPanel,"panel"),this.rightPanel&&this.applyClassName(this.rightPanel,"panel");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-dimension-info":this.dimensionInfo&&this.applyClassName(this.dimensionInfo,"dimension-info");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.containerInner=document.createElement("div"),this.applyClassName(this.containerInner,"container-inner"),this.leftPanel=document.createElement("div"),this.applyClassName(this.leftPanel,"panel"),this.rightPanel=document.createElement("div"),this.applyClassName(this.rightPanel,"panel"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.dimensionInfo=document.createElement("div"),this.applyClassName(this.dimensionInfo,"dimension-info"),this.leftPanel.appendChild(this.img1),this.rightPanel.appendChild(this.img2),this.containerInner.appendChild(this.leftPanel),this.containerInner.appendChild(this.rightPanel),this.container.appendChild(this.containerInner),this.container.appendChild(this.dimensionInfo),this.appendChild(this.container);}async loadImages(){let t=this.getAttribute("src1"),i=this.getAttribute("src2");if(!(!t||!i||!this.img1||!this.img2||!this.dimensionInfo))try{this.img1.src=t,this.img2.src=i,await Promise.all([new Promise((h,l)=>{if(!this.img1)return l();this.img1.onload=h,this.img1.onerror=l;}),new Promise((h,l)=>{if(!this.img2)return l();this.img2.onload=h,this.img2.onerror=l;})]);let e=this.img1.naturalWidth,s=this.img1.naturalHeight,a=this.img2.naturalWidth,n=this.img2.naturalHeight;if(e!==a||s!==n){let h=a-e,l=n-s;this.dimensionInfo.textContent=`Dimensions changed: ${e}\xD7${s} \u2192 ${a}\xD7${n} (${h>0?"+":""}${h}\xD7${l>0?"+":""}${l})`,this.dimensionInfo.style.display="block";}else this.dimensionInfo.textContent=`${e}\xD7${s}`,this.dimensionInfo.style.display="block";this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:e,height:s},image2:{width:a,height:n}}}));}catch(e){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:e}}));}}};customElements.define("blazediff-twoup",p);export{r as BaseElement,m as DifferenceMode,d as OnionSkinMode,g as SwipeMode,p as TwoUpMode};
1
+ import B from'@blazediff/core';function W(s){return s.buffer instanceof ArrayBuffer?new Uint8Array(s.buffer,s.byteOffset,s.byteLength):Uint8Array.from(s)}function H(s){if(typeof s=="string")return {url:s,revoke(){}};let n=URL.createObjectURL(new Blob([W(s)]));return {url:n,revoke(){URL.revokeObjectURL(n);}}}function N(s,n){let e=n,t,i=c=>{let o=t;t=c===void 0?void 0:H(c),t?s.src=t.url:s.removeAttribute("src"),o?.revoke();};return i(n),{update(c){c!==e&&(e=c,i(c));},destroy(){t?.revoke(),t=void 0;}}}function b(s,n={}){let{crossOrigin:e="anonymous"}=n,t=H(s);return new Promise((i,c)=>{let o=new Image;e!==null&&(o.crossOrigin=e),o.onload=()=>i(o),o.onerror=c,o.src=t.url;}).finally(t.revoke)}function x(s){let n=document.createElement("canvas");n.width=s.naturalWidth,n.height=s.naturalHeight;let e=n.getContext("2d");if(!e)throw new Error("Could not get canvas context");return e.drawImage(s,0,0),e.getImageData(0,0,s.naturalWidth,s.naturalHeight)}function E(s){let n=s,e=new Set;return {get(){return n},set(t){n=typeof t=="function"?t(n):t;for(let i of e)i();},subscribe(t){return e.add(t),()=>{e.delete(t);}}}}var R=["src1","src2","threshold","includeAA","alpha","crossOrigin","enabled"];function A(s){let n=E({status:"idle"}),e={...s},t=0,i=false;async function c(){let o=++t,{src1:r,src2:m,threshold:d=.1,includeAA:f=false,alpha:l=.1,enabled:S=true}=e;if(!S||!r||!m){n.set({status:"idle"});return}n.set({status:"loading"});try{let[u,a]=await Promise.all([b(r,{crossOrigin:e.crossOrigin}),b(m,{crossOrigin:e.crossOrigin})]);if(i||o!==t)return;if(u.naturalWidth!==a.naturalWidth||u.naturalHeight!==a.naturalHeight)throw new Error(`Image dimensions do not match. Image 1: ${u.naturalWidth}x${u.naturalHeight}, Image 2: ${a.naturalWidth}x${a.naturalHeight}`);let v=u.naturalWidth,C=u.naturalHeight,h=x(u).data,w=x(a).data;if(i||o!==t)return;let y=new Uint8ClampedArray(v*C*4),L=B(h,w,y,v,C,{threshold:d,includeAA:f,alpha:l});if(i||o!==t)return;let I=v*C;n.set({status:"ready",diff:{output:y,width:v,height:C,diffCount:L,totalPixels:I,percentage:L/I*100}});}catch(u){if(i||o!==t)return;n.set({status:"error",error:u});}}return c(),{getState:n.get,subscribe:n.subscribe,setConfig(o){let r=R.some(m=>m in o&&o[m]!==e[m]);e={...e,...o},r&&c();},actions:{},destroy(){i=true,t++;}}}function p(s,n){s.className=n??"";}function g(s,n={}){let e=document.createElement(s);return n.className&&(e.className=n.className),n.style&&Object.assign(e.style,n.style),e}function z(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("canvas",{className:e.canvasClassName}),o=c.getContext("2d"),r=g("img",{className:e.canvasClassName});r.alt="";let m=N(r,e.diff),d=e.diff!==void 0;c.hidden=d,r.hidden=!d;let f=a=>{e.diff!==void 0&&e.onDiffError?.(a);};r.addEventListener("error",f),i.append(c,r),t.appendChild(i),s.appendChild(t);let l=A({src1:e.src1,src2:e.src2,threshold:e.threshold,includeAA:e.includeAA,alpha:e.alpha,crossOrigin:e.crossOrigin,enabled:!d}),S=null,u=l.subscribe(()=>{let a=l.getState();if(a.status==="ready"&&a.diff&&o){let{output:v,width:C,height:h}=a.diff;c.width=C,c.height=h,o.putImageData(new ImageData(v,C,h),0,0);}a.status!==S&&(S=a.status,a.status==="ready"&&a.diff?e.onDiffComplete?.({diffCount:a.diff.diffCount,totalPixels:a.diff.totalPixels,percentage:a.diff.percentage}):a.status==="error"&&e.onDiffError?.(a.error));});return {update(a){e={...e,...a},p(t,e.className),p(i,e.containerClassName),p(c,e.canvasClassName),p(r,e.canvasClassName);let v=e.diff!==void 0;c.hidden=v,r.hidden=!v,m.update(e.diff),l.setConfig({src1:e.src1,src2:e.src2,threshold:e.threshold,includeAA:e.includeAA,alpha:e.alpha,crossOrigin:e.crossOrigin,enabled:!v});},destroy(){u(),l.destroy(),r.removeEventListener("error",f),m.destroy(),t.remove();}}}var X=["src1","src2","crossOrigin"];function T(s){return s/100}function k(s,n=50){let e=E({status:"idle",opacity:Math.max(0,Math.min(100,n))}),t={...s},i=0,c=false;async function o(){let r=++i,{src1:m,src2:d}=t;if(!m||!d){e.set(f=>({...f,status:"idle"}));return}e.set(f=>({...f,status:"loading"}));try{let[f,l]=await Promise.all([b(m,{crossOrigin:t.crossOrigin}),b(d,{crossOrigin:t.crossOrigin})]);if(c||r!==i)return;let S={width:f.naturalWidth,height:f.naturalHeight},u={width:l.naturalWidth,height:l.naturalHeight};e.set(a=>({...a,status:"ready",dims1:S,dims2:u}));}catch(f){if(c||r!==i)return;e.set(l=>({...l,status:"error",error:f}));}}return o(),{getState:e.get,subscribe:e.subscribe,setConfig(r){let m=X.some(d=>d in r&&r[d]!==t[d]);t={...t,...r},m&&o();},actions:{setOpacity(r){e.set(m=>({...m,opacity:Math.max(0,Math.min(100,r))}));}},destroy(){c=true,i++;}}}function j(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("div",{className:e.imageContainerClassName,style:{position:"relative"}}),o=g("img",{className:e.imageClassName}),r=g("img",{className:e.imageClassName,style:{position:"absolute",top:"0",left:"0"}}),m=e.crossOrigin===void 0?"anonymous":e.crossOrigin;o.crossOrigin=m,r.crossOrigin=m,o.alt="",r.alt="";let d=N(o,e.src1),f=N(r,e.src2);c.append(o,r);let l=g("div",{className:e.sliderContainerClassName}),S=g("label",{className:e.sliderLabelClassName});S.textContent=e.sliderLabelText??"Opacity:";let u=g("input",{className:e.sliderClassName});u.type="range",u.min="0",u.max="100",l.append(S,u),i.append(c,l),t.appendChild(i),s.appendChild(t);let a=k({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin},e.opacity??50),v=null,C=a.getState().opacity;u.value=String(C),r.style.opacity=String(T(C));let h=()=>a.actions.setOpacity(Number(u.value));u.addEventListener("input",h);let w=a.subscribe(()=>{let y=a.getState();r.style.opacity=String(T(y.opacity)),u.value!==String(y.opacity)&&(u.value=String(y.opacity)),y.opacity!==C&&(C=y.opacity,e.onOpacityChange?.(y.opacity)),y.status!==v&&(v=y.status,y.status==="ready"&&y.dims1&&y.dims2?e.onImagesLoaded?.({image1:y.dims1,image2:y.dims2}):y.status==="error"&&e.onLoadError?.(y.error));});return {update(y){e={...e,...y},p(t,e.className),p(i,e.containerClassName),p(c,e.imageContainerClassName),p(o,e.imageClassName),p(r,e.imageClassName),p(l,e.sliderContainerClassName),p(S,e.sliderLabelClassName),p(u,e.sliderClassName),S.textContent=e.sliderLabelText??"Opacity:",d.update(e.src1),f.update(e.src2),e.opacity!==void 0&&e.opacity!==a.getState().opacity&&a.actions.setOpacity(e.opacity),a.setConfig({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin});},destroy(){w(),u.removeEventListener("input",h),a.destroy(),d.destroy(),f.destroy(),t.remove();}}}function U(s){return Math.max(0,Math.min(100,s))}function P(s=50){let n=E({position:U(s),isDragging:false});function e(t){n.set(i=>({...i,position:U(t)}));}return {getState:n.get,subscribe:n.subscribe,setConfig(){},actions:{start(t){n.set({position:U(t),isDragging:true});},move(t){n.get().isDragging&&e(t);},end(){n.set(t=>({...t,isDragging:false}));},setPosition:e},destroy(){}}}function K(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName,style:{position:"relative",overflow:"hidden",cursor:"ew-resize",userSelect:"none",WebkitUserSelect:"none"}}),c={width:"100%",height:"100%",objectFit:"contain",pointerEvents:"none"},o=g("img",{className:e.image1ClassName,style:{...c}}),r=N(o,e.src1);o.alt=e.alt1??"Before";let m=g("img",{className:e.image2ClassName,style:{...c,position:"absolute",top:"0",left:"0"}}),d=N(m,e.src2);m.alt=e.alt2??"After";let f=g("div",{className:e.dividerClassName,style:{position:"absolute",top:"0",bottom:"0",width:"2px",backgroundColor:"white",cursor:"ew-resize",zIndex:"10"}});i.append(o,m,f),t.appendChild(i),s.appendChild(t);let l=P(e.initialPosition??50),S=l.getState().position;function u(){let{position:O}=l.getState();m.style.clipPath=`inset(0 0 0 ${O}%)`,f.style.left=`${O}%`,O!==S&&(S=O,e.onPositionChange?.(O));}u();let a=l.subscribe(u),v=O=>{let M=i.getBoundingClientRect();return M.width===0?0:(O-M.left)/M.width*100},C=O=>l.actions.move(v(O.clientX)),h=()=>{l.actions.end(),document.removeEventListener("mousemove",C),document.removeEventListener("mouseup",h);},w=O=>{l.actions.start(v(O.clientX)),document.addEventListener("mousemove",C),document.addEventListener("mouseup",h);},y=O=>l.actions.move(v(O.touches[0].clientX)),D=()=>{l.actions.end(),document.removeEventListener("touchmove",y),document.removeEventListener("touchend",D);},L=O=>{l.actions.start(v(O.touches[0].clientX)),document.addEventListener("touchmove",y),document.addEventListener("touchend",D);};i.addEventListener("mousedown",w),i.addEventListener("touchstart",L);let I=()=>{document.removeEventListener("mousemove",C),document.removeEventListener("mouseup",h),document.removeEventListener("touchmove",y),document.removeEventListener("touchend",D);};return {update(O){e={...e,...O},p(t,e.className),p(i,e.containerClassName),p(o,e.image1ClassName),p(m,e.image2ClassName),p(f,e.dividerClassName),r.update(e.src1),d.update(e.src2),o.alt=e.alt1??"Before",m.alt=e.alt2??"After";},destroy(){a(),i.removeEventListener("mousedown",w),i.removeEventListener("touchstart",L),I(),r.destroy(),d.destroy(),t.remove();}}}var V=["src1","src2","crossOrigin"];function _(s,n){if(s.width===n.width&&s.height===n.height)return `${s.width}\xD7${s.height}`;let e=n.width-s.width,t=n.height-s.height,i=c=>c>0?"+":"";return `Dimensions changed: ${s.width}\xD7${s.height} \u2192 ${n.width}\xD7${n.height} (${i(e)}${e}\xD7${i(t)}${t})`}function $(s){let n=E({status:"idle"}),e={...s},t=0,i=false;async function c(){let o=++t,{src1:r,src2:m}=e;if(!r||!m){n.set({status:"idle"});return}n.set({status:"loading"});try{let[d,f]=await Promise.all([b(r,{crossOrigin:e.crossOrigin}),b(m,{crossOrigin:e.crossOrigin})]);if(i||o!==t)return;let l={width:d.naturalWidth,height:d.naturalHeight},S={width:f.naturalWidth,height:f.naturalHeight};n.set({status:"ready",dims1:l,dims2:S,dimensionLabel:_(l,S),changed:l.width!==S.width||l.height!==S.height});}catch(d){if(i||o!==t)return;n.set({status:"error",error:d});}}return c(),{getState:n.get,subscribe:n.subscribe,setConfig(o){let r=V.some(m=>m in o&&o[m]!==e[m]);e={...e,...o},r&&c();},actions:{},destroy(){i=true,t++;}}}function F(s,n){let e=n,t=g("div",{className:e.className}),i=g("div",{className:e.containerClassName}),c=g("div",{className:e.containerInnerClassName,style:{display:"flex"}}),o=g("div",{className:e.panelClassName}),r=g("div",{className:e.panelClassName}),m=g("img",{className:e.imageClassName}),d=g("img",{className:e.imageClassName}),f=e.crossOrigin===void 0?"anonymous":e.crossOrigin;m.crossOrigin=f,d.crossOrigin=f,m.alt="",d.alt="";let l=N(m,e.src1),S=N(d,e.src2),u=g("div",{className:e.dimensionInfoClassName});o.appendChild(m),r.appendChild(d),c.append(o,r),i.append(c,u),t.appendChild(i),s.appendChild(t);let a=$({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin}),v=null,C=a.subscribe(()=>{let h=a.getState();h.status==="ready"&&h.dimensionLabel!==void 0&&(u.textContent=h.dimensionLabel),h.status!==v&&(v=h.status,h.status==="ready"&&h.dims1&&h.dims2?e.onImagesLoaded?.({image1:h.dims1,image2:h.dims2}):h.status==="error"&&e.onLoadError?.(h.error));});return {update(h){e={...e,...h},p(t,e.className),p(i,e.containerClassName),p(c,e.containerInnerClassName),p(o,e.panelClassName),p(r,e.panelClassName),p(m,e.imageClassName),p(d,e.imageClassName),p(u,e.dimensionInfoClassName),l.update(e.src1),S.update(e.src2),a.setConfig({src1:e.src1,src2:e.src2,crossOrigin:e.crossOrigin});},destroy(){C(),a.destroy(),l.destroy(),S.destroy(),t.remove();}}}export{z as mountDifference,j as mountOnionSkin,K as mountSwipe,F as mountTwoUp};
@@ -0,0 +1,88 @@
1
+ type ImageSource = string | Uint8Array;
2
+ type Status = "idle" | "loading" | "ready" | "error";
3
+ interface Dimensions {
4
+ width: number;
5
+ height: number;
6
+ }
7
+ /**
8
+ * Shared shape every mode controller implements. The engine owns all state,
9
+ * calculations, and async work; it never touches the display surface.
10
+ */
11
+ interface Engine<S, C, A> {
12
+ getState(): S;
13
+ subscribe(listener: () => void): () => void;
14
+ /** Shallow-diffs against the current config; reloads only when a load-relevant field changes. */
15
+ setConfig(config: Partial<C>): void;
16
+ actions: A;
17
+ /** Aborts in-flight loads and stops all further state mutations. */
18
+ destroy(): void;
19
+ }
20
+ interface DiffStats {
21
+ output: Uint8ClampedArray;
22
+ width: number;
23
+ height: number;
24
+ diffCount: number;
25
+ totalPixels: number;
26
+ percentage: number;
27
+ }
28
+ interface DifferenceState {
29
+ status: Status;
30
+ diff?: DiffStats;
31
+ error?: unknown;
32
+ }
33
+ interface DifferenceConfig {
34
+ src1: ImageSource;
35
+ src2: ImageSource;
36
+ threshold?: number;
37
+ includeAA?: boolean;
38
+ alpha?: number;
39
+ crossOrigin?: string | null;
40
+ enabled?: boolean;
41
+ }
42
+ type DifferenceEngine = Engine<DifferenceState, DifferenceConfig, Record<string, never>>;
43
+ interface SwipeState {
44
+ position: number;
45
+ isDragging: boolean;
46
+ }
47
+ interface SwipeActions {
48
+ /** `position` is the already-computed 0–100 percentage. */
49
+ start(position: number): void;
50
+ /** No-op unless currently dragging. */
51
+ move(position: number): void;
52
+ end(): void;
53
+ setPosition(position: number): void;
54
+ }
55
+ type SwipeEngine = Engine<SwipeState, Record<string, never>, SwipeActions>;
56
+ interface TwoUpState {
57
+ status: Status;
58
+ dims1?: Dimensions;
59
+ dims2?: Dimensions;
60
+ dimensionLabel?: string;
61
+ changed?: boolean;
62
+ error?: unknown;
63
+ }
64
+ interface TwoUpConfig {
65
+ src1: ImageSource;
66
+ src2: ImageSource;
67
+ crossOrigin?: string | null;
68
+ }
69
+ type TwoUpEngine = Engine<TwoUpState, TwoUpConfig, Record<string, never>>;
70
+ interface OnionSkinState {
71
+ status: Status;
72
+ opacity: number;
73
+ dims1?: Dimensions;
74
+ dims2?: Dimensions;
75
+ error?: unknown;
76
+ }
77
+ interface OnionSkinConfig {
78
+ src1: ImageSource;
79
+ src2: ImageSource;
80
+ crossOrigin?: string | null;
81
+ }
82
+ interface OnionSkinActions {
83
+ /** Clamps to 0–100. */
84
+ setOpacity(value: number): void;
85
+ }
86
+ type OnionSkinEngine = Engine<OnionSkinState, OnionSkinConfig, OnionSkinActions>;
87
+
88
+ export type { Dimensions as D, Engine as E, ImageSource as I, OnionSkinConfig as O, SwipeEngine as S, TwoUpConfig as T, DiffStats as a, DifferenceConfig as b, DifferenceEngine as c, OnionSkinEngine as d, TwoUpEngine as e, DifferenceState as f, OnionSkinActions as g, OnionSkinState as h, Status as i, SwipeActions as j, SwipeState as k, TwoUpState as l };
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@blazediff/ui",
3
- "version": "1.6.6",
4
- "description": "Unstyled web components for displaying image differences",
3
+ "version": "2.1.0",
4
+ "description": "Headless engine and framework-agnostic renderer for image-diff UIs",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "sideEffects": false,
9
10
  "main": "dist/index.js",
10
11
  "module": "dist/index.mjs",
11
12
  "types": "dist/index.d.ts",
@@ -15,30 +16,10 @@
15
16
  "import": "./dist/index.mjs",
16
17
  "require": "./dist/index.js"
17
18
  },
18
- "./difference-mode": {
19
- "types": "./dist/difference-mode.d.ts",
20
- "import": "./dist/difference-mode.mjs",
21
- "require": "./dist/difference-mode.js"
22
- },
23
- "./swipe-mode": {
24
- "types": "./dist/swipe-mode.d.ts",
25
- "import": "./dist/swipe-mode.mjs",
26
- "require": "./dist/swipe-mode.js"
27
- },
28
- "./two-up-mode": {
29
- "types": "./dist/two-up-mode.d.ts",
30
- "import": "./dist/two-up-mode.mjs",
31
- "require": "./dist/two-up-mode.js"
32
- },
33
- "./onion-skin-mode": {
34
- "types": "./dist/onion-skin-mode.d.ts",
35
- "import": "./dist/onion-skin-mode.mjs",
36
- "require": "./dist/onion-skin-mode.js"
37
- },
38
- "./base-element": {
39
- "types": "./dist/base-element.d.ts",
40
- "import": "./dist/base-element.mjs",
41
- "require": "./dist/base-element.js"
19
+ "./engine": {
20
+ "types": "./dist/engine.d.ts",
21
+ "import": "./dist/engine.mjs",
22
+ "require": "./dist/engine.js"
42
23
  }
43
24
  },
44
25
  "files": [
@@ -48,7 +29,7 @@
48
29
  "image",
49
30
  "comparison",
50
31
  "diff",
51
- "web-components",
32
+ "headless",
52
33
  "ui",
53
34
  "visual-testing"
54
35
  ],
@@ -57,13 +38,12 @@
57
38
  "homepage": "https://blazediff.dev",
58
39
  "license": "MIT",
59
40
  "dependencies": {
60
- "@blazediff/core": "1.9.3"
41
+ "@blazediff/core": "1.10.0"
61
42
  },
62
43
  "devDependencies": {
63
- "@storybook/addon-essentials": "9.0.0-alpha.12",
64
44
  "@storybook/addon-links": "^9.1.8",
65
- "@storybook/web-components": "^9.1.8",
66
- "@storybook/web-components-vite": "^9.1.8",
45
+ "@storybook/html": "9.1.8",
46
+ "@storybook/html-vite": "9.1.8",
67
47
  "@tailwindcss/vite": "^4.1.13",
68
48
  "@types/node": "^24.3.0",
69
49
  "storybook": "^9.1.8",
@@ -1,6 +0,0 @@
1
- declare abstract class BaseElement extends HTMLElement {
2
- protected getClassName(name: string): string;
3
- protected applyClassName(element: HTMLElement, name: string): void;
4
- }
5
-
6
- export { BaseElement };
@@ -1 +0,0 @@
1
- 'use strict';var e=class extends HTMLElement{getClassName(t){return this.getAttribute(`class-${t}`)||""}applyClassName(t,a){let s=this.getClassName(a);s&&(t.className=s);}};exports.BaseElement=e;
@@ -1 +0,0 @@
1
- var e=class extends HTMLElement{getClassName(t){return this.getAttribute(`class-${t}`)||""}applyClassName(t,a){let s=this.getClassName(a);s&&(t.className=s);}};export{e as BaseElement};
@@ -1,16 +0,0 @@
1
- import { BaseElement } from './base-element.js';
2
-
3
- declare class DifferenceMode extends BaseElement {
4
- private container;
5
- private canvas;
6
- private ctx;
7
- static get observedAttributes(): string[];
8
- connectedCallback(): void;
9
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
10
- private render;
11
- private loadAndCompare;
12
- private loadImage;
13
- private getImageData;
14
- }
15
-
16
- export { DifferenceMode };
@@ -1 +0,0 @@
1
- 'use strict';var v=require('@blazediff/core');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var v__default=/*#__PURE__*/_interopDefault(v);var c=class extends HTMLElement{getClassName(r){return this.getAttribute(`class-${r}`)||""}applyClassName(r,e){let a=this.getClassName(e);a&&(r.className=a);}};var h=class extends c{constructor(){super(...arguments);this.container=null;this.canvas=null;this.ctx=null;}static get observedAttributes(){return ["src1","src2","threshold","include-aa","alpha","class-container","class-canvas"]}connectedCallback(){this.render(),this.loadAndCompare();}attributeChangedCallback(e,a,t){if(a!==t)switch(e){case "src1":case "src2":case "threshold":case "include-aa":case "alpha":this.loadAndCompare();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-canvas":this.canvas&&this.applyClassName(this.canvas,"canvas");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.canvas=document.createElement("canvas"),this.applyClassName(this.canvas,"canvas"),this.ctx=this.canvas.getContext("2d"),this.container.appendChild(this.canvas),this.appendChild(this.container);}async loadAndCompare(){let e=this.getAttribute("src1"),a=this.getAttribute("src2");if(!(!e||!a||!this.canvas||!this.ctx))try{let[t,s]=await Promise.all([this.loadImage(e),this.loadImage(a)]);if(t.width!==s.width||t.height!==s.height)throw new Error(`Image dimensions do not match. Image 1: ${t.width}x${t.height}, Image 2: ${s.width}x${s.height}`);let n=t.width,i=t.height;this.canvas.width=n,this.canvas.height=i;let m=this.getImageData(t),g=this.getImageData(s),o=new Uint8ClampedArray(n*i*4),p={threshold:Number(this.getAttribute("threshold"))||.1,includeAA:this.getAttribute("include-aa")==="true",alpha:Number(this.getAttribute("alpha"))||.1},l=v__default.default(m.data,g.data,o,n,i,p),u=new ImageData(o,n,i);this.ctx.putImageData(u,0,0),this.dispatchEvent(new CustomEvent("diff-complete",{detail:{diffCount:l,totalPixels:n*i,percentage:l/(n*i)*100}}));}catch(t){this.dispatchEvent(new CustomEvent("diff-error",{detail:{error:t}}));}}loadImage(e){return new Promise((a,t)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>a(s),s.onerror=t,s.src=e;})}getImageData(e){let a=document.createElement("canvas");a.width=e.width,a.height=e.height;let t=a.getContext("2d");if(!t)throw new Error("Could not get canvas context");return t.drawImage(e,0,0),t.getImageData(0,0,e.width,e.height)}};customElements.define("blazediff-difference",h);exports.DifferenceMode=h;
@@ -1 +0,0 @@
1
- import v from'@blazediff/core';var c=class extends HTMLElement{getClassName(r){return this.getAttribute(`class-${r}`)||""}applyClassName(r,e){let a=this.getClassName(e);a&&(r.className=a);}};var h=class extends c{constructor(){super(...arguments);this.container=null;this.canvas=null;this.ctx=null;}static get observedAttributes(){return ["src1","src2","threshold","include-aa","alpha","class-container","class-canvas"]}connectedCallback(){this.render(),this.loadAndCompare();}attributeChangedCallback(e,a,t){if(a!==t)switch(e){case "src1":case "src2":case "threshold":case "include-aa":case "alpha":this.loadAndCompare();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-canvas":this.canvas&&this.applyClassName(this.canvas,"canvas");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.canvas=document.createElement("canvas"),this.applyClassName(this.canvas,"canvas"),this.ctx=this.canvas.getContext("2d"),this.container.appendChild(this.canvas),this.appendChild(this.container);}async loadAndCompare(){let e=this.getAttribute("src1"),a=this.getAttribute("src2");if(!(!e||!a||!this.canvas||!this.ctx))try{let[t,s]=await Promise.all([this.loadImage(e),this.loadImage(a)]);if(t.width!==s.width||t.height!==s.height)throw new Error(`Image dimensions do not match. Image 1: ${t.width}x${t.height}, Image 2: ${s.width}x${s.height}`);let n=t.width,i=t.height;this.canvas.width=n,this.canvas.height=i;let m=this.getImageData(t),g=this.getImageData(s),o=new Uint8ClampedArray(n*i*4),p={threshold:Number(this.getAttribute("threshold"))||.1,includeAA:this.getAttribute("include-aa")==="true",alpha:Number(this.getAttribute("alpha"))||.1},l=v(m.data,g.data,o,n,i,p),u=new ImageData(o,n,i);this.ctx.putImageData(u,0,0),this.dispatchEvent(new CustomEvent("diff-complete",{detail:{diffCount:l,totalPixels:n*i,percentage:l/(n*i)*100}}));}catch(t){this.dispatchEvent(new CustomEvent("diff-error",{detail:{error:t}}));}}loadImage(e){return new Promise((a,t)=>{let s=new Image;s.crossOrigin="anonymous",s.onload=()=>a(s),s.onerror=t,s.src=e;})}getImageData(e){let a=document.createElement("canvas");a.width=e.width,a.height=e.height;let t=a.getContext("2d");if(!t)throw new Error("Could not get canvas context");return t.drawImage(e,0,0),t.getImageData(0,0,e.width,e.height)}};customElements.define("blazediff-difference",h);export{h as DifferenceMode};
@@ -1,19 +0,0 @@
1
- import { BaseElement } from './base-element.js';
2
-
3
- declare class OnionSkinMode extends BaseElement {
4
- private container;
5
- private imageContainer;
6
- private img1;
7
- private img2;
8
- private sliderContainer;
9
- private slider;
10
- private sliderLabel;
11
- static get observedAttributes(): string[];
12
- connectedCallback(): void;
13
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
14
- private render;
15
- private updateOpacity;
16
- private loadImages;
17
- }
18
-
19
- export { OnionSkinMode };
@@ -1 +0,0 @@
1
- 'use strict';var l=class extends HTMLElement{getClassName(a){return this.getAttribute(`class-${a}`)||""}applyClassName(a,i){let e=this.getClassName(i);e&&(a.className=e);}};var r=class extends l{constructor(){super(...arguments);this.container=null;this.imageContainer=null;this.img1=null;this.img2=null;this.sliderContainer=null;this.slider=null;this.sliderLabel=null;}static get observedAttributes(){return ["src1","src2","opacity","class-container","class-image-container","class-image","class-slider-container","class-slider","class-slider-label","text-slider-label"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(i,e,t){if(e!==t)switch(i){case "src1":case "src2":this.loadImages();break;case "opacity":this.updateOpacity();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image-container":this.imageContainer&&this.applyClassName(this.imageContainer,"image-container");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-slider-container":this.sliderContainer&&this.applyClassName(this.sliderContainer,"slider-container");break;case "class-slider":this.slider&&this.applyClassName(this.slider,"slider");break;case "class-slider-label":this.sliderLabel&&this.applyClassName(this.sliderLabel,"slider-label");break;case "text-slider-label":this.sliderLabel&&(this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity: ");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.imageContainer=document.createElement("div"),this.applyClassName(this.imageContainer,"image-container"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.img2.style.position="absolute",this.img2.style.top="0",this.img2.style.left="0",this.imageContainer.style.position="relative",this.imageContainer.appendChild(this.img1),this.imageContainer.appendChild(this.img2),this.sliderContainer=document.createElement("div"),this.applyClassName(this.sliderContainer,"slider-container"),this.sliderLabel=document.createElement("label"),this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity:",this.applyClassName(this.sliderLabel,"slider-label"),this.slider=document.createElement("input"),this.slider.type="range",this.slider.min="0",this.slider.max="100",this.slider.value=this.getAttribute("opacity")||"50",this.applyClassName(this.slider,"slider"),this.slider.addEventListener("input",i=>{let e=i.target.value;this.setAttribute("opacity",e),this.dispatchEvent(new CustomEvent("opacity-change",{detail:{opacity:Number(e)}}));}),this.sliderContainer.appendChild(this.sliderLabel),this.sliderContainer.appendChild(this.slider),this.container.appendChild(this.imageContainer),this.container.appendChild(this.sliderContainer),this.appendChild(this.container),this.updateOpacity();}updateOpacity(){if(!this.img2||!this.slider)return;let i=this.getAttribute("opacity")||"50";this.img2.style.opacity=(Number(i)/100).toString(),this.slider.value!==i&&(this.slider.value=i);}async loadImages(){let i=this.getAttribute("src1"),e=this.getAttribute("src2");if(!(!i||!e||!this.img1||!this.img2))try{this.img1.src=i,this.img2.src=e,await Promise.all([new Promise((t,s)=>{if(!this.img1)return s();this.img1.onload=t,this.img1.onerror=s;}),new Promise((t,s)=>{if(!this.img2)return s();this.img2.onload=t,this.img2.onerror=s;})]),this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:this.img1.naturalWidth,height:this.img1.naturalHeight},image2:{width:this.img2.naturalWidth,height:this.img2.naturalHeight}}}));}catch(t){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:t}}));}}};customElements.define("blazediff-onionskin",r);exports.OnionSkinMode=r;
@@ -1 +0,0 @@
1
- var l=class extends HTMLElement{getClassName(a){return this.getAttribute(`class-${a}`)||""}applyClassName(a,i){let e=this.getClassName(i);e&&(a.className=e);}};var r=class extends l{constructor(){super(...arguments);this.container=null;this.imageContainer=null;this.img1=null;this.img2=null;this.sliderContainer=null;this.slider=null;this.sliderLabel=null;}static get observedAttributes(){return ["src1","src2","opacity","class-container","class-image-container","class-image","class-slider-container","class-slider","class-slider-label","text-slider-label"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(i,e,t){if(e!==t)switch(i){case "src1":case "src2":this.loadImages();break;case "opacity":this.updateOpacity();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image-container":this.imageContainer&&this.applyClassName(this.imageContainer,"image-container");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-slider-container":this.sliderContainer&&this.applyClassName(this.sliderContainer,"slider-container");break;case "class-slider":this.slider&&this.applyClassName(this.slider,"slider");break;case "class-slider-label":this.sliderLabel&&this.applyClassName(this.sliderLabel,"slider-label");break;case "text-slider-label":this.sliderLabel&&(this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity: ");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.imageContainer=document.createElement("div"),this.applyClassName(this.imageContainer,"image-container"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.img2.style.position="absolute",this.img2.style.top="0",this.img2.style.left="0",this.imageContainer.style.position="relative",this.imageContainer.appendChild(this.img1),this.imageContainer.appendChild(this.img2),this.sliderContainer=document.createElement("div"),this.applyClassName(this.sliderContainer,"slider-container"),this.sliderLabel=document.createElement("label"),this.sliderLabel.textContent=this.getAttribute("text-slider-label")||"Opacity:",this.applyClassName(this.sliderLabel,"slider-label"),this.slider=document.createElement("input"),this.slider.type="range",this.slider.min="0",this.slider.max="100",this.slider.value=this.getAttribute("opacity")||"50",this.applyClassName(this.slider,"slider"),this.slider.addEventListener("input",i=>{let e=i.target.value;this.setAttribute("opacity",e),this.dispatchEvent(new CustomEvent("opacity-change",{detail:{opacity:Number(e)}}));}),this.sliderContainer.appendChild(this.sliderLabel),this.sliderContainer.appendChild(this.slider),this.container.appendChild(this.imageContainer),this.container.appendChild(this.sliderContainer),this.appendChild(this.container),this.updateOpacity();}updateOpacity(){if(!this.img2||!this.slider)return;let i=this.getAttribute("opacity")||"50";this.img2.style.opacity=(Number(i)/100).toString(),this.slider.value!==i&&(this.slider.value=i);}async loadImages(){let i=this.getAttribute("src1"),e=this.getAttribute("src2");if(!(!i||!e||!this.img1||!this.img2))try{this.img1.src=i,this.img2.src=e,await Promise.all([new Promise((t,s)=>{if(!this.img1)return s();this.img1.onload=t,this.img1.onerror=s;}),new Promise((t,s)=>{if(!this.img2)return s();this.img2.onload=t,this.img2.onerror=s;})]),this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:this.img1.naturalWidth,height:this.img1.naturalHeight},image2:{width:this.img2.naturalWidth,height:this.img2.naturalHeight}}}));}catch(t){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:t}}));}}};customElements.define("blazediff-onionskin",r);export{r as OnionSkinMode};
@@ -1,26 +0,0 @@
1
- import { BaseElement } from './base-element.js';
2
-
3
- declare class SwipeMode extends BaseElement {
4
- private container;
5
- private image1;
6
- private image2;
7
- private divider;
8
- private isDragging;
9
- private currentPosition;
10
- static get observedAttributes(): string[];
11
- connectedCallback(): void;
12
- disconnectedCallback(): void;
13
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
14
- private render;
15
- private setupEventListeners;
16
- private removeEventListeners;
17
- private handleMouseDown;
18
- private handleMouseMove;
19
- private handleMouseUp;
20
- private handleTouchStart;
21
- private handleTouchMove;
22
- private handleTouchEnd;
23
- private updatePosition;
24
- }
25
-
26
- export { SwipeMode };
@@ -1 +0,0 @@
1
- 'use strict';var a=class extends HTMLElement{getClassName(s){return this.getAttribute(`class-${s}`)||""}applyClassName(s,e){let t=this.getClassName(e);t&&(s.className=t);}};var h=class extends a{constructor(){super(...arguments);this.container=null;this.image1=null;this.image2=null;this.divider=null;this.isDragging=false;this.currentPosition=50;}static get observedAttributes(){return ["src1","src2","alt1","alt2","class-container","class-image1","class-image2","class-divider"]}connectedCallback(){this.render(),this.setupEventListeners();}disconnectedCallback(){this.removeEventListeners();}attributeChangedCallback(e,t,i){if(t!==i)switch(e){case "src1":this.image1&&(this.image1.src=i);break;case "src2":this.image2&&(this.image2.src=i);break;case "alt1":this.image1&&(this.image1.alt=i);break;case "alt2":this.image2&&(this.image2.alt=i);break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image1":this.image1&&this.applyClassName(this.image1,"image1");break;case "class-image2":this.image2&&this.applyClassName(this.image2,"image2");break;case "class-divider":this.divider&&this.applyClassName(this.divider,"divider");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.container.style.position="relative",this.container.style.overflow="hidden",this.container.style.cursor="ew-resize",this.container.style.webkitTapHighlightColor="transparent",this.container.style.webkitTouchCallout="none",this.container.style.webkitUserSelect="none",this.container.style.khtmlUserSelect="none",this.container.style.mozUserSelect="none",this.container.style.msUserSelect="none",this.container.style.userSelect="none",this.image1=document.createElement("img"),this.applyClassName(this.image1,"image1"),this.image1.src=this.getAttribute("src1")||"",this.image1.alt=this.getAttribute("alt1")||"Before",this.image1.style.width="100%",this.image1.style.height="100%",this.image1.style.objectFit="contain",this.image1.style.pointerEvents="none",this.image2=document.createElement("img"),this.applyClassName(this.image2,"image2"),this.image2.src=this.getAttribute("src2")||"",this.image2.alt=this.getAttribute("alt2")||"After",this.image2.style.position="absolute",this.image2.style.top="0",this.image2.style.left="0",this.image2.style.width="100%",this.image2.style.height="100%",this.image2.style.objectFit="contain",this.image2.style.clipPath=`inset(0 0 0 ${this.currentPosition}%)`,this.image2.style.pointerEvents="none",this.divider=document.createElement("div"),this.applyClassName(this.divider,"divider"),this.divider.style.position="absolute",this.divider.style.top="0",this.divider.style.bottom="0",this.divider.style.left=`${this.currentPosition}%`,this.divider.style.width="2px",this.divider.style.backgroundColor="white",this.divider.style.cursor="ew-resize",this.divider.style.zIndex="10",this.container.appendChild(this.image1),this.container.appendChild(this.image2),this.container.appendChild(this.divider),this.appendChild(this.container);}setupEventListeners(){this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.handleTouchStart=this.handleTouchStart.bind(this),this.handleTouchMove=this.handleTouchMove.bind(this),this.handleTouchEnd=this.handleTouchEnd.bind(this),this.container?.addEventListener("mousedown",this.handleMouseDown),this.container?.addEventListener("touchstart",this.handleTouchStart);}removeEventListeners(){this.container?.removeEventListener("mousedown",this.handleMouseDown),this.container?.removeEventListener("touchstart",this.handleTouchStart),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp),document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}handleMouseDown(e){this.isDragging=true,document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp),this.updatePosition(e.clientX);}handleMouseMove(e){this.isDragging&&this.updatePosition(e.clientX);}handleMouseUp(){this.isDragging=false,document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp);}handleTouchStart(e){this.isDragging=true,document.addEventListener("touchmove",this.handleTouchMove),document.addEventListener("touchend",this.handleTouchEnd);let t=e.touches[0];this.updatePosition(t.clientX);}handleTouchMove(e){if(!this.isDragging)return;let t=e.touches[0];this.updatePosition(t.clientX);}handleTouchEnd(){this.isDragging=false,document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}updatePosition(e){if(!this.container||!this.image2||!this.divider)return;let t=this.container.getBoundingClientRect(),i=e-t.left,n=Math.max(0,Math.min(100,i/t.width*100));this.currentPosition=n,this.image2.style.clipPath=`inset(0 0 0 ${n}%)`,this.divider.style.left=`${n}%`,this.dispatchEvent(new CustomEvent("position-change",{detail:{position:n}}));}};customElements.define("blazediff-swipe",h);exports.SwipeMode=h;
@@ -1 +0,0 @@
1
- var a=class extends HTMLElement{getClassName(s){return this.getAttribute(`class-${s}`)||""}applyClassName(s,e){let t=this.getClassName(e);t&&(s.className=t);}};var h=class extends a{constructor(){super(...arguments);this.container=null;this.image1=null;this.image2=null;this.divider=null;this.isDragging=false;this.currentPosition=50;}static get observedAttributes(){return ["src1","src2","alt1","alt2","class-container","class-image1","class-image2","class-divider"]}connectedCallback(){this.render(),this.setupEventListeners();}disconnectedCallback(){this.removeEventListeners();}attributeChangedCallback(e,t,i){if(t!==i)switch(e){case "src1":this.image1&&(this.image1.src=i);break;case "src2":this.image2&&(this.image2.src=i);break;case "alt1":this.image1&&(this.image1.alt=i);break;case "alt2":this.image2&&(this.image2.alt=i);break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-image1":this.image1&&this.applyClassName(this.image1,"image1");break;case "class-image2":this.image2&&this.applyClassName(this.image2,"image2");break;case "class-divider":this.divider&&this.applyClassName(this.divider,"divider");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.container.style.position="relative",this.container.style.overflow="hidden",this.container.style.cursor="ew-resize",this.container.style.webkitTapHighlightColor="transparent",this.container.style.webkitTouchCallout="none",this.container.style.webkitUserSelect="none",this.container.style.khtmlUserSelect="none",this.container.style.mozUserSelect="none",this.container.style.msUserSelect="none",this.container.style.userSelect="none",this.image1=document.createElement("img"),this.applyClassName(this.image1,"image1"),this.image1.src=this.getAttribute("src1")||"",this.image1.alt=this.getAttribute("alt1")||"Before",this.image1.style.width="100%",this.image1.style.height="100%",this.image1.style.objectFit="contain",this.image1.style.pointerEvents="none",this.image2=document.createElement("img"),this.applyClassName(this.image2,"image2"),this.image2.src=this.getAttribute("src2")||"",this.image2.alt=this.getAttribute("alt2")||"After",this.image2.style.position="absolute",this.image2.style.top="0",this.image2.style.left="0",this.image2.style.width="100%",this.image2.style.height="100%",this.image2.style.objectFit="contain",this.image2.style.clipPath=`inset(0 0 0 ${this.currentPosition}%)`,this.image2.style.pointerEvents="none",this.divider=document.createElement("div"),this.applyClassName(this.divider,"divider"),this.divider.style.position="absolute",this.divider.style.top="0",this.divider.style.bottom="0",this.divider.style.left=`${this.currentPosition}%`,this.divider.style.width="2px",this.divider.style.backgroundColor="white",this.divider.style.cursor="ew-resize",this.divider.style.zIndex="10",this.container.appendChild(this.image1),this.container.appendChild(this.image2),this.container.appendChild(this.divider),this.appendChild(this.container);}setupEventListeners(){this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.handleTouchStart=this.handleTouchStart.bind(this),this.handleTouchMove=this.handleTouchMove.bind(this),this.handleTouchEnd=this.handleTouchEnd.bind(this),this.container?.addEventListener("mousedown",this.handleMouseDown),this.container?.addEventListener("touchstart",this.handleTouchStart);}removeEventListeners(){this.container?.removeEventListener("mousedown",this.handleMouseDown),this.container?.removeEventListener("touchstart",this.handleTouchStart),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp),document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}handleMouseDown(e){this.isDragging=true,document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp),this.updatePosition(e.clientX);}handleMouseMove(e){this.isDragging&&this.updatePosition(e.clientX);}handleMouseUp(){this.isDragging=false,document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp);}handleTouchStart(e){this.isDragging=true,document.addEventListener("touchmove",this.handleTouchMove),document.addEventListener("touchend",this.handleTouchEnd);let t=e.touches[0];this.updatePosition(t.clientX);}handleTouchMove(e){if(!this.isDragging)return;let t=e.touches[0];this.updatePosition(t.clientX);}handleTouchEnd(){this.isDragging=false,document.removeEventListener("touchmove",this.handleTouchMove),document.removeEventListener("touchend",this.handleTouchEnd);}updatePosition(e){if(!this.container||!this.image2||!this.divider)return;let t=this.container.getBoundingClientRect(),i=e-t.left,n=Math.max(0,Math.min(100,i/t.width*100));this.currentPosition=n,this.image2.style.clipPath=`inset(0 0 0 ${n}%)`,this.divider.style.left=`${n}%`,this.dispatchEvent(new CustomEvent("position-change",{detail:{position:n}}));}};customElements.define("blazediff-swipe",h);export{h as SwipeMode};
@@ -1,18 +0,0 @@
1
- import { BaseElement } from './base-element.js';
2
-
3
- declare class TwoUpMode extends BaseElement {
4
- private container;
5
- private containerInner;
6
- private leftPanel;
7
- private rightPanel;
8
- private img1;
9
- private img2;
10
- private dimensionInfo;
11
- static get observedAttributes(): string[];
12
- connectedCallback(): void;
13
- attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
14
- private render;
15
- private loadImages;
16
- }
17
-
18
- export { TwoUpMode };
@@ -1 +0,0 @@
1
- 'use strict';var m=class extends HTMLElement{getClassName(l){return this.getAttribute(`class-${l}`)||""}applyClassName(l,n){let t=this.getClassName(n);t&&(l.className=t);}};var o=class extends m{constructor(){super(...arguments);this.container=null;this.containerInner=null;this.leftPanel=null;this.rightPanel=null;this.img1=null;this.img2=null;this.dimensionInfo=null;}static get observedAttributes(){return ["src1","src2","class-container","class-container-inner","class-panel","class-image","class-dimension-info"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(n,t,i){if(t!==i)switch(n){case "src1":case "src2":this.loadImages();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-container-inner":this.containerInner&&this.applyClassName(this.containerInner,"container-inner");break;case "class-panel":this.leftPanel&&this.applyClassName(this.leftPanel,"panel"),this.rightPanel&&this.applyClassName(this.rightPanel,"panel");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-dimension-info":this.dimensionInfo&&this.applyClassName(this.dimensionInfo,"dimension-info");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.containerInner=document.createElement("div"),this.applyClassName(this.containerInner,"container-inner"),this.leftPanel=document.createElement("div"),this.applyClassName(this.leftPanel,"panel"),this.rightPanel=document.createElement("div"),this.applyClassName(this.rightPanel,"panel"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.dimensionInfo=document.createElement("div"),this.applyClassName(this.dimensionInfo,"dimension-info"),this.leftPanel.appendChild(this.img1),this.rightPanel.appendChild(this.img2),this.containerInner.appendChild(this.leftPanel),this.containerInner.appendChild(this.rightPanel),this.container.appendChild(this.containerInner),this.container.appendChild(this.dimensionInfo),this.appendChild(this.container);}async loadImages(){let n=this.getAttribute("src1"),t=this.getAttribute("src2");if(!(!n||!t||!this.img1||!this.img2||!this.dimensionInfo))try{this.img1.src=n,this.img2.src=t,await Promise.all([new Promise((s,e)=>{if(!this.img1)return e();this.img1.onload=s,this.img1.onerror=e;}),new Promise((s,e)=>{if(!this.img2)return e();this.img2.onload=s,this.img2.onerror=e;})]);let i=this.img1.naturalWidth,a=this.img1.naturalHeight,r=this.img2.naturalWidth,h=this.img2.naturalHeight;if(i!==r||a!==h){let s=r-i,e=h-a;this.dimensionInfo.textContent=`Dimensions changed: ${i}\xD7${a} \u2192 ${r}\xD7${h} (${s>0?"+":""}${s}\xD7${e>0?"+":""}${e})`,this.dimensionInfo.style.display="block";}else this.dimensionInfo.textContent=`${i}\xD7${a}`,this.dimensionInfo.style.display="block";this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:i,height:a},image2:{width:r,height:h}}}));}catch(i){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:i}}));}}};customElements.define("blazediff-twoup",o);exports.TwoUpMode=o;
@@ -1 +0,0 @@
1
- var m=class extends HTMLElement{getClassName(l){return this.getAttribute(`class-${l}`)||""}applyClassName(l,n){let t=this.getClassName(n);t&&(l.className=t);}};var o=class extends m{constructor(){super(...arguments);this.container=null;this.containerInner=null;this.leftPanel=null;this.rightPanel=null;this.img1=null;this.img2=null;this.dimensionInfo=null;}static get observedAttributes(){return ["src1","src2","class-container","class-container-inner","class-panel","class-image","class-dimension-info"]}connectedCallback(){this.render(),this.loadImages();}attributeChangedCallback(n,t,i){if(t!==i)switch(n){case "src1":case "src2":this.loadImages();break;case "class-container":this.container&&this.applyClassName(this.container,"container");break;case "class-container-inner":this.containerInner&&this.applyClassName(this.containerInner,"container-inner");break;case "class-panel":this.leftPanel&&this.applyClassName(this.leftPanel,"panel"),this.rightPanel&&this.applyClassName(this.rightPanel,"panel");break;case "class-image":this.img1&&this.applyClassName(this.img1,"image"),this.img2&&this.applyClassName(this.img2,"image");break;case "class-dimension-info":this.dimensionInfo&&this.applyClassName(this.dimensionInfo,"dimension-info");break}}render(){this.container=document.createElement("div"),this.applyClassName(this.container,"container"),this.containerInner=document.createElement("div"),this.applyClassName(this.containerInner,"container-inner"),this.leftPanel=document.createElement("div"),this.applyClassName(this.leftPanel,"panel"),this.rightPanel=document.createElement("div"),this.applyClassName(this.rightPanel,"panel"),this.img1=document.createElement("img"),this.img1.crossOrigin="anonymous",this.applyClassName(this.img1,"image"),this.img2=document.createElement("img"),this.img2.crossOrigin="anonymous",this.applyClassName(this.img2,"image"),this.dimensionInfo=document.createElement("div"),this.applyClassName(this.dimensionInfo,"dimension-info"),this.leftPanel.appendChild(this.img1),this.rightPanel.appendChild(this.img2),this.containerInner.appendChild(this.leftPanel),this.containerInner.appendChild(this.rightPanel),this.container.appendChild(this.containerInner),this.container.appendChild(this.dimensionInfo),this.appendChild(this.container);}async loadImages(){let n=this.getAttribute("src1"),t=this.getAttribute("src2");if(!(!n||!t||!this.img1||!this.img2||!this.dimensionInfo))try{this.img1.src=n,this.img2.src=t,await Promise.all([new Promise((s,e)=>{if(!this.img1)return e();this.img1.onload=s,this.img1.onerror=e;}),new Promise((s,e)=>{if(!this.img2)return e();this.img2.onload=s,this.img2.onerror=e;})]);let i=this.img1.naturalWidth,a=this.img1.naturalHeight,r=this.img2.naturalWidth,h=this.img2.naturalHeight;if(i!==r||a!==h){let s=r-i,e=h-a;this.dimensionInfo.textContent=`Dimensions changed: ${i}\xD7${a} \u2192 ${r}\xD7${h} (${s>0?"+":""}${s}\xD7${e>0?"+":""}${e})`,this.dimensionInfo.style.display="block";}else this.dimensionInfo.textContent=`${i}\xD7${a}`,this.dimensionInfo.style.display="block";this.dispatchEvent(new CustomEvent("images-loaded",{detail:{image1:{width:i,height:a},image2:{width:r,height:h}}}));}catch(i){this.dispatchEvent(new CustomEvent("load-error",{detail:{error:i}}));}}};customElements.define("blazediff-twoup",o);export{o as TwoUpMode};