@molecule/app-feature-image-canvas-solid 0.0.0-stage → 1.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/LICENSE +202 -0
- package/README.md +329 -2
- package/dist/ImageCanvas.d.ts +27 -0
- package/dist/ImageCanvas.d.ts.map +1 -0
- package/dist/ImageCanvas.jsx +183 -0
- package/dist/ImageCanvas.jsx.map +1 -0
- package/dist/helpers.d.ts +75 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +139 -0
- package/dist/helpers.js.map +1 -0
- package/dist/image-source.d.ts +16 -0
- package/dist/image-source.d.ts.map +1 -0
- package/dist/image-source.js +22 -0
- package/dist/image-source.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +129 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +8 -0
- package/dist/types.js.map +1 -0
- package/package.json +66 -4
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solid Canvas2D image-editing surface — a filter chain, pointer-drag
|
|
3
|
+
* panning, wheel zooming and a data-URL export handle.
|
|
4
|
+
*
|
|
5
|
+
* Solid counterpart of `@molecule/app-feature-image-canvas-react`'s
|
|
6
|
+
* `ImageCanvas` — the same props (React names), the same DOM for the same
|
|
7
|
+
* input.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import { createEffect, createMemo, createSignal, onCleanup, onMount, } from 'solid-js';
|
|
12
|
+
import { useTranslation } from '@molecule/app-solid';
|
|
13
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
14
|
+
import { clamp, clampPan, composeFilterString } from './helpers.js';
|
|
15
|
+
import { isImageElement } from './image-source.js';
|
|
16
|
+
/**
|
|
17
|
+
* Canvas2D image-editing surface. Renders a source image onto a canvas with a
|
|
18
|
+
* filter chain (brightness, contrast, saturation, hue, sepia, grayscale, blur,
|
|
19
|
+
* sharpen), pointer-event drag panning, and wheel zooming. The current state
|
|
20
|
+
* can be exported to a data URL via the `exportRef` imperative handle.
|
|
21
|
+
*
|
|
22
|
+
* All styling routes through `getClassMap()` (no raw class names). All
|
|
23
|
+
* user-visible text routes through `t()` so the canvas translates via the
|
|
24
|
+
* companion locale bond.
|
|
25
|
+
*
|
|
26
|
+
* @param props - Component props (see {@link ImageCanvasProps}).
|
|
27
|
+
* @returns The rendered image canvas.
|
|
28
|
+
*/
|
|
29
|
+
export const ImageCanvas = (props) => {
|
|
30
|
+
const cm = getClassMap();
|
|
31
|
+
const { t } = useTranslation();
|
|
32
|
+
let canvas;
|
|
33
|
+
const [internalImage, setInternalImage] = createSignal(isImageElement(props.src) ? props.src : null);
|
|
34
|
+
const [loadError, setLoadError] = createSignal(null);
|
|
35
|
+
const width = () => props.width ?? 512;
|
|
36
|
+
const height = () => props.height ?? 512;
|
|
37
|
+
const safeZoom = () => {
|
|
38
|
+
const zoom = props.zoom;
|
|
39
|
+
return zoom !== undefined && Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
40
|
+
};
|
|
41
|
+
const safePan = () => ({
|
|
42
|
+
x: Number.isFinite(props.pan?.x) ? props.pan?.x : 0,
|
|
43
|
+
y: Number.isFinite(props.pan?.y) ? props.pan?.y : 0,
|
|
44
|
+
});
|
|
45
|
+
const filterString = createMemo(() => composeFilterString(props.filters));
|
|
46
|
+
// Drag state lives outside signals: handlers mutate it without re-rendering,
|
|
47
|
+
// exactly like React's ref.
|
|
48
|
+
let dragState = { pointerId: null, startX: 0, startY: 0, startPan: { x: 0, y: 0 } };
|
|
49
|
+
// Load the source image when the prop is a URL string.
|
|
50
|
+
createEffect(() => {
|
|
51
|
+
const src = props.src;
|
|
52
|
+
if (typeof src !== 'string') {
|
|
53
|
+
if (isImageElement(src)) {
|
|
54
|
+
setInternalImage(src);
|
|
55
|
+
setLoadError(null);
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
let cancelled = false;
|
|
60
|
+
setLoadError(null);
|
|
61
|
+
const img = new Image();
|
|
62
|
+
img.crossOrigin = 'anonymous';
|
|
63
|
+
img.onload = () => {
|
|
64
|
+
if (cancelled)
|
|
65
|
+
return;
|
|
66
|
+
setInternalImage(img);
|
|
67
|
+
};
|
|
68
|
+
img.onerror = () => {
|
|
69
|
+
if (cancelled)
|
|
70
|
+
return;
|
|
71
|
+
setLoadError(src);
|
|
72
|
+
setInternalImage(null);
|
|
73
|
+
};
|
|
74
|
+
img.src = src;
|
|
75
|
+
onCleanup(() => {
|
|
76
|
+
cancelled = true;
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
// Re-paint whenever the image, viewport, or filter chain changes.
|
|
80
|
+
createEffect(() => {
|
|
81
|
+
const image = internalImage();
|
|
82
|
+
const zoom = safeZoom();
|
|
83
|
+
const pan = safePan();
|
|
84
|
+
const filter = filterString();
|
|
85
|
+
const canvasWidth = width();
|
|
86
|
+
const canvasHeight = height();
|
|
87
|
+
const node = canvas;
|
|
88
|
+
if (!node)
|
|
89
|
+
return;
|
|
90
|
+
const ctx = node.getContext('2d');
|
|
91
|
+
if (!ctx)
|
|
92
|
+
return;
|
|
93
|
+
ctx.save();
|
|
94
|
+
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
|
|
95
|
+
if (!image) {
|
|
96
|
+
ctx.restore();
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
ctx.filter = filter;
|
|
100
|
+
const drawWidth = image.naturalWidth * zoom;
|
|
101
|
+
const drawHeight = image.naturalHeight * zoom;
|
|
102
|
+
const dx = canvasWidth / 2 - drawWidth / 2 + pan.x;
|
|
103
|
+
const dy = canvasHeight / 2 - drawHeight / 2 + pan.y;
|
|
104
|
+
ctx.drawImage(image, dx, dy, drawWidth, drawHeight);
|
|
105
|
+
ctx.restore();
|
|
106
|
+
});
|
|
107
|
+
onMount(() => {
|
|
108
|
+
if (!props.exportRef)
|
|
109
|
+
return;
|
|
110
|
+
props.exportRef.current = {
|
|
111
|
+
toDataURL: (type, quality) => {
|
|
112
|
+
if (!canvas)
|
|
113
|
+
return '';
|
|
114
|
+
return canvas.toDataURL(type, quality);
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
onCleanup(() => {
|
|
119
|
+
if (props.exportRef)
|
|
120
|
+
props.exportRef.current = null;
|
|
121
|
+
});
|
|
122
|
+
const handlePointerDown = (event) => {
|
|
123
|
+
if (!canvas)
|
|
124
|
+
return;
|
|
125
|
+
canvas.setPointerCapture(event.pointerId);
|
|
126
|
+
dragState = {
|
|
127
|
+
pointerId: event.pointerId,
|
|
128
|
+
startX: event.clientX,
|
|
129
|
+
startY: event.clientY,
|
|
130
|
+
startPan: { ...safePan() },
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
const handlePointerMove = (event) => {
|
|
134
|
+
const state = dragState;
|
|
135
|
+
if (state.pointerId !== event.pointerId)
|
|
136
|
+
return;
|
|
137
|
+
const dx = event.clientX - state.startX;
|
|
138
|
+
const dy = event.clientY - state.startY;
|
|
139
|
+
const image = internalImage();
|
|
140
|
+
const next = clampPan({ x: state.startPan.x + dx, y: state.startPan.y + dy }, { width: width(), height: height() }, image
|
|
141
|
+
? { width: image.naturalWidth, height: image.naturalHeight }
|
|
142
|
+
: { width: width(), height: height() }, safeZoom());
|
|
143
|
+
props.onChange?.({ zoom: safeZoom(), pan: next });
|
|
144
|
+
};
|
|
145
|
+
const endDrag = (event) => {
|
|
146
|
+
const state = dragState;
|
|
147
|
+
if (state.pointerId !== event.pointerId)
|
|
148
|
+
return;
|
|
149
|
+
if (canvas && canvas.hasPointerCapture(event.pointerId)) {
|
|
150
|
+
canvas.releasePointerCapture(event.pointerId);
|
|
151
|
+
}
|
|
152
|
+
dragState = { pointerId: null, startX: 0, startY: 0, startPan: { x: 0, y: 0 } };
|
|
153
|
+
};
|
|
154
|
+
const handleWheel = (event) => {
|
|
155
|
+
// Negative deltaY = scroll up = zoom in.
|
|
156
|
+
const factor = event.deltaY < 0 ? 1.1 : 1 / 1.1;
|
|
157
|
+
const nextZoom = clamp(safeZoom() * factor, 0.05, 32);
|
|
158
|
+
props.onChange?.({ zoom: nextZoom, pan: safePan() });
|
|
159
|
+
};
|
|
160
|
+
return (<div role="region" aria-label={t('imageCanvas.aria.region', {}, { defaultValue: 'Image canvas' })} data-mol-id="image-canvas" class={cm.cn(props.className)} style={{
|
|
161
|
+
display: 'inline-block',
|
|
162
|
+
width: `${width()}px`,
|
|
163
|
+
height: `${height()}px`,
|
|
164
|
+
position: 'relative',
|
|
165
|
+
}}>
|
|
166
|
+
<canvas ref={(element) => {
|
|
167
|
+
canvas = element;
|
|
168
|
+
}} width={width()} height={height()} aria-label={t('imageCanvas.aria.canvas', {}, { defaultValue: 'Drag to pan, scroll to zoom' })} data-mol-id="image-canvas-surface" data-loading={internalImage() ? 'false' : loadError() ? 'false' : 'true'} data-error={loadError() ? 'true' : 'false'} style={{
|
|
169
|
+
display: 'block',
|
|
170
|
+
width: `${width()}px`,
|
|
171
|
+
height: `${height()}px`,
|
|
172
|
+
cursor: props.onChange ? 'grab' : 'default',
|
|
173
|
+
'touch-action': 'none',
|
|
174
|
+
}} onPointerDown={handlePointerDown} onPointerMove={handlePointerMove} onPointerUp={endDrag} onPointerCancel={endDrag} onWheel={handleWheel}/>
|
|
175
|
+
{!internalImage() && !loadError() ? (<p class={cm.cn(cm.textSize('xs'), cm.sp('p', 2))} data-mol-id="image-canvas-loading">
|
|
176
|
+
{t('imageCanvas.loading', {}, { defaultValue: 'Loading image…' })}
|
|
177
|
+
</p>) : null}
|
|
178
|
+
{loadError() ? (<p class={cm.cn(cm.textSize('xs'), cm.sp('p', 2))} data-mol-id="image-canvas-error" role="alert">
|
|
179
|
+
{t('imageCanvas.error', {}, { defaultValue: 'Image failed to load.' })}
|
|
180
|
+
</p>) : null}
|
|
181
|
+
</div>);
|
|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AAE,EAAE,EAAE,YAAY,EAAE,gBAAgB,EAAE,CAAC,CACnE;QAAA,EAAE,CAAC,CAAC,CACL,CAAC,CAAC,CAAC,IAAI,CACR;MAAA,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC,CACb,CAAC,CAAC,CACA,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAC/C,WAAW,CAAC,oBAAoB,CAChC,IAAI,CAAC,OAAO,CAEZ;UAAA,CAAC,CAAC,CAAC,mBAAmB,EAAE,EAAE,EAAE,EAAE,YAAY,EAAE,uBAAuB,EAAE,CAAC,CACxE;QAAA,EAAE,CAAC,CAAC,CACL,CAAC,CAAC,CAAC,IAAI,CACV;IAAA,EAAE,GAAG,CAAC,CACP,CAAA;AACH,CAAC,CAAA"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free helpers of the Solid Canvas2D image-editing surface — the same
|
|
3
|
+
* values `@molecule/app-feature-image-canvas-react` exports.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { ImageCanvasFilters, PanOffset } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Clamp a numeric value into the closed interval `[min, max]`. Returns
|
|
10
|
+
* `min` for non-finite inputs.
|
|
11
|
+
*
|
|
12
|
+
* @param value - The value to clamp.
|
|
13
|
+
* @param min - Inclusive minimum.
|
|
14
|
+
* @param max - Inclusive maximum.
|
|
15
|
+
* @returns The clamped value.
|
|
16
|
+
*/
|
|
17
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
18
|
+
/**
|
|
19
|
+
* Compose a CSS filter string from a set of `ImageCanvasFilters` values.
|
|
20
|
+
* Out-of-range values are clamped; omitted / non-finite fields are
|
|
21
|
+
* ignored. Returns `'none'` when no filters are active so callers can
|
|
22
|
+
* assign the result directly to `CanvasRenderingContext2D.filter`.
|
|
23
|
+
*
|
|
24
|
+
* @param filters - The filter parameters. May be `undefined`.
|
|
25
|
+
* @returns A CSS filter string, or `'none'` when nothing applies.
|
|
26
|
+
*/
|
|
27
|
+
export declare function composeFilterString(filters?: ImageCanvasFilters): string;
|
|
28
|
+
/**
|
|
29
|
+
* Convert a screen-space (canvas-relative) point into image-space
|
|
30
|
+
* coordinates given the current zoom + pan and the source image
|
|
31
|
+
* dimensions. Useful for tools that need to know which image pixel a
|
|
32
|
+
* pointer event hit (cropping, picking, masking).
|
|
33
|
+
*
|
|
34
|
+
* @param point - The canvas-space point in CSS pixels (origin top-left
|
|
35
|
+
* of the canvas element).
|
|
36
|
+
* @param canvasSize - The canvas size in CSS pixels.
|
|
37
|
+
* @param imageSize - The source image's natural size in pixels.
|
|
38
|
+
* @param zoom - The current zoom factor. Non-positive values fall back
|
|
39
|
+
* to `1`.
|
|
40
|
+
* @param pan - The current pan offset in CSS pixels.
|
|
41
|
+
* @returns The corresponding point in image-space coordinates.
|
|
42
|
+
*/
|
|
43
|
+
export declare function screenToCanvas(point: {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
}, canvasSize: {
|
|
47
|
+
width: number;
|
|
48
|
+
height: number;
|
|
49
|
+
}, imageSize: {
|
|
50
|
+
width: number;
|
|
51
|
+
height: number;
|
|
52
|
+
}, zoom: number, pan: PanOffset): {
|
|
53
|
+
x: number;
|
|
54
|
+
y: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Clamp a pan offset so the (zoomed) image cannot be dragged entirely
|
|
58
|
+
* off-canvas. The clamp keeps at least half of the image visible in
|
|
59
|
+
* each axis. Non-finite inputs are treated as `0`.
|
|
60
|
+
*
|
|
61
|
+
* @param pan - The desired pan offset, in CSS pixels.
|
|
62
|
+
* @param canvasSize - The canvas size in CSS pixels.
|
|
63
|
+
* @param imageSize - The source image's natural size in pixels.
|
|
64
|
+
* @param zoom - The current zoom factor. Non-positive values fall back
|
|
65
|
+
* to `1`.
|
|
66
|
+
* @returns The clamped pan offset.
|
|
67
|
+
*/
|
|
68
|
+
export declare function clampPan(pan: PanOffset, canvasSize: {
|
|
69
|
+
width: number;
|
|
70
|
+
height: number;
|
|
71
|
+
}, imageSize: {
|
|
72
|
+
width: number;
|
|
73
|
+
height: number;
|
|
74
|
+
}, zoom: number): PanOffset;
|
|
75
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,kBAAkB,EAAE,SAAS,EAAE,MAAM,YAAY,CAAA;AAE/D;;;;;;;;GAQG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAKrE;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,CAAC,EAAE,kBAAkB,GAAG,MAAM,CAuCxE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,EAC/B,UAAU,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,SAAS,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC5C,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,SAAS,GACb;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAa1B;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,QAAQ,CACtB,GAAG,EAAE,SAAS,EACd,UAAU,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC7C,SAAS,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,EAC5C,IAAI,EAAE,MAAM,GACX,SAAS,CAgBX"}
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free helpers of the Solid Canvas2D image-editing surface — the same
|
|
3
|
+
* values `@molecule/app-feature-image-canvas-react` exports.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Clamp a numeric value into the closed interval `[min, max]`. Returns
|
|
9
|
+
* `min` for non-finite inputs.
|
|
10
|
+
*
|
|
11
|
+
* @param value - The value to clamp.
|
|
12
|
+
* @param min - Inclusive minimum.
|
|
13
|
+
* @param max - Inclusive maximum.
|
|
14
|
+
* @returns The clamped value.
|
|
15
|
+
*/
|
|
16
|
+
export function clamp(value, min, max) {
|
|
17
|
+
if (!Number.isFinite(value))
|
|
18
|
+
return min;
|
|
19
|
+
if (value < min)
|
|
20
|
+
return min;
|
|
21
|
+
if (value > max)
|
|
22
|
+
return max;
|
|
23
|
+
return value;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Compose a CSS filter string from a set of `ImageCanvasFilters` values.
|
|
27
|
+
* Out-of-range values are clamped; omitted / non-finite fields are
|
|
28
|
+
* ignored. Returns `'none'` when no filters are active so callers can
|
|
29
|
+
* assign the result directly to `CanvasRenderingContext2D.filter`.
|
|
30
|
+
*
|
|
31
|
+
* @param filters - The filter parameters. May be `undefined`.
|
|
32
|
+
* @returns A CSS filter string, or `'none'` when nothing applies.
|
|
33
|
+
*/
|
|
34
|
+
export function composeFilterString(filters) {
|
|
35
|
+
if (!filters)
|
|
36
|
+
return 'none';
|
|
37
|
+
const parts = [];
|
|
38
|
+
if (filters.brightness !== undefined && Number.isFinite(filters.brightness)) {
|
|
39
|
+
const v = Math.max(0, filters.brightness);
|
|
40
|
+
if (v !== 1)
|
|
41
|
+
parts.push(`brightness(${v})`);
|
|
42
|
+
}
|
|
43
|
+
if (filters.contrast !== undefined && Number.isFinite(filters.contrast)) {
|
|
44
|
+
const v = Math.max(0, filters.contrast);
|
|
45
|
+
if (v !== 1)
|
|
46
|
+
parts.push(`contrast(${v})`);
|
|
47
|
+
}
|
|
48
|
+
if (filters.saturation !== undefined && Number.isFinite(filters.saturation)) {
|
|
49
|
+
const v = Math.max(0, filters.saturation);
|
|
50
|
+
if (v !== 1)
|
|
51
|
+
parts.push(`saturate(${v})`);
|
|
52
|
+
}
|
|
53
|
+
if (filters.hue !== undefined && Number.isFinite(filters.hue)) {
|
|
54
|
+
const v = ((filters.hue % 360) + 360) % 360;
|
|
55
|
+
if (v !== 0)
|
|
56
|
+
parts.push(`hue-rotate(${v}deg)`);
|
|
57
|
+
}
|
|
58
|
+
if (filters.sepia !== undefined && Number.isFinite(filters.sepia)) {
|
|
59
|
+
const v = clamp(filters.sepia, 0, 1);
|
|
60
|
+
if (v > 0)
|
|
61
|
+
parts.push(`sepia(${v})`);
|
|
62
|
+
}
|
|
63
|
+
if (filters.grayscale !== undefined && Number.isFinite(filters.grayscale)) {
|
|
64
|
+
const v = clamp(filters.grayscale, 0, 1);
|
|
65
|
+
if (v > 0)
|
|
66
|
+
parts.push(`grayscale(${v})`);
|
|
67
|
+
}
|
|
68
|
+
if (filters.blur !== undefined && Number.isFinite(filters.blur)) {
|
|
69
|
+
const v = Math.max(0, filters.blur);
|
|
70
|
+
if (v > 0)
|
|
71
|
+
parts.push(`blur(${v}px)`);
|
|
72
|
+
}
|
|
73
|
+
if (filters.sharpen !== undefined && Number.isFinite(filters.sharpen)) {
|
|
74
|
+
const v = clamp(filters.sharpen, 0, 1);
|
|
75
|
+
// Approximate sharpen as a small contrast bump. 1.0 = +50% contrast.
|
|
76
|
+
if (v > 0)
|
|
77
|
+
parts.push(`contrast(${1 + 0.5 * v})`);
|
|
78
|
+
}
|
|
79
|
+
return parts.length === 0 ? 'none' : parts.join(' ');
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Convert a screen-space (canvas-relative) point into image-space
|
|
83
|
+
* coordinates given the current zoom + pan and the source image
|
|
84
|
+
* dimensions. Useful for tools that need to know which image pixel a
|
|
85
|
+
* pointer event hit (cropping, picking, masking).
|
|
86
|
+
*
|
|
87
|
+
* @param point - The canvas-space point in CSS pixels (origin top-left
|
|
88
|
+
* of the canvas element).
|
|
89
|
+
* @param canvasSize - The canvas size in CSS pixels.
|
|
90
|
+
* @param imageSize - The source image's natural size in pixels.
|
|
91
|
+
* @param zoom - The current zoom factor. Non-positive values fall back
|
|
92
|
+
* to `1`.
|
|
93
|
+
* @param pan - The current pan offset in CSS pixels.
|
|
94
|
+
* @returns The corresponding point in image-space coordinates.
|
|
95
|
+
*/
|
|
96
|
+
export function screenToCanvas(point, canvasSize, imageSize, zoom, pan) {
|
|
97
|
+
const safeZoom = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
98
|
+
const cx = canvasSize.width / 2;
|
|
99
|
+
const cy = canvasSize.height / 2;
|
|
100
|
+
// The image is drawn centred at (cx + pan.x, cy + pan.y) and scaled by
|
|
101
|
+
// `safeZoom` from its natural size. Inverting that mapping yields the
|
|
102
|
+
// image-space pixel for an arbitrary canvas-space point.
|
|
103
|
+
const dx = point.x - cx - pan.x;
|
|
104
|
+
const dy = point.y - cy - pan.y;
|
|
105
|
+
return {
|
|
106
|
+
x: dx / safeZoom + imageSize.width / 2,
|
|
107
|
+
y: dy / safeZoom + imageSize.height / 2,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Clamp a pan offset so the (zoomed) image cannot be dragged entirely
|
|
112
|
+
* off-canvas. The clamp keeps at least half of the image visible in
|
|
113
|
+
* each axis. Non-finite inputs are treated as `0`.
|
|
114
|
+
*
|
|
115
|
+
* @param pan - The desired pan offset, in CSS pixels.
|
|
116
|
+
* @param canvasSize - The canvas size in CSS pixels.
|
|
117
|
+
* @param imageSize - The source image's natural size in pixels.
|
|
118
|
+
* @param zoom - The current zoom factor. Non-positive values fall back
|
|
119
|
+
* to `1`.
|
|
120
|
+
* @returns The clamped pan offset.
|
|
121
|
+
*/
|
|
122
|
+
export function clampPan(pan, canvasSize, imageSize, zoom) {
|
|
123
|
+
const safeZoom = Number.isFinite(zoom) && zoom > 0 ? zoom : 1;
|
|
124
|
+
const halfImgW = (imageSize.width * safeZoom) / 2;
|
|
125
|
+
const halfImgH = (imageSize.height * safeZoom) / 2;
|
|
126
|
+
const halfCanvasW = canvasSize.width / 2;
|
|
127
|
+
const halfCanvasH = canvasSize.height / 2;
|
|
128
|
+
// Allow the image centre to move up to (halfCanvas + halfImg/2) — i.e.
|
|
129
|
+
// half the image always stays visible.
|
|
130
|
+
const maxX = halfCanvasW + halfImgW / 2;
|
|
131
|
+
const maxY = halfCanvasH + halfImgH / 2;
|
|
132
|
+
const px = Number.isFinite(pan.x) ? pan.x : 0;
|
|
133
|
+
const py = Number.isFinite(pan.y) ? pan.y : 0;
|
|
134
|
+
return {
|
|
135
|
+
x: clamp(px, -maxX, maxX),
|
|
136
|
+
y: clamp(py, -maxY, maxY),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.js","sourceRoot":"","sources":["../src/helpers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH;;;;;;;;GAQG;AACH,MAAM,UAAU,KAAK,CAAC,KAAa,EAAE,GAAW,EAAE,GAAW;IAC3D,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,GAAG,CAAA;IACvC,IAAI,KAAK,GAAG,GAAG;QAAE,OAAO,GAAG,CAAA;IAC3B,IAAI,KAAK,GAAG,GAAG;QAA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EZ,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7D,MAAM,QAAQ,GAAG,CAAC,SAAS,CAAC,KAAK,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAA;IACjD,MAAM,QAAQ,GAAG,CAAC,SAAS,CAAC,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAA;IAClD,MAAM,WAAW,GAAG,UAAU,CAAC,KAAK,GAAG,CAAC,CAAA;IACxC,MAAM,WAAW,GAAG,UAAU,CAAC,MAAM,GAAG,CAAC,CAAA;IACzC,uEAAuE;IACvE,uCAAuC;IACvC,MAAM,IAAI,GAAG,WAAW,GAAG,QAAQ,GAAG,CAAC,CAAA;IACvC,MAAM,IAAI,GAAG,WAAW,GAAG,QAAQ,GAAG,CAAC,CAAA;IACvC,MAAM,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7C,MAAM,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;IAC7C,OAAO;QACL,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;QACzB,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC;KAC1B,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal source guard of the Solid Canvas2D image-editing surface: whether
|
|
3
|
+
* the `src` prop is an already-loaded element rather than a URL string.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Determine whether a `src` prop is an `HTMLImageElement` instance. The
|
|
9
|
+
* constructor is tested at call time so jsdom's `HTMLImageElement` shim
|
|
10
|
+
* works in tests.
|
|
11
|
+
*
|
|
12
|
+
* @param src - The `src` value to inspect.
|
|
13
|
+
* @returns `true` if `src` is an `HTMLImageElement`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function isImageElement(src: string | HTMLImageElement): src is HTMLImageElement;
|
|
16
|
+
//# sourceMappingURL=image-source.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-source.d.ts","sourceRoot":"","sources":["../src/image-source.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;GAOG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,gBAAgB,GAAG,GAAG,IAAI,gBAAgB,CAItF"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal source guard of the Solid Canvas2D image-editing surface: whether
|
|
3
|
+
* the `src` prop is an already-loaded element rather than a URL string.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Determine whether a `src` prop is an `HTMLImageElement` instance. The
|
|
9
|
+
* constructor is tested at call time so jsdom's `HTMLImageElement` shim
|
|
10
|
+
* works in tests.
|
|
11
|
+
*
|
|
12
|
+
* @param src - The `src` value to inspect.
|
|
13
|
+
* @returns `true` if `src` is an `HTMLImageElement`.
|
|
14
|
+
*/
|
|
15
|
+
export function isImageElement(src) {
|
|
16
|
+
if (typeof src === 'string')
|
|
17
|
+
return false;
|
|
18
|
+
if (typeof HTMLImageElement === 'undefined')
|
|
19
|
+
return false;
|
|
20
|
+
return src instanceof HTMLImageElement;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=image-source.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-source.js","sourceRoot":"","sources":["../src/image-source.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH;;;;;;;GAOG;AACH,MAAM,UAAU,cAAc,CAAC,GAA8B;IAC3D,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IACzC,IAAI,OAAO,gBAAgB,KAAK,WAAW;QAAE,OAAO,KAAK,CAAA;IACzD,OAAO,GAAG,YAAY,gBAAgB,CAAA;AACxC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solid Canvas2D image-editing surface.
|
|
3
|
+
*
|
|
4
|
+
* Exports `ImageCanvas` — the Solid counterpart of
|
|
5
|
+
* `@molecule/app-feature-image-canvas-react` with the same props and the same
|
|
6
|
+
* DOM: an image rendered onto a canvas with a filter chain (brightness,
|
|
7
|
+
* contrast, saturation, hue, sepia, grayscale, blur, sharpen), pointer-drag
|
|
8
|
+
* panning, wheel zooming, and a `toDataURL()` export handle, plus the
|
|
9
|
+
* framework-free `clamp`, `composeFilterString`, `screenToCanvas` and
|
|
10
|
+
* `clampPan` helpers.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* import { createSignal } from 'solid-js'
|
|
15
|
+
* import {
|
|
16
|
+
* ImageCanvas,
|
|
17
|
+
* type ImageCanvasExportHandle,
|
|
18
|
+
* } from '@molecule/app-feature-image-canvas-solid'
|
|
19
|
+
*
|
|
20
|
+
* export function Editor() {
|
|
21
|
+
* const [zoom, setZoom] = createSignal(1)
|
|
22
|
+
* const [pan, setPan] = createSignal({ x: 0, y: 0 })
|
|
23
|
+
* const exportRef: { current: ImageCanvasExportHandle | null } = { current: null }
|
|
24
|
+
* return (
|
|
25
|
+
* <ImageCanvas
|
|
26
|
+
* src="/photo.jpg"
|
|
27
|
+
* filters={{ brightness: 1.1, contrast: 1.2, sepia: 0.3 }}
|
|
28
|
+
* zoom={zoom()}
|
|
29
|
+
* pan={pan()}
|
|
30
|
+
* onChange={(next) => {
|
|
31
|
+
* setZoom(next.zoom)
|
|
32
|
+
* setPan(next.pan)
|
|
33
|
+
* }}
|
|
34
|
+
* exportRef={exportRef}
|
|
35
|
+
* />
|
|
36
|
+
* )
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* - Pan / zoom interaction is CONTROLLED-ONLY: pointer-drag and wheel events
|
|
42
|
+
* call `onChange` with the proposed `{ zoom, pan }` and never mutate
|
|
43
|
+
* internal state. Without `onChange` and a caller feeding the values back
|
|
44
|
+
* through the `zoom` / `pan` props, dragging and scrolling do nothing.
|
|
45
|
+
* - Requires a bonded ClassMap (the frame and the loading text use ClassMap
|
|
46
|
+
* tokens) and a bonded i18n provider for its aria labels and messages.
|
|
47
|
+
* - URL sources load with `crossOrigin="anonymous"` so `toDataURL()` is never
|
|
48
|
+
* blocked by a tainted canvas — remote images MUST be served with CORS
|
|
49
|
+
* headers. Same-origin and data-URL sources are unaffected.
|
|
50
|
+
* - The canvas bitmap is exactly `width` x `height` device-independent
|
|
51
|
+
* pixels — there is no devicePixelRatio upscaling.
|
|
52
|
+
*
|
|
53
|
+
* @module
|
|
54
|
+
*/
|
|
55
|
+
export * from './helpers.js';
|
|
56
|
+
export * from './ImageCanvas.jsx';
|
|
57
|
+
export * from './types.js';
|
|
58
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Solid Canvas2D image-editing surface.
|
|
3
|
+
*
|
|
4
|
+
* Exports `ImageCanvas` — the Solid counterpart of
|
|
5
|
+
* `@molecule/app-feature-image-canvas-react` with the same props and the same
|
|
6
|
+
* DOM: an image rendered onto a canvas with a filter chain (brightness,
|
|
7
|
+
* contrast, saturation, hue, sepia, grayscale, blur, sharpen), pointer-drag
|
|
8
|
+
* panning, wheel zooming, and a `toDataURL()` export handle, plus the
|
|
9
|
+
* framework-free `clamp`, `composeFilterString`, `screenToCanvas` and
|
|
10
|
+
* `clampPan` helpers.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* import { createSignal } from 'solid-js'
|
|
15
|
+
* import {
|
|
16
|
+
* ImageCanvas,
|
|
17
|
+
* type ImageCanvasExportHandle,
|
|
18
|
+
* } from '@molecule/app-feature-image-canvas-solid'
|
|
19
|
+
*
|
|
20
|
+
* export function Editor() {
|
|
21
|
+
* const [zoom, setZoom] = createSignal(1)
|
|
22
|
+
* const [pan, setPan] = createSignal({ x: 0, y: 0 })
|
|
23
|
+
* const exportRef: { current: ImageCanvasExportHandle | null } = { current: null }
|
|
24
|
+
* return (
|
|
25
|
+
* <ImageCanvas
|
|
26
|
+
* src="/photo.jpg"
|
|
27
|
+
* filters={{ brightness: 1.1, contrast: 1.2, sepia: 0.3 }}
|
|
28
|
+
* zoom={zoom()}
|
|
29
|
+
* pan={pan()}
|
|
30
|
+
* onChange={(next) => {
|
|
31
|
+
* setZoom(next.zoom)
|
|
32
|
+
* setPan(next.pan)
|
|
33
|
+
* }}
|
|
34
|
+
* exportRef={exportRef}
|
|
35
|
+
* />
|
|
36
|
+
* )
|
|
37
|
+
* }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @remarks
|
|
41
|
+
* - Pan / zoom interaction is CONTROLLED-ONLY: pointer-drag and wheel events
|
|
42
|
+
* call `onChange` with the proposed `{ zoom, pan }` and never mutate
|
|
43
|
+
* internal state. Without `onChange` and a caller feeding the values back
|
|
44
|
+
* through the `zoom` / `pan` props, dragging and scrolling do nothing.
|
|
45
|
+
* - Requires a bonded ClassMap (the frame and the loading text use ClassMap
|
|
46
|
+
* tokens) and a bonded i18n provider for its aria labels and messages.
|
|
47
|
+
* - URL sources load with `crossOrigin="anonymous"` so `toDataURL()` is never
|
|
48
|
+
* blocked by a tainted canvas — remote images MUST be served with CORS
|
|
49
|
+
* headers. Same-origin and data-URL sources are unaffected.
|
|
50
|
+
* - The canvas bitmap is exactly `width` x `height` device-independent
|
|
51
|
+
* pixels — there is no devicePixelRatio upscaling.
|
|
52
|
+
*
|
|
53
|
+
* @module
|
|
54
|
+
*/
|
|
55
|
+
export * from './helpers.js';
|
|
56
|
+
export * from './ImageCanvas.jsx';
|
|
57
|
+
export * from './types.js';
|
|
58
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AAEH,cAAc,cAAc,CAAA;AAC5B,cAAc,mBAAmB,CAAA;AACjC,cAAc,YAAY,CAAA"}
|