Image Crop

Choose the part of a picture to keep: drag, pinch, scroll or arrow keys, in a fixed frame or a circle.

Drag or use the arrow keys to move. Pinch, scroll, or press plus and minus to zoom. Press 0 to start over.

Loading…

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/image-crop

Usage

import { ImageCrop } from "./ui/uicontext/components/upload/ImageCrop.ts"import { CropArea } from "./ui/uicontext/components/upload/crop.ts"
const Message = UiSchema.TaggedEnum({ CroppedCover: { area: CropArea } })// The crop is yours: keep `area` (in the image's pixels) and pass it back as `value`export const CoverCrop = (h: Html.H<typeof Message.Type>, src: string, area: CropArea | null) =>	ImageCrop(h, {		props: { src, label: "Cover image", value: area, aspect: 16 / 9 },		onEmit: { RequestedCrop: ({ area }) => Message.CroppedCover({ area }) },	})

API reference

Props

ImageCrop props
NameTypeDefaultDescription
srcrequiredstring—The picture: a URL, or an object URL for a file the person just chose
labelrequiredstring—What's being cropped, as the control's name: "Profile photo"
valuerequiredCropArea | null—The area kept, in the image's pixels; null to start centered
aspectnumber1The frame's width ÷ height; 1 by default
shape"rect" | "circle"—A circle mask for a profile photo (its aspect is 1)
maxZoomnumber4How far in it can go, from the whole picture; 4 by default
disabledboolean—Shows the crop and takes no changes

Events

ImageCrop events
EventPayloadDescription
RequestedCrop{ area: CropArea }The area to keep, in the image's pixels

Source