Data & Media
@hacline/ui/image-cropperImageCropper
Pan, zoom, and rotate a crop region over an image.
Usage
Drag the selection or resize the bottom-right handle, and adjust zoom or rotation. Current dimensions come from the image pixel coordinates provided by onCropChange, and cropImage exports the selection as a file.
"use client";
import { useState } from "react";
import {
ImageCropper,
ImageCropperGrid,
ImageCropperHandle,
ImageCropperImage,
ImageCropperRotate,
ImageCropperSelection,
ImageCropperViewport,
ImageCropperZoom,
} from "@/components/ui/hacline/components/image-cropper";
import type { ImageCrop } from "@/components/ui/hacline/components/image-cropper";
export const Example = () => {
const [crop, setCrop] = useState<ImageCrop>();
return (
<ImageCropper
aria-label="Crop lake project cover"
aspectRatio={4 / 3}
className="max-w-sm"
onCropChange={setCrop}
src="/examples/alpine-lake.jpg"
>
<ImageCropperViewport>
<ImageCropperImage alt="Lake nestled between mountains" />
<ImageCropperSelection aria-label="Cover crop selection">
<ImageCropperGrid />
</ImageCropperSelection>
<ImageCropperHandle
aria-label="Resize bottom-right corner"
position="se"
/>
</ImageCropperViewport>
<div className="flex items-center gap-3">
<ImageCropperZoom aria-label="Cover zoom" />
<ImageCropperRotate>Rotate 90°</ImageCropperRotate>
</div>
{crop && (
<output className="text-hl-fg-muted text-xs">
Crop size: {Math.round(crop.width)} × {Math.round(crop.height)} px
</output>
)}
</ImageCropper>
);
};Exporting a Circular Avatar
Use shape="circle" to display a circular crop selection, then call cropImage to generate a PNG with transparent corners.
"use client";
import { useState } from "react";
import { Button } from "@/components/ui/hacline/components/button";
import {
cropImage,
ImageCropper,
ImageCropperImage,
ImageCropperSelection,
ImageCropperViewport,
ImageCropperZoom,
} from "@/components/ui/hacline/components/image-cropper";
import type { ImageCrop } from "@/components/ui/hacline/components/image-cropper";
const IMAGE = "/examples/alpine-lake.jpg";
const INITIAL_CROP: ImageCrop = { height: 400, width: 400, x: 280, y: 120 };
export const Example = () => {
const [crop, setCrop] = useState(INITIAL_CROP);
const [file, setFile] = useState<File>();
return (
<div className="grid w-full max-w-64 justify-items-start gap-2">
<ImageCropper
aria-label="Create circular avatar"
aspectRatio={1}
defaultCrop={INITIAL_CROP}
onCropChange={setCrop}
shape="circle"
src={IMAGE}
>
<ImageCropperViewport>
<ImageCropperImage alt="Lake photo used for avatar" />
<ImageCropperSelection
aria-label="Circular avatar selection"
className="rounded-full"
/>
</ImageCropperViewport>
<ImageCropperZoom aria-label="Avatar crop zoom" />
</ImageCropper>
<Button
onClick={async () => {
setFile(
await cropImage(IMAGE, crop, {
name: "avatar.png",
shape: "circle",
width: 128,
})
);
}}
size="sm"
variant="outline"
>
Export 128px PNG
</Button>
{file && (
<output className="text-hl-fg-muted text-xs">
Generated {file.name}, {Math.round(file.size / 1024)} KB
</output>
)}
</div>
);
};Parts
| Part | Description |
|---|---|
ImageCropperFlip | A button that mirrors the image, left to right or, with direction="vertical", top to bottom. |
ImageCropperGrid | Lines across the selection in thirds, for CSS to draw, with data-dragging during a drag. |
ImageCropperHandle | An edge or corner of the selection, by its position, that resizes it in a box. |
ImageCropperImage | The image, from the root's src, turned and flipped. Give it an alt. |
ImageCropperRotate | A button that turns the image a quarter clockwise. |
ImageCropperSelection | The crop. A pointer drags it in a box; arrows move it, plus and minus zoom, and R turns the image. |
ImageCropperViewport | The area the image shows in. Give it a size; its padding keeps the image off its edges. In a frame, a pointer pans the image and the wheel zooms it. |
ImageCropperZoom | A range input for the zoom, from 1 to maxZoom. |
Props
Prop
Type
View parts and state attributes in UI SDK.
The lake photo used in the examples is from Unsplash.
