Files
simple-react-app-kit/packages/cropper/docs/zh-Hans/guide/basic-usage.mdx
T
2026-09-29 17:46:16 +08:00

51 lines
1.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 基础使用
description: 添加样式入口并创建一个可交互的图片裁剪器。
order: 10
toc:
- id: stylesheet
title: 样式入口
- id: cropper
title: 裁剪器
- id: controls
title: 外部控件
---
## 样式入口 {#stylesheet}
在应用的 Tailwind 样式入口中添加:
```css
@import "@workspace/cropper/globals.css";
```
## 裁剪器 {#cropper}
可以使用 class,也可以直接传入 `width`、`height`。`aspectRatio` 接受数字或 `"宽:高"` 字符串:
```tsx
import { Cropper, type CropArea } from "@workspace/cropper"
function ProductImageCropper({ src }: { src: string }) {
const [area, setArea] = React.useState<CropArea>()
return (
<Cropper
src={src}
alt="商品图片"
aspectRatio="4:3"
className="w-full max-w-xl rounded-xl"
onCropAreaChange={setArea}
/>
)
}
```
用户可以拖拽图片、滚动缩放,或者在 Cropper 聚焦时使用方向键、`+`、`-`、`0` 和 `Home`。
`src` 也可以直接传入文件选择器获得的 `Blob` 或 `File`。组件会创建临时 object URL,并在图片改变或卸载时自动释放。
## 外部控件 {#controls}
通过 `value` 和 `onValueChange` 控制裁剪状态,即可接入 Slider。快速重置可以直接调用组件 ref 的 `reset()`;`minZoom` 最小为 `1`,保证图片始终覆盖整个视口。