Image Crop
A canvas-based component for cropping images with zoom and pan support, providing a real-time cropped preview.
Props
Prop | Type | Default | Description |
|---|---|---|---|
sourceImage | ImageBitmap | required | The source ImageBitmap to be cropped. |
aspectRatio | { x: number, y: number } | required | Fixed aspect ratio for the crop area. |
resultImage | string | undefined | The base64 encoded cropped image result. Supports $bindable. |
zoom | number | 1 | Zoom level for the image. |
pan | { x: number, y: number } | { x: 0, y: 0 } | Pan coordinates (x, y) for the image. |
padding | number | 0 | Padding in pixels around the crop area within the canvas. |
shape | 'rectangle' | 'circle' | 'rectangle' | The shape of the crop area. |
overlayColor | string | 'rgba(0, 0, 0, 0.5)' | The color of the overlay around the crop area. |
variant | string | '' | Custom variant class for theming. |
componentId | string | undefined | The unique identifier for the component wrapper. |
Samples
Rectangle Crop (1:1)
Standard rectangular cropping with zoom, pan, and padding controls.
Rectangle
Loading image bitmap...
0
<script>1
import { ImageCrop } from 'fluid-ui-svelte/components';2
import { onMount } from 'svelte';3
4
let bitmap = $state<ImageBitmap>();5
6
onMount(async () => {7
const response = await fetch('https://picsum.photos/id/10/800/600');8
const blob = await response.blob();9
bitmap = await createImageBitmap(blob);10
});11
</script>12
13
{#if bitmap}14
<ImageCrop sourceImage={bitmap} aspectRatio={{ x: 16, y: 9 }} />15
{/if}File Upload
Select a local image file to crop. Supports custom aspect ratios and colors.
Rectangle
Please select an image to start cropping
0
<script>1
import { ImageCrop } from 'fluid-ui-svelte/components';2
3
let uploadedBitmap = $state<ImageBitmap>();4
let resultImage = $state<string>();5
6
async function handleFileChange(event) {7
const file = event.target.files?.[0];8
if (file) {9
const blob = new Blob([file], { type: file.type });10
uploadedBitmap = await createImageBitmap(blob);11
}12
}13
</script>14
15
<input type="file" accept="image/*" onchange={handleFileChange} />16
17
{#if uploadedBitmap}18
<ImageCrop 19
sourceImage={uploadedBitmap} 20
aspectRatio={{ x: 16, y: 9 }} 21
bind:resultImage 22
/>23
24
{#if resultImage}25
<img src={resultImage} alt="Cropped result" />26
{/if}27
{/if}