@blazediff/react 1.6.6 → 1.8.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 +12 -9
- package/dist/index.d.ts +5 -2
- package/dist/index.js +327 -165
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +331 -169
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -17,6 +17,8 @@ npm install @blazediff/react
|
|
|
17
17
|
|
|
18
18
|
## Usage
|
|
19
19
|
|
|
20
|
+
`src1` and `src2` accept image URLs or `Uint8Array` values containing encoded image bytes in every component.
|
|
21
|
+
|
|
20
22
|
```tsx
|
|
21
23
|
import { DifferenceMode, SwipeMode, TwoUpMode, OnionSkinMode } from '@blazediff/react';
|
|
22
24
|
|
|
@@ -59,11 +61,12 @@ import { DifferenceMode, SwipeMode, TwoUpMode, OnionSkinMode } from '@blazediff/
|
|
|
59
61
|
|
|
60
62
|
### DifferenceMode
|
|
61
63
|
|
|
62
|
-
Highlights pixel differences between two images using the blazediff algorithm.
|
|
64
|
+
Highlights pixel differences between two images using the blazediff algorithm. Pass encoded diff image bytes through `diff` to display a precomputed diff without running BlazeDiff in the browser.
|
|
63
65
|
|
|
64
66
|
**Props:**
|
|
65
|
-
- `src1` (string): URL
|
|
66
|
-
- `src2` (string): URL
|
|
67
|
+
- `src1` (`string | Uint8Array`): URL or encoded bytes for the first image
|
|
68
|
+
- `src2` (`string | Uint8Array`): URL or encoded bytes for the second image
|
|
69
|
+
- `diff` (`Uint8Array`, optional): Encoded precomputed diff image bytes
|
|
67
70
|
- `threshold` (number): Matching threshold (0-1, default: 0.1)
|
|
68
71
|
- `includeAA` (boolean): Include anti-aliasing in diff (default: false)
|
|
69
72
|
- `alpha` (number): Blending factor for unchanged pixels (0-1, default: 0.1)
|
|
@@ -75,8 +78,8 @@ Highlights pixel differences between two images using the blazediff algorithm.
|
|
|
75
78
|
Interactive slider to compare two images.
|
|
76
79
|
|
|
77
80
|
**Props:**
|
|
78
|
-
- `src1` (string): URL
|
|
79
|
-
- `src2` (string): URL
|
|
81
|
+
- `src1` (`string | Uint8Array`): URL or encoded bytes for the first image
|
|
82
|
+
- `src2` (`string | Uint8Array`): URL or encoded bytes for the second image
|
|
80
83
|
- `alt1` (string): Alt text for first image (default: "Before")
|
|
81
84
|
- `alt2` (string): Alt text for second image (default: "After")
|
|
82
85
|
- `onPositionChange` (function): Callback when slider position changes
|
|
@@ -86,8 +89,8 @@ Interactive slider to compare two images.
|
|
|
86
89
|
Side-by-side image comparison with dimension change detection.
|
|
87
90
|
|
|
88
91
|
**Props:**
|
|
89
|
-
- `src1` (string): URL
|
|
90
|
-
- `src2` (string): URL
|
|
92
|
+
- `src1` (`string | Uint8Array`): URL or encoded bytes for the first image
|
|
93
|
+
- `src2` (`string | Uint8Array`): URL or encoded bytes for the second image
|
|
91
94
|
- `onImagesLoaded` (function): Callback when images are loaded
|
|
92
95
|
- `onLoadError` (function): Callback when loading fails
|
|
93
96
|
|
|
@@ -96,8 +99,8 @@ Side-by-side image comparison with dimension change detection.
|
|
|
96
99
|
Overlay images with adjustable opacity.
|
|
97
100
|
|
|
98
101
|
**Props:**
|
|
99
|
-
- `src1` (string): URL
|
|
100
|
-
- `src2` (string): URL
|
|
102
|
+
- `src1` (`string | Uint8Array`): URL or encoded bytes for the first image
|
|
103
|
+
- `src2` (`string | Uint8Array`): URL or encoded bytes for the second image
|
|
101
104
|
- `opacity` (number): Initial opacity (0-100, default: 50)
|
|
102
105
|
- `onOpacityChange` (function): Callback when opacity changes
|
|
103
106
|
- `onImagesLoaded` (function): Callback when images are loaded
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
+
import { ImageSource } from '@blazediff/ui';
|
|
3
|
+
export { ImageSource } from '@blazediff/ui';
|
|
2
4
|
|
|
3
5
|
interface BaseBlazeDiffProps {
|
|
4
|
-
src1:
|
|
5
|
-
src2:
|
|
6
|
+
src1: ImageSource;
|
|
7
|
+
src2: ImageSource;
|
|
6
8
|
className?: string;
|
|
7
9
|
containerClassName?: string;
|
|
8
10
|
}
|
|
9
11
|
interface DifferenceModeProps extends BaseBlazeDiffProps {
|
|
12
|
+
diff?: Uint8Array;
|
|
10
13
|
threshold?: number;
|
|
11
14
|
includeAA?: boolean;
|
|
12
15
|
alpha?: number;
|
package/dist/index.js
CHANGED
|
@@ -28,12 +28,64 @@ __export(index_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(index_exports);
|
|
29
29
|
|
|
30
30
|
// src/DifferenceMode.tsx
|
|
31
|
+
var import_engine2 = require("@blazediff/ui/engine");
|
|
32
|
+
var import_react4 = require("react");
|
|
33
|
+
|
|
34
|
+
// src/useEngine.ts
|
|
31
35
|
var import_react = require("react");
|
|
32
|
-
|
|
36
|
+
function useEngine(factory) {
|
|
37
|
+
const ref = (0, import_react.useRef)(null);
|
|
38
|
+
if (ref.current === null) ref.current = factory();
|
|
39
|
+
const [, force] = (0, import_react.useState)(0);
|
|
40
|
+
(0, import_react.useEffect)(() => {
|
|
41
|
+
if (ref.current === null) ref.current = factory();
|
|
42
|
+
const engine2 = ref.current;
|
|
43
|
+
force((n) => n + 1);
|
|
44
|
+
const unsubscribe = engine2.subscribe(() => force((n) => n + 1));
|
|
45
|
+
return () => {
|
|
46
|
+
unsubscribe();
|
|
47
|
+
engine2.destroy();
|
|
48
|
+
ref.current = null;
|
|
49
|
+
};
|
|
50
|
+
}, []);
|
|
51
|
+
const engine = ref.current;
|
|
52
|
+
return [engine, engine.getState()];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// src/useImageSourceUrl.ts
|
|
56
|
+
var import_engine = require("@blazediff/ui/engine");
|
|
57
|
+
var import_react2 = require("react");
|
|
58
|
+
function useImageSourceUrl(source) {
|
|
59
|
+
const [resolvedBytes, setResolvedBytes] = (0, import_react2.useState)();
|
|
60
|
+
(0, import_react2.useEffect)(() => {
|
|
61
|
+
if (!(source instanceof Uint8Array)) {
|
|
62
|
+
setResolvedBytes(void 0);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const sourceUrl = (0, import_engine.createImageSourceUrl)(source);
|
|
66
|
+
setResolvedBytes({ source, url: sourceUrl.url });
|
|
67
|
+
return sourceUrl.revoke;
|
|
68
|
+
}, [source]);
|
|
69
|
+
if (typeof source === "string") return source;
|
|
70
|
+
if (resolvedBytes && resolvedBytes.source === source)
|
|
71
|
+
return resolvedBytes.url;
|
|
72
|
+
return void 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/useLatestRef.ts
|
|
76
|
+
var import_react3 = require("react");
|
|
77
|
+
function useLatestRef(value) {
|
|
78
|
+
const ref = (0, import_react3.useRef)(value);
|
|
79
|
+
ref.current = value;
|
|
80
|
+
return ref;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// src/DifferenceMode.tsx
|
|
33
84
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
34
85
|
var DifferenceMode = ({
|
|
35
86
|
src1,
|
|
36
87
|
src2,
|
|
88
|
+
diff,
|
|
37
89
|
threshold = 0.1,
|
|
38
90
|
includeAA = false,
|
|
39
91
|
alpha = 0.1,
|
|
@@ -43,51 +95,71 @@ var DifferenceMode = ({
|
|
|
43
95
|
onDiffComplete,
|
|
44
96
|
onDiffError
|
|
45
97
|
}) => {
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (!element) return;
|
|
50
|
-
const handleDiffComplete = (e) => {
|
|
51
|
-
onDiffComplete?.(e.detail);
|
|
52
|
-
};
|
|
53
|
-
const handleDiffError = (e) => {
|
|
54
|
-
onDiffError?.(e.detail.error);
|
|
55
|
-
};
|
|
56
|
-
element.addEventListener(
|
|
57
|
-
"diff-complete",
|
|
58
|
-
handleDiffComplete
|
|
59
|
-
);
|
|
60
|
-
element.addEventListener("diff-error", handleDiffError);
|
|
61
|
-
return () => {
|
|
62
|
-
element.removeEventListener(
|
|
63
|
-
"diff-complete",
|
|
64
|
-
handleDiffComplete
|
|
65
|
-
);
|
|
66
|
-
element.removeEventListener(
|
|
67
|
-
"diff-error",
|
|
68
|
-
handleDiffError
|
|
69
|
-
);
|
|
70
|
-
};
|
|
71
|
-
}, [onDiffComplete, onDiffError]);
|
|
72
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
73
|
-
"blazediff-difference",
|
|
74
|
-
{
|
|
75
|
-
ref,
|
|
76
|
-
className,
|
|
98
|
+
const diffUrl = useImageSourceUrl(diff);
|
|
99
|
+
const [engine, state] = useEngine(
|
|
100
|
+
() => (0, import_engine2.createDifferenceEngine)({
|
|
77
101
|
src1,
|
|
78
102
|
src2,
|
|
79
|
-
threshold
|
|
80
|
-
|
|
81
|
-
alpha
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
}
|
|
103
|
+
threshold,
|
|
104
|
+
includeAA,
|
|
105
|
+
alpha,
|
|
106
|
+
enabled: diff === void 0
|
|
107
|
+
})
|
|
85
108
|
);
|
|
109
|
+
const canvasRef = (0, import_react4.useRef)(null);
|
|
110
|
+
const onDiffCompleteRef = useLatestRef(onDiffComplete);
|
|
111
|
+
const onDiffErrorRef = useLatestRef(onDiffError);
|
|
112
|
+
(0, import_react4.useEffect)(() => {
|
|
113
|
+
engine.setConfig({
|
|
114
|
+
src1,
|
|
115
|
+
src2,
|
|
116
|
+
threshold,
|
|
117
|
+
includeAA,
|
|
118
|
+
alpha,
|
|
119
|
+
enabled: diff === void 0
|
|
120
|
+
});
|
|
121
|
+
}, [engine, src1, src2, threshold, includeAA, alpha, diff]);
|
|
122
|
+
(0, import_react4.useEffect)(() => {
|
|
123
|
+
const canvas = canvasRef.current;
|
|
124
|
+
if (state.status !== "ready" || !state.diff || !canvas) return;
|
|
125
|
+
const ctx = canvas.getContext("2d");
|
|
126
|
+
canvas.width = state.diff.width;
|
|
127
|
+
canvas.height = state.diff.height;
|
|
128
|
+
ctx?.putImageData(
|
|
129
|
+
new ImageData(
|
|
130
|
+
state.diff.output,
|
|
131
|
+
state.diff.width,
|
|
132
|
+
state.diff.height
|
|
133
|
+
),
|
|
134
|
+
0,
|
|
135
|
+
0
|
|
136
|
+
);
|
|
137
|
+
}, [state.status, state.diff]);
|
|
138
|
+
(0, import_react4.useEffect)(() => {
|
|
139
|
+
if (state.status === "ready" && state.diff) {
|
|
140
|
+
onDiffCompleteRef.current?.({
|
|
141
|
+
diffCount: state.diff.diffCount,
|
|
142
|
+
totalPixels: state.diff.totalPixels,
|
|
143
|
+
percentage: state.diff.percentage
|
|
144
|
+
});
|
|
145
|
+
} else if (state.status === "error") {
|
|
146
|
+
onDiffErrorRef.current?.(state.error);
|
|
147
|
+
}
|
|
148
|
+
}, [state.status]);
|
|
149
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: containerClassName, children: diff === void 0 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("canvas", { ref: canvasRef, className: canvasClassName }) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
150
|
+
"img",
|
|
151
|
+
{
|
|
152
|
+
src: diffUrl,
|
|
153
|
+
className: canvasClassName,
|
|
154
|
+
alt: "",
|
|
155
|
+
onError: (event) => onDiffErrorRef.current?.(event.nativeEvent)
|
|
156
|
+
}
|
|
157
|
+
) }) });
|
|
86
158
|
};
|
|
87
159
|
|
|
88
160
|
// src/OnionSkinMode.tsx
|
|
89
|
-
var
|
|
90
|
-
var
|
|
161
|
+
var import_engine3 = require("@blazediff/ui/engine");
|
|
162
|
+
var import_react5 = require("react");
|
|
91
163
|
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
92
164
|
var OnionSkinMode = ({
|
|
93
165
|
src1,
|
|
@@ -105,66 +177,96 @@ var OnionSkinMode = ({
|
|
|
105
177
|
onImagesLoaded,
|
|
106
178
|
onLoadError
|
|
107
179
|
}) => {
|
|
108
|
-
const
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const handleOpacityChange = (e) => {
|
|
113
|
-
onOpacityChange?.(e.detail.opacity);
|
|
114
|
-
};
|
|
115
|
-
const handleImagesLoaded = (e) => {
|
|
116
|
-
onImagesLoaded?.(e.detail);
|
|
117
|
-
};
|
|
118
|
-
const handleLoadError = (e) => {
|
|
119
|
-
onLoadError?.(e.detail.error);
|
|
120
|
-
};
|
|
121
|
-
element.addEventListener(
|
|
122
|
-
"opacity-change",
|
|
123
|
-
handleOpacityChange
|
|
124
|
-
);
|
|
125
|
-
element.addEventListener(
|
|
126
|
-
"images-loaded",
|
|
127
|
-
handleImagesLoaded
|
|
128
|
-
);
|
|
129
|
-
element.addEventListener("load-error", handleLoadError);
|
|
130
|
-
return () => {
|
|
131
|
-
element.removeEventListener(
|
|
132
|
-
"opacity-change",
|
|
133
|
-
handleOpacityChange
|
|
134
|
-
);
|
|
135
|
-
element.removeEventListener(
|
|
136
|
-
"images-loaded",
|
|
137
|
-
handleImagesLoaded
|
|
138
|
-
);
|
|
139
|
-
element.removeEventListener(
|
|
140
|
-
"load-error",
|
|
141
|
-
handleLoadError
|
|
142
|
-
);
|
|
143
|
-
};
|
|
144
|
-
}, [onOpacityChange, onImagesLoaded, onLoadError]);
|
|
145
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
146
|
-
"blazediff-onionskin",
|
|
147
|
-
{
|
|
148
|
-
ref,
|
|
149
|
-
className,
|
|
150
|
-
src1,
|
|
151
|
-
src2,
|
|
152
|
-
opacity: String(opacity),
|
|
153
|
-
"class-container": containerClassName,
|
|
154
|
-
"class-image-container": imageContainerClassName,
|
|
155
|
-
"class-image": imageClassName,
|
|
156
|
-
"class-slider-container": sliderContainerClassName,
|
|
157
|
-
"class-slider": sliderClassName,
|
|
158
|
-
"class-slider-label": sliderLabelClassName,
|
|
159
|
-
"text-slider-label": sliderLabelText
|
|
160
|
-
}
|
|
180
|
+
const src1Url = useImageSourceUrl(src1);
|
|
181
|
+
const src2Url = useImageSourceUrl(src2);
|
|
182
|
+
const [engine, state] = useEngine(
|
|
183
|
+
() => (0, import_engine3.createOnionSkinEngine)({ src1, src2 }, opacity)
|
|
161
184
|
);
|
|
185
|
+
const isFirstOpacity = (0, import_react5.useRef)(true);
|
|
186
|
+
const onOpacityChangeRef = useLatestRef(onOpacityChange);
|
|
187
|
+
const onImagesLoadedRef = useLatestRef(onImagesLoaded);
|
|
188
|
+
const onLoadErrorRef = useLatestRef(onLoadError);
|
|
189
|
+
(0, import_react5.useEffect)(() => {
|
|
190
|
+
engine.setConfig({ src1, src2 });
|
|
191
|
+
}, [engine, src1, src2]);
|
|
192
|
+
(0, import_react5.useEffect)(() => {
|
|
193
|
+
engine.actions.setOpacity(opacity);
|
|
194
|
+
}, [engine, opacity]);
|
|
195
|
+
(0, import_react5.useEffect)(() => {
|
|
196
|
+
if (isFirstOpacity.current) {
|
|
197
|
+
isFirstOpacity.current = false;
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
onOpacityChangeRef.current?.(state.opacity);
|
|
201
|
+
}, [state.opacity, onOpacityChangeRef]);
|
|
202
|
+
(0, import_react5.useEffect)(() => {
|
|
203
|
+
if (state.status === "ready" && state.dims1 && state.dims2) {
|
|
204
|
+
onImagesLoadedRef.current?.({ image1: state.dims1, image2: state.dims2 });
|
|
205
|
+
} else if (state.status === "error") {
|
|
206
|
+
onLoadErrorRef.current?.(state.error);
|
|
207
|
+
}
|
|
208
|
+
}, [state.status]);
|
|
209
|
+
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: containerClassName, children: [
|
|
210
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)(
|
|
211
|
+
"div",
|
|
212
|
+
{
|
|
213
|
+
className: imageContainerClassName,
|
|
214
|
+
style: { position: "relative" },
|
|
215
|
+
children: [
|
|
216
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
217
|
+
"img",
|
|
218
|
+
{
|
|
219
|
+
className: imageClassName,
|
|
220
|
+
src: src1Url,
|
|
221
|
+
crossOrigin: "anonymous",
|
|
222
|
+
alt: ""
|
|
223
|
+
}
|
|
224
|
+
),
|
|
225
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
226
|
+
"img",
|
|
227
|
+
{
|
|
228
|
+
className: imageClassName,
|
|
229
|
+
src: src2Url,
|
|
230
|
+
crossOrigin: "anonymous",
|
|
231
|
+
alt: "",
|
|
232
|
+
style: {
|
|
233
|
+
position: "absolute",
|
|
234
|
+
top: 0,
|
|
235
|
+
left: 0,
|
|
236
|
+
opacity: (0, import_engine3.normalizedOpacity)(state.opacity)
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
)
|
|
240
|
+
]
|
|
241
|
+
}
|
|
242
|
+
),
|
|
243
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsxs)("div", { className: sliderContainerClassName, children: [
|
|
244
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)("label", { className: sliderLabelClassName, children: sliderLabelText ?? "Opacity:" }),
|
|
245
|
+
/* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
|
|
246
|
+
"input",
|
|
247
|
+
{
|
|
248
|
+
className: sliderClassName,
|
|
249
|
+
type: "range",
|
|
250
|
+
min: 0,
|
|
251
|
+
max: 100,
|
|
252
|
+
value: state.opacity,
|
|
253
|
+
onChange: (e) => engine.actions.setOpacity(Number(e.target.value))
|
|
254
|
+
}
|
|
255
|
+
)
|
|
256
|
+
] })
|
|
257
|
+
] }) });
|
|
162
258
|
};
|
|
163
259
|
|
|
164
260
|
// src/SwipeMode.tsx
|
|
165
|
-
var
|
|
166
|
-
var
|
|
261
|
+
var import_engine4 = require("@blazediff/ui/engine");
|
|
262
|
+
var import_react6 = require("react");
|
|
167
263
|
var import_jsx_runtime3 = require("react/jsx-runtime");
|
|
264
|
+
var sharedImageStyle = {
|
|
265
|
+
width: "100%",
|
|
266
|
+
height: "100%",
|
|
267
|
+
objectFit: "contain",
|
|
268
|
+
pointerEvents: "none"
|
|
269
|
+
};
|
|
168
270
|
var SwipeMode = ({
|
|
169
271
|
src1,
|
|
170
272
|
src2,
|
|
@@ -177,44 +279,106 @@ var SwipeMode = ({
|
|
|
177
279
|
dividerClassName,
|
|
178
280
|
onPositionChange
|
|
179
281
|
}) => {
|
|
180
|
-
const
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
);
|
|
282
|
+
const src1Url = useImageSourceUrl(src1);
|
|
283
|
+
const src2Url = useImageSourceUrl(src2);
|
|
284
|
+
const [engine, state] = useEngine(() => (0, import_engine4.createSwipeEngine)(50));
|
|
285
|
+
const containerRef = (0, import_react6.useRef)(null);
|
|
286
|
+
const isFirstPosition = (0, import_react6.useRef)(true);
|
|
287
|
+
const onPositionChangeRef = useLatestRef(onPositionChange);
|
|
288
|
+
const percentFromClientX = (0, import_react6.useCallback)((clientX) => {
|
|
289
|
+
const el = containerRef.current;
|
|
290
|
+
if (!el) return 0;
|
|
291
|
+
const rect = el.getBoundingClientRect();
|
|
292
|
+
if (rect.width === 0) return 0;
|
|
293
|
+
return (clientX - rect.left) / rect.width * 100;
|
|
294
|
+
}, []);
|
|
295
|
+
(0, import_react6.useEffect)(() => {
|
|
296
|
+
if (!state.isDragging) return;
|
|
297
|
+
const onMouseMove = (e) => engine.actions.move(percentFromClientX(e.clientX));
|
|
298
|
+
const onMouseUp = () => engine.actions.end();
|
|
299
|
+
const onTouchMove = (e) => engine.actions.move(percentFromClientX(e.touches[0].clientX));
|
|
300
|
+
const onTouchEnd = () => engine.actions.end();
|
|
301
|
+
document.addEventListener("mousemove", onMouseMove);
|
|
302
|
+
document.addEventListener("mouseup", onMouseUp);
|
|
303
|
+
document.addEventListener("touchmove", onTouchMove);
|
|
304
|
+
document.addEventListener("touchend", onTouchEnd);
|
|
191
305
|
return () => {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
);
|
|
306
|
+
document.removeEventListener("mousemove", onMouseMove);
|
|
307
|
+
document.removeEventListener("mouseup", onMouseUp);
|
|
308
|
+
document.removeEventListener("touchmove", onTouchMove);
|
|
309
|
+
document.removeEventListener("touchend", onTouchEnd);
|
|
196
310
|
};
|
|
197
|
-
}, [
|
|
198
|
-
|
|
199
|
-
|
|
311
|
+
}, [state.isDragging, engine, percentFromClientX]);
|
|
312
|
+
(0, import_react6.useEffect)(() => {
|
|
313
|
+
if (isFirstPosition.current) {
|
|
314
|
+
isFirstPosition.current = false;
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
onPositionChangeRef.current?.(state.position);
|
|
318
|
+
}, [state.position, onPositionChangeRef]);
|
|
319
|
+
return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(
|
|
320
|
+
"div",
|
|
200
321
|
{
|
|
201
|
-
ref,
|
|
202
|
-
className,
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
322
|
+
ref: containerRef,
|
|
323
|
+
className: containerClassName,
|
|
324
|
+
style: {
|
|
325
|
+
position: "relative",
|
|
326
|
+
overflow: "hidden",
|
|
327
|
+
cursor: "ew-resize",
|
|
328
|
+
userSelect: "none",
|
|
329
|
+
WebkitUserSelect: "none"
|
|
330
|
+
},
|
|
331
|
+
onMouseDown: (e) => engine.actions.start(percentFromClientX(e.clientX)),
|
|
332
|
+
onTouchStart: (e) => engine.actions.start(percentFromClientX(e.touches[0].clientX)),
|
|
333
|
+
children: [
|
|
334
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
335
|
+
"img",
|
|
336
|
+
{
|
|
337
|
+
className: image1ClassName,
|
|
338
|
+
src: src1Url,
|
|
339
|
+
alt: alt1,
|
|
340
|
+
style: sharedImageStyle
|
|
341
|
+
}
|
|
342
|
+
),
|
|
343
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
344
|
+
"img",
|
|
345
|
+
{
|
|
346
|
+
className: image2ClassName,
|
|
347
|
+
src: src2Url,
|
|
348
|
+
alt: alt2,
|
|
349
|
+
style: {
|
|
350
|
+
...sharedImageStyle,
|
|
351
|
+
position: "absolute",
|
|
352
|
+
top: 0,
|
|
353
|
+
left: 0,
|
|
354
|
+
clipPath: `inset(0 0 0 ${state.position}%)`
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
),
|
|
358
|
+
/* @__PURE__ */ (0, import_jsx_runtime3.jsx)(
|
|
359
|
+
"div",
|
|
360
|
+
{
|
|
361
|
+
className: dividerClassName,
|
|
362
|
+
style: {
|
|
363
|
+
position: "absolute",
|
|
364
|
+
top: 0,
|
|
365
|
+
bottom: 0,
|
|
366
|
+
left: `${state.position}%`,
|
|
367
|
+
width: 2,
|
|
368
|
+
backgroundColor: "white",
|
|
369
|
+
cursor: "ew-resize",
|
|
370
|
+
zIndex: 10
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
)
|
|
374
|
+
]
|
|
211
375
|
}
|
|
212
|
-
);
|
|
376
|
+
) });
|
|
213
377
|
};
|
|
214
378
|
|
|
215
379
|
// src/TwoUpMode.tsx
|
|
216
|
-
var
|
|
217
|
-
var
|
|
380
|
+
var import_engine5 = require("@blazediff/ui/engine");
|
|
381
|
+
var import_react7 = require("react");
|
|
218
382
|
var import_jsx_runtime4 = require("react/jsx-runtime");
|
|
219
383
|
var TwoUpMode = ({
|
|
220
384
|
src1,
|
|
@@ -228,46 +392,44 @@ var TwoUpMode = ({
|
|
|
228
392
|
onImagesLoaded,
|
|
229
393
|
onLoadError
|
|
230
394
|
}) => {
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
);
|
|
245
|
-
element.addEventListener("load-error", handleLoadError);
|
|
246
|
-
return () => {
|
|
247
|
-
element.removeEventListener(
|
|
248
|
-
"images-loaded",
|
|
249
|
-
handleImagesLoaded
|
|
250
|
-
);
|
|
251
|
-
element.removeEventListener(
|
|
252
|
-
"load-error",
|
|
253
|
-
handleLoadError
|
|
254
|
-
);
|
|
255
|
-
};
|
|
256
|
-
}, [onImagesLoaded, onLoadError]);
|
|
257
|
-
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
258
|
-
"blazediff-twoup",
|
|
259
|
-
{
|
|
260
|
-
ref,
|
|
261
|
-
className,
|
|
262
|
-
src1,
|
|
263
|
-
src2,
|
|
264
|
-
"class-container": containerClassName,
|
|
265
|
-
"class-container-inner": containerInnerClassName,
|
|
266
|
-
"class-panel": panelClassName,
|
|
267
|
-
"class-image": imageClassName,
|
|
268
|
-
"class-dimension-info": dimensionInfoClassName
|
|
395
|
+
const src1Url = useImageSourceUrl(src1);
|
|
396
|
+
const src2Url = useImageSourceUrl(src2);
|
|
397
|
+
const [engine, state] = useEngine(() => (0, import_engine5.createTwoUpEngine)({ src1, src2 }));
|
|
398
|
+
const onImagesLoadedRef = useLatestRef(onImagesLoaded);
|
|
399
|
+
const onLoadErrorRef = useLatestRef(onLoadError);
|
|
400
|
+
(0, import_react7.useEffect)(() => {
|
|
401
|
+
engine.setConfig({ src1, src2 });
|
|
402
|
+
}, [engine, src1, src2]);
|
|
403
|
+
(0, import_react7.useEffect)(() => {
|
|
404
|
+
if (state.status === "ready" && state.dims1 && state.dims2) {
|
|
405
|
+
onImagesLoadedRef.current?.({ image1: state.dims1, image2: state.dims2 });
|
|
406
|
+
} else if (state.status === "error") {
|
|
407
|
+
onLoadErrorRef.current?.(state.error);
|
|
269
408
|
}
|
|
270
|
-
);
|
|
409
|
+
}, [state.status]);
|
|
410
|
+
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: containerClassName, children: [
|
|
411
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: containerInnerClassName, style: { display: "flex" }, children: [
|
|
412
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: panelClassName, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
413
|
+
"img",
|
|
414
|
+
{
|
|
415
|
+
className: imageClassName,
|
|
416
|
+
src: src1Url,
|
|
417
|
+
crossOrigin: "anonymous",
|
|
418
|
+
alt: ""
|
|
419
|
+
}
|
|
420
|
+
) }),
|
|
421
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: panelClassName, children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
|
|
422
|
+
"img",
|
|
423
|
+
{
|
|
424
|
+
className: imageClassName,
|
|
425
|
+
src: src2Url,
|
|
426
|
+
crossOrigin: "anonymous",
|
|
427
|
+
alt: ""
|
|
428
|
+
}
|
|
429
|
+
) })
|
|
430
|
+
] }),
|
|
431
|
+
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { className: dimensionInfoClassName, children: state.dimensionLabel ?? "" })
|
|
432
|
+
] }) });
|
|
271
433
|
};
|
|
272
434
|
// Annotate the CommonJS export names for ESM import in node:
|
|
273
435
|
0 && (module.exports = {
|