@molecule/app-feature-image-canvas-svelte 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 +319 -2
- package/dist/ImageCanvas.svelte +220 -0
- package/dist/ImageCanvas.svelte.d.ts +5 -0
- package/dist/ImageCanvas.svelte.d.ts.map +1 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.d.ts.map +1 -0
- package/dist/components.js +7 -0
- package/dist/helpers.d.ts +75 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +138 -0
- package/dist/image-source.d.ts +16 -0
- package/dist/image-source.d.ts.map +1 -0
- package/dist/image-source.js +21 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/types.d.ts +129 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/package.json +75 -4
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
ImageCanvas: the Svelte counterpart of
|
|
3
|
+
`@molecule/app-feature-image-canvas-react`'s `ImageCanvas` — a source image
|
|
4
|
+
rendered onto a canvas with a filter chain (brightness, contrast,
|
|
5
|
+
saturation, hue, sepia, grayscale, blur, sharpen), pointer-drag panning,
|
|
6
|
+
wheel zooming, and a `toDataURL()` export handle. Same props (React names),
|
|
7
|
+
same DOM for the same input; the `loading` and `error` messages are the
|
|
8
|
+
React package's translated text.
|
|
9
|
+
-->
|
|
10
|
+
<script lang="ts">
|
|
11
|
+
import { onMount } from 'svelte'
|
|
12
|
+
|
|
13
|
+
import { useTranslation } from '@molecule/app-svelte'
|
|
14
|
+
import { getClassMap } from '@molecule/app-ui'
|
|
15
|
+
|
|
16
|
+
import { clamp, clampPan, composeFilterString } from './helpers.js'
|
|
17
|
+
import { isImageElement } from './image-source.js'
|
|
18
|
+
import type { ImageCanvasProps, PanOffset } from './types.js'
|
|
19
|
+
|
|
20
|
+
let {
|
|
21
|
+
src,
|
|
22
|
+
filters,
|
|
23
|
+
zoom,
|
|
24
|
+
pan,
|
|
25
|
+
onChange,
|
|
26
|
+
exportRef,
|
|
27
|
+
width = 512,
|
|
28
|
+
height = 512,
|
|
29
|
+
className,
|
|
30
|
+
}: ImageCanvasProps = $props()
|
|
31
|
+
|
|
32
|
+
const cm = $derived(getClassMap())
|
|
33
|
+
const { t } = useTranslation()
|
|
34
|
+
|
|
35
|
+
let canvas: HTMLCanvasElement
|
|
36
|
+
|
|
37
|
+
// React's `useState(isImageElement(src) ? src : null)`: the initial value
|
|
38
|
+
// only — a later `src` write is picked up by the loading effect below.
|
|
39
|
+
// svelte-ignore state_referenced_locally
|
|
40
|
+
let internalImage = $state<HTMLImageElement | null>(isImageElement(src) ? src : null)
|
|
41
|
+
let loadError = $state<string | null>(null)
|
|
42
|
+
|
|
43
|
+
const safeZoom = $derived(zoom !== undefined && Number.isFinite(zoom) && zoom > 0 ? zoom : 1)
|
|
44
|
+
const safePan = $derived<PanOffset>({
|
|
45
|
+
x: Number.isFinite(pan?.x) ? (pan?.x as number) : 0,
|
|
46
|
+
y: Number.isFinite(pan?.y) ? (pan?.y as number) : 0,
|
|
47
|
+
})
|
|
48
|
+
const filterString = $derived(composeFilterString(filters))
|
|
49
|
+
|
|
50
|
+
// Drag state lives outside reactive state: handlers mutate it without
|
|
51
|
+
// re-rendering, exactly like React's ref.
|
|
52
|
+
let dragState: {
|
|
53
|
+
pointerId: number | null
|
|
54
|
+
startX: number
|
|
55
|
+
startY: number
|
|
56
|
+
startPan: PanOffset
|
|
57
|
+
} = { pointerId: null, startX: 0, startY: 0, startPan: { x: 0, y: 0 } }
|
|
58
|
+
|
|
59
|
+
// Load the source image when the prop is a URL string.
|
|
60
|
+
$effect(() => {
|
|
61
|
+
const current = src
|
|
62
|
+
if (typeof current !== 'string') {
|
|
63
|
+
if (isImageElement(current)) {
|
|
64
|
+
internalImage = current
|
|
65
|
+
loadError = null
|
|
66
|
+
}
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let cancelled = false
|
|
71
|
+
loadError = null
|
|
72
|
+
const img = new Image()
|
|
73
|
+
img.crossOrigin = 'anonymous'
|
|
74
|
+
img.onload = () => {
|
|
75
|
+
if (cancelled) return
|
|
76
|
+
internalImage = img
|
|
77
|
+
}
|
|
78
|
+
img.onerror = () => {
|
|
79
|
+
if (cancelled) return
|
|
80
|
+
loadError = current
|
|
81
|
+
internalImage = null
|
|
82
|
+
}
|
|
83
|
+
img.src = current
|
|
84
|
+
|
|
85
|
+
return () => {
|
|
86
|
+
cancelled = true
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// Re-paint whenever the image, viewport, or filter chain changes.
|
|
91
|
+
$effect(() => {
|
|
92
|
+
const image = internalImage
|
|
93
|
+
const zoomValue = safeZoom
|
|
94
|
+
const offset = safePan
|
|
95
|
+
const filter = filterString
|
|
96
|
+
const canvasWidth = width
|
|
97
|
+
const canvasHeight = height
|
|
98
|
+
const node = canvas
|
|
99
|
+
if (!node) return
|
|
100
|
+
|
|
101
|
+
const ctx = node.getContext('2d')
|
|
102
|
+
if (!ctx) return
|
|
103
|
+
|
|
104
|
+
ctx.save()
|
|
105
|
+
ctx.clearRect(0, 0, canvasWidth, canvasHeight)
|
|
106
|
+
|
|
107
|
+
if (!image) {
|
|
108
|
+
ctx.restore()
|
|
109
|
+
return
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
ctx.filter = filter
|
|
113
|
+
const drawWidth = image.naturalWidth * zoomValue
|
|
114
|
+
const drawHeight = image.naturalHeight * zoomValue
|
|
115
|
+
const dx = canvasWidth / 2 - drawWidth / 2 + offset.x
|
|
116
|
+
const dy = canvasHeight / 2 - drawHeight / 2 + offset.y
|
|
117
|
+
ctx.drawImage(image, dx, dy, drawWidth, drawHeight)
|
|
118
|
+
ctx.restore()
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
onMount(() => {
|
|
122
|
+
if (!exportRef) return
|
|
123
|
+
exportRef.current = {
|
|
124
|
+
toDataURL: (type, quality) => (canvas ? canvas.toDataURL(type, quality) : ''),
|
|
125
|
+
}
|
|
126
|
+
return () => {
|
|
127
|
+
if (exportRef) exportRef.current = null
|
|
128
|
+
}
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
/** Starts a drag, capturing the pointer for its duration. */
|
|
132
|
+
function handlePointerDown(event: PointerEvent): void {
|
|
133
|
+
if (!canvas) return
|
|
134
|
+
canvas.setPointerCapture(event.pointerId)
|
|
135
|
+
dragState = {
|
|
136
|
+
pointerId: event.pointerId,
|
|
137
|
+
startX: event.clientX,
|
|
138
|
+
startY: event.clientY,
|
|
139
|
+
startPan: { ...safePan },
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Reports the proposed pan while a drag is in progress. */
|
|
144
|
+
function handlePointerMove(event: PointerEvent): void {
|
|
145
|
+
const state = dragState
|
|
146
|
+
if (state.pointerId !== event.pointerId) return
|
|
147
|
+
const dx = event.clientX - state.startX
|
|
148
|
+
const dy = event.clientY - state.startY
|
|
149
|
+
const image = internalImage
|
|
150
|
+
const next = clampPan(
|
|
151
|
+
{ x: state.startPan.x + dx, y: state.startPan.y + dy },
|
|
152
|
+
{ width, height },
|
|
153
|
+
image ? { width: image.naturalWidth, height: image.naturalHeight } : { width, height },
|
|
154
|
+
safeZoom,
|
|
155
|
+
)
|
|
156
|
+
onChange?.({ zoom: safeZoom, pan: next })
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Ends a drag, releasing the capture it took. */
|
|
160
|
+
function endDrag(event: PointerEvent): void {
|
|
161
|
+
const state = dragState
|
|
162
|
+
if (state.pointerId !== event.pointerId) return
|
|
163
|
+
if (canvas && canvas.hasPointerCapture(event.pointerId)) {
|
|
164
|
+
canvas.releasePointerCapture(event.pointerId)
|
|
165
|
+
}
|
|
166
|
+
dragState = { pointerId: null, startX: 0, startY: 0, startPan: { x: 0, y: 0 } }
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Reports the zoomed factor for a wheel scroll (negative `deltaY` zooms in). */
|
|
170
|
+
function handleWheel(event: WheelEvent): void {
|
|
171
|
+
const factor = event.deltaY < 0 ? 1.1 : 1 / 1.1
|
|
172
|
+
const nextZoom = clamp(safeZoom * factor, 0.05, 32)
|
|
173
|
+
onChange?.({ zoom: nextZoom, pan: safePan })
|
|
174
|
+
}
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<div
|
|
178
|
+
role="region"
|
|
179
|
+
aria-label={t('imageCanvas.aria.region', {}, { defaultValue: 'Image canvas' })}
|
|
180
|
+
data-mol-id="image-canvas"
|
|
181
|
+
class={cm.cn(className)}
|
|
182
|
+
style:display="inline-block"
|
|
183
|
+
style:width="{width}px"
|
|
184
|
+
style:height="{height}px"
|
|
185
|
+
style:position="relative"
|
|
186
|
+
>
|
|
187
|
+
<canvas
|
|
188
|
+
bind:this={canvas}
|
|
189
|
+
{width}
|
|
190
|
+
{height}
|
|
191
|
+
aria-label={t('imageCanvas.aria.canvas', {}, { defaultValue: 'Drag to pan, scroll to zoom' })}
|
|
192
|
+
data-mol-id="image-canvas-surface"
|
|
193
|
+
data-loading={internalImage ? 'false' : loadError ? 'false' : 'true'}
|
|
194
|
+
data-error={loadError ? 'true' : 'false'}
|
|
195
|
+
style:display="block"
|
|
196
|
+
style:width="{width}px"
|
|
197
|
+
style:height="{height}px"
|
|
198
|
+
style:cursor={onChange ? 'grab' : 'default'}
|
|
199
|
+
style:touch-action="none"
|
|
200
|
+
onpointerdown={handlePointerDown}
|
|
201
|
+
onpointermove={handlePointerMove}
|
|
202
|
+
onpointerup={endDrag}
|
|
203
|
+
onpointercancel={endDrag}
|
|
204
|
+
onwheel={handleWheel}
|
|
205
|
+
></canvas>
|
|
206
|
+
{#if !internalImage && !loadError}
|
|
207
|
+
<p class={cm.cn(cm.textSize('xs'), cm.sp('p', 2))} data-mol-id="image-canvas-loading">
|
|
208
|
+
{t('imageCanvas.loading', {}, { defaultValue: 'Loading image…' })}
|
|
209
|
+
</p>
|
|
210
|
+
{/if}
|
|
211
|
+
{#if loadError}
|
|
212
|
+
<p
|
|
213
|
+
class={cm.cn(cm.textSize('xs'), cm.sp('p', 2))}
|
|
214
|
+
data-mol-id="image-canvas-error"
|
|
215
|
+
role="alert"
|
|
216
|
+
>
|
|
217
|
+
{t('imageCanvas.error', {}, { defaultValue: 'Image failed to load.' })}
|
|
218
|
+
</p>
|
|
219
|
+
{/if}
|
|
220
|
+
</div>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImageCanvas.svelte.d.ts","sourceRoot":"","sources":["../src/ImageCanvas.svelte"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,gBAAgB,EAAa,MAAM,YAAY,CAAC;AA0L9D,QAAA,MAAM,WAAW,sDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The package's components: one Svelte 5 component per React component in
|
|
3
|
+
* `@molecule/app-feature-image-canvas-react`, under the same React name.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
export { default as ImageCanvas } from './ImageCanvas.svelte';
|
|
8
|
+
//# sourceMappingURL=components.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"components.d.ts","sourceRoot":"","sources":["../src/components.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,sBAAsB,CAAA"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free helpers of the Svelte 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,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework-free helpers of the Svelte 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
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal source guard of the Svelte 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,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal source guard of the Svelte 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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte Canvas2D image-editing surface.
|
|
3
|
+
*
|
|
4
|
+
* Exports `ImageCanvas` — the Svelte 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
|
+
* ```svelte
|
|
14
|
+
* <script lang="ts">
|
|
15
|
+
* import {
|
|
16
|
+
* ImageCanvas,
|
|
17
|
+
* type ImageCanvasExportHandle,
|
|
18
|
+
* } from '@molecule/app-feature-image-canvas-svelte'
|
|
19
|
+
*
|
|
20
|
+
* let zoom = $state(1)
|
|
21
|
+
* let pan = $state({ x: 0, y: 0 })
|
|
22
|
+
* const exportRef: { current: ImageCanvasExportHandle | null } = { current: null }
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* <ImageCanvas
|
|
26
|
+
* src="/photo.jpg"
|
|
27
|
+
* filters={{ brightness: 1.1, contrast: 1.2, sepia: 0.3 }}
|
|
28
|
+
* {zoom}
|
|
29
|
+
* {pan}
|
|
30
|
+
* onChange={(next) => {
|
|
31
|
+
* zoom = next.zoom
|
|
32
|
+
* pan = next.pan
|
|
33
|
+
* }}
|
|
34
|
+
* {exportRef}
|
|
35
|
+
* />
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - Pan / zoom interaction is CONTROLLED-ONLY: pointer-drag and wheel events
|
|
40
|
+
* call `onChange` with the proposed `{ zoom, pan }` and never mutate
|
|
41
|
+
* internal state. Without `onChange` and a caller feeding the values back
|
|
42
|
+
* through the `zoom` / `pan` props, dragging and scrolling do nothing.
|
|
43
|
+
* - Requires a bonded ClassMap (the frame and the loading text use ClassMap
|
|
44
|
+
* tokens) and a bonded i18n provider for its aria labels and messages.
|
|
45
|
+
* - URL sources load with `crossOrigin="anonymous"` so `toDataURL()` is never
|
|
46
|
+
* blocked by a tainted canvas — remote images MUST be served with CORS
|
|
47
|
+
* headers. Same-origin and data-URL sources are unaffected.
|
|
48
|
+
* - The canvas bitmap is exactly `width` x `height` device-independent
|
|
49
|
+
* pixels — there is no devicePixelRatio upscaling.
|
|
50
|
+
*
|
|
51
|
+
* @module
|
|
52
|
+
*/
|
|
53
|
+
export * from './components.js';
|
|
54
|
+
export * from './helpers.js';
|
|
55
|
+
export * from './types.js';
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmDG;AAEH,cAAc,iBAAiB,CAAA;AAC/B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Svelte Canvas2D image-editing surface.
|
|
3
|
+
*
|
|
4
|
+
* Exports `ImageCanvas` — the Svelte 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
|
+
* ```svelte
|
|
14
|
+
* <script lang="ts">
|
|
15
|
+
* import {
|
|
16
|
+
* ImageCanvas,
|
|
17
|
+
* type ImageCanvasExportHandle,
|
|
18
|
+
* } from '@molecule/app-feature-image-canvas-svelte'
|
|
19
|
+
*
|
|
20
|
+
* let zoom = $state(1)
|
|
21
|
+
* let pan = $state({ x: 0, y: 0 })
|
|
22
|
+
* const exportRef: { current: ImageCanvasExportHandle | null } = { current: null }
|
|
23
|
+
* </script>
|
|
24
|
+
*
|
|
25
|
+
* <ImageCanvas
|
|
26
|
+
* src="/photo.jpg"
|
|
27
|
+
* filters={{ brightness: 1.1, contrast: 1.2, sepia: 0.3 }}
|
|
28
|
+
* {zoom}
|
|
29
|
+
* {pan}
|
|
30
|
+
* onChange={(next) => {
|
|
31
|
+
* zoom = next.zoom
|
|
32
|
+
* pan = next.pan
|
|
33
|
+
* }}
|
|
34
|
+
* {exportRef}
|
|
35
|
+
* />
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @remarks
|
|
39
|
+
* - Pan / zoom interaction is CONTROLLED-ONLY: pointer-drag and wheel events
|
|
40
|
+
* call `onChange` with the proposed `{ zoom, pan }` and never mutate
|
|
41
|
+
* internal state. Without `onChange` and a caller feeding the values back
|
|
42
|
+
* through the `zoom` / `pan` props, dragging and scrolling do nothing.
|
|
43
|
+
* - Requires a bonded ClassMap (the frame and the loading text use ClassMap
|
|
44
|
+
* tokens) and a bonded i18n provider for its aria labels and messages.
|
|
45
|
+
* - URL sources load with `crossOrigin="anonymous"` so `toDataURL()` is never
|
|
46
|
+
* blocked by a tainted canvas — remote images MUST be served with CORS
|
|
47
|
+
* headers. Same-origin and data-URL sources are unaffected.
|
|
48
|
+
* - The canvas bitmap is exactly `width` x `height` device-independent
|
|
49
|
+
* pixels — there is no devicePixelRatio upscaling.
|
|
50
|
+
*
|
|
51
|
+
* @module
|
|
52
|
+
*/
|
|
53
|
+
export * from './components.js';
|
|
54
|
+
export * from './helpers.js';
|
|
55
|
+
export * from './types.js';
|