feat(cropper): expand image export API
This commit is contained in:
@@ -10,31 +10,25 @@ toc:
|
||||
## 示例 {#example}
|
||||
|
||||
```tsx
|
||||
import { Cropper, cropImage, type CropArea } from "@workspace/cropper"
|
||||
import { Cropper, type CropperHandle } from "@workspace/cropper"
|
||||
|
||||
function AvatarCropper({ src }: { src: string }) {
|
||||
const [area, setArea] = React.useState<CropArea>()
|
||||
const cropperRef = React.useRef<CropperHandle>(null)
|
||||
|
||||
async function saveAvatar() {
|
||||
if (!area) return
|
||||
const blob = await cropImage(src, area, {
|
||||
const blob = await cropperRef.current?.getCropImage({
|
||||
height: 512,
|
||||
shape: "circle",
|
||||
type: "image/png",
|
||||
width: 512,
|
||||
})
|
||||
await uploadAvatar(blob)
|
||||
if (blob instanceof Blob) 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}>
|
||||
<Cropper ref={cropperRef} src={src} shape="circle" className="size-72" />
|
||||
<button type="button" onClick={saveAvatar}>
|
||||
保存头像
|
||||
</button>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user