docs(cropper): add bilingual usage guides

This commit is contained in:
Maofeng
2026-09-29 17:09:39 +08:00
parent 23e0867027
commit 844acad316
8 changed files with 340 additions and 0 deletions
@@ -0,0 +1,49 @@
---
title: Geometry and export
description: Work with source coordinates, pure functions, and Canvas output.
order: 20
toc:
- id: controlled-state
title: Controlled state
- id: geometry
title: Geometry functions
- id: export
title: Exporting an image
- id: cross-origin
title: Cross-origin images
---
## Controlled state {#controlled-state}
```tsx
const [value, setValue] = React.useState({ x: 0, y: 0, zoom: 1 })
<Cropper
src={src}
value={value}
onValueChange={setValue}
onCropComplete={(area) => saveDraft(area)}
/>
```
`onCropAreaChange` runs whenever a valid crop changes. `onCropComplete` runs after drag or wheel interaction and after discrete keyboard operations, making it a better persistence boundary.
## Geometry functions {#geometry}
`getCoverScale`, `getCropperBounds`, `constrainCropperValue`, `getCropArea`, and `zoomCropperValueAtPoint` are DOM-independent pure functions that can be reused in custom interfaces and tests.
## Exporting an image {#export}
```tsx
const blob = await cropImage(src, area, {
type: "image/webp",
quality: 0.9,
width: 1200,
})
```
Supplying only `width` or `height` preserves the crop aspect ratio. `shape: "circle"` clips through an ellipse; provide `background` when an opaque JPEG background is required.
## Cross-origin images {#cross-origin}
A remote image server must return CORS headers that permit Canvas use. Pass `crossOrigin="anonymous"` to `Cropper` and the matching `crossOrigin` option to `cropImage`; otherwise the browser may taint the Canvas and prevent Blob encoding.
@@ -0,0 +1,43 @@
---
title: Avatar crop
description: Build a circular avatar preview and export a fixed-size PNG.
order: 30
toc:
- id: example
title: Example
---
## Example {#example}
```tsx
import { Cropper, cropImage, type CropArea } from "@workspace/cropper"
function AvatarCropper({ src }: { src: string }) {
const [area, setArea] = React.useState<CropArea>()
async function saveAvatar() {
if (!area) return
const blob = await cropImage(src, area, {
height: 512,
shape: "circle",
type: "image/png",
width: 512,
})
await uploadAvatar(blob)
}
return (
<div className="space-y-4">
<Cropper
src={src}
shape="circle"
className="size-72"
onCropAreaChange={setArea}
/>
<button type="button" disabled={!area} onClick={saveAvatar}>
Save avatar
</button>
</div>
)
}
```
@@ -0,0 +1,48 @@
---
title: Basic usage
description: Add the stylesheet source and create an interactive image cropper.
order: 10
toc:
- id: stylesheet
title: Stylesheet
- id: cropper
title: Cropper
- id: controls
title: External controls
---
## Stylesheet {#stylesheet}
Add the package source to the application's Tailwind stylesheet:
```css
@import "@workspace/cropper/globals.css";
```
## Cropper {#cropper}
Give the component an explicit width; `aspectRatio` determines its height:
```tsx
import { Cropper, type CropArea } from "@workspace/cropper"
function ProductImageCropper({ src }: { src: string }) {
const [area, setArea] = React.useState<CropArea>()
return (
<Cropper
src={src}
alt="Product"
aspectRatio={4 / 3}
className="w-full max-w-xl rounded-xl"
onCropAreaChange={setArea}
/>
)
}
```
Users can drag the image, scroll to zoom, or focus the Cropper and use the arrow, `+`, `-`, `0`, and `Home` keys.
## External controls {#controls}
Control the crop with `value` and `onValueChange` to connect a Slider or reset button. `minZoom` is clamped to at least `1`, ensuring that the image always covers the viewport.
+30
View File
@@ -0,0 +1,30 @@
---
title: Overview
description: Pan, zoom, calculate, and export image crops in React.
order: 1
toc:
- id: package-role
title: Package role
- id: capabilities
title: Capabilities
- id: data-model
title: Data model
---
## Package role {#package-role}
`@workspace/cropper` provides fixed-viewport image cropping. Users move and scale an image inside the viewport while the component reports the crop in source-image pixels. It owns interaction and geometry, but does not upload, store, or mutate the original file.
## Capabilities {#capabilities}
- Mouse and touch dragging.
- Pinch, wheel, double-click, and keyboard zooming.
- Controlled and uncontrolled state.
- Rectangular and circular viewports.
- Resize-aware image constraints.
- Standalone pure geometry functions.
- Canvas export to PNG, JPEG, or WebP Blob.
## Data model {#data-model}
In `CropperValue`, `x` and `y` are CSS-pixel offsets from the viewport center and `zoom` is relative to the cover fit. `CropArea` uses source-image pixels, so it can be sent to an image service or passed directly to `cropImage()` for browser-side export.