|
@@ -30,6 +30,7 @@ import { Cropper } from '@douyinfe/semi-ui';
|
|
|
|
|
|
```jsx live=true dir=column noInline=true
|
|
|
import { Cropper, Button, RadioGroup, Radio } from '@douyinfe/semi-ui';
|
|
|
+import React, { useState, useRef, useCallback } from 'react';
|
|
|
|
|
|
const containerStyle = {
|
|
|
width: 550,
|
|
@@ -39,13 +40,12 @@ const containerStyle = {
|
|
|
|
|
|
function Demo() {
|
|
|
const ref = useRef(null);
|
|
|
- const [shape, setShape] = useState('rect');
|
|
|
+ const [shape, setShape] = useState('rect');
|
|
|
+ const [cropperUrl, setCropperUrl] = useState('');
|
|
|
|
|
|
const onButtonClick = useCallback(() => {
|
|
|
- const value = ref.current.getCropperCanvas();
|
|
|
- const previewContainer = document.getElementById('previewContainer');
|
|
|
- previewContainer.innerHTML = '';
|
|
|
- previewContainer.appendChild(value);
|
|
|
+ const canvas = ref.current.getCropperCanvas();
|
|
|
+ setCropperUrl(canvas.toDataURL());
|
|
|
}, []);
|
|
|
|
|
|
const onShapeChange = useCallback((e) => {
|
|
@@ -65,7 +65,8 @@ function Demo() {
|
|
|
shape={shape}
|
|
|
/>
|
|
|
<Button onClick={onButtonClick}>裁切</Button>
|
|
|
- <div id='previewContainer'/>
|
|
|
+ <br/><br/>
|
|
|
+ {cropperUrl && <img src={cropperUrl} style={{height: 400}}/>}
|
|
|
</>;
|
|
|
}
|
|
|
|
|
@@ -82,6 +83,7 @@ render(<Demo />)
|
|
|
|
|
|
```jsx live=true dir=column noInline=true
|
|
|
import { Cropper, Button, RadioGroup, Radio } from '@douyinfe/semi-ui';
|
|
|
+import React, { useState, useRef, useCallback } from 'react';
|
|
|
|
|
|
const containerStyle = {
|
|
|
width: 550,
|
|
@@ -91,13 +93,11 @@ const containerStyle = {
|
|
|
|
|
|
function Demo() {
|
|
|
const ref = useRef(null);
|
|
|
- const shape = useState('rect');
|
|
|
+ const [cropperUrl, setCropperUrl] = useState('');
|
|
|
|
|
|
const onButtonClick = useCallback(() => {
|
|
|
- const value = ref.current.getCropperCanvas();
|
|
|
- const previewContainer = document.getElementById('previewContainer-aspect');
|
|
|
- previewContainer.innerHTML = '';
|
|
|
- previewContainer.appendChild(value);
|
|
|
+ const canvas = ref.current.getCropperCanvas();
|
|
|
+ setCropperUrl(canvas.toDataURL());
|
|
|
}, []);
|
|
|
|
|
|
return <>
|
|
@@ -108,7 +108,8 @@ function Demo() {
|
|
|
style={containerStyle}
|
|
|
/>
|
|
|
<Button onClick={onButtonClick}>裁切</Button>
|
|
|
- <div id='previewContainer-aspect' />
|
|
|
+ <br /><br />
|
|
|
+ {cropperUrl && <img src={cropperUrl} style={{height: 400}}/>}
|
|
|
</>;
|
|
|
}
|
|
|
|
|
@@ -121,6 +122,7 @@ render(<Demo />)
|
|
|
|
|
|
```jsx live=true dir=column noInline=true
|
|
|
import { Cropper, Button, Slider } from '@douyinfe/semi-ui';
|
|
|
+import React, { useState, useRef, useCallback } from 'react';
|
|
|
|
|
|
const containerStyle = {
|
|
|
width: 550,
|
|
@@ -140,6 +142,7 @@ function Demo() {
|
|
|
const [rotate, setRotate] = useState(0);
|
|
|
const [zoom, setZoom] = useState(1);
|
|
|
const ref = useRef();
|
|
|
+ const [cropperUrl, setCropperUrl] = useState('');
|
|
|
|
|
|
const onZoomChange = useCallback((value) => {
|
|
|
setZoom(value);
|
|
@@ -150,10 +153,8 @@ function Demo() {
|
|
|
}, []);
|
|
|
|
|
|
const onButtonClick = useCallback(() => {
|
|
|
- const value = ref.current.getCropperCanvas();
|
|
|
- const previewContainer = document.getElementById('previewContainer-control');
|
|
|
- previewContainer.innerHTML = '';
|
|
|
- previewContainer.appendChild(value);
|
|
|
+ const canvas = ref.current.getCropperCanvas();
|
|
|
+ setCropperUrl(canvas.toDataURL());
|
|
|
}, []);
|
|
|
|
|
|
return (
|
|
@@ -167,7 +168,7 @@ function Demo() {
|
|
|
onZoomChange={onZoomChange}
|
|
|
/>
|
|
|
<div style={actionStyle} >
|
|
|
- <span>Rotate</span>
|
|
|
+ <span>旋转</span>
|
|
|
<Slider
|
|
|
style={{ width: 500}}
|
|
|
value={rotate}
|
|
@@ -178,7 +179,7 @@ function Demo() {
|
|
|
/>
|
|
|
</div>
|
|
|
<div style={actionStyle} >
|
|
|
- <span>Zoom</span>
|
|
|
+ <span>缩放</span>
|
|
|
<Slider
|
|
|
style={{ width: 500}}
|
|
|
value={zoom}
|
|
@@ -190,13 +191,8 @@ function Demo() {
|
|
|
</div>
|
|
|
<br />
|
|
|
<Button onClick={onButtonClick}>裁切</Button>
|
|
|
- <br />
|
|
|
- <div
|
|
|
- // style={{ background: 'pink' }}
|
|
|
- >
|
|
|
- <div id='previewContainer-control'
|
|
|
- />
|
|
|
- </div>
|
|
|
+ <br /><br />
|
|
|
+ {cropperUrl && <img src={cropperUrl} style={{height: 400}}/>}
|
|
|
</div>
|
|
|
);
|
|
|
};
|
|
@@ -210,6 +206,7 @@ render(<Demo />)
|
|
|
|
|
|
```jsx live=true dir=column noInline=true
|
|
|
import { Cropper, Button, Switch } from '@douyinfe/semi-ui';
|
|
|
+import React, { useState, useRef, useCallback } from 'react';
|
|
|
|
|
|
const containerStyle = {
|
|
|
width: 550,
|
|
@@ -226,12 +223,11 @@ const centerStyle = {
|
|
|
|
|
|
function Demo() {
|
|
|
const ref = useRef(null);
|
|
|
+ const [cropperUrl, setCropperUrl] = useState('');
|
|
|
|
|
|
const onButtonClick = useCallback(() => {
|
|
|
- const value = ref.current.getCropperCanvas();
|
|
|
- const previewContainer = document.getElementById('previewContainer-cropperBox');
|
|
|
- previewContainer.innerHTML = '';
|
|
|
- previewContainer.appendChild(value);
|
|
|
+ const canvas = ref.current.getCropperCanvas();
|
|
|
+ setCropperUrl(canvas.toDataURL());
|
|
|
}, []);
|
|
|
|
|
|
return <>
|
|
@@ -244,13 +240,113 @@ function Demo() {
|
|
|
showResizeBox={false}
|
|
|
/>
|
|
|
<Button onClick={onButtonClick}>裁切</Button>
|
|
|
- <div id='previewContainer-cropperBox'/>
|
|
|
+ <br /><br />
|
|
|
+ {cropperUrl && <img src={cropperUrl} style={{height: 400}}/>}
|
|
|
</>;
|
|
|
}
|
|
|
|
|
|
render(<Demo />)
|
|
|
```
|
|
|
|
|
|
+### 实时预览裁切效果
|
|
|
+
|
|
|
+通过 `preview` 指定预览容器,实时预览裁切效果。
|
|
|
+
|
|
|
+```jsx live=true dir=column noInline=true
|
|
|
+import { Cropper, Button, RadioGroup, Radio } from '@douyinfe/semi-ui';
|
|
|
+import React, { useState, useRef, useCallback } from 'react';
|
|
|
+
|
|
|
+const containerStyle = {
|
|
|
+ width: 550,
|
|
|
+ height: 300,
|
|
|
+ margin: 20,
|
|
|
+}
|
|
|
+
|
|
|
+const actionStyle = {
|
|
|
+ marginTop: 20,
|
|
|
+ display: 'flex',
|
|
|
+ alignItems: 'center',
|
|
|
+ justifyContent: 'center',
|
|
|
+ width: 'fit-content'
|
|
|
+}
|
|
|
+
|
|
|
+function Demo() {
|
|
|
+ const [rotate, setRotate] = useState(0);
|
|
|
+ const [zoom, setZoom] = useState(1);
|
|
|
+ const [cropperUrl, setCropperUrl ] = useState('');
|
|
|
+ const ref = useRef();
|
|
|
+
|
|
|
+ const onZoomChange = useCallback((value) => {
|
|
|
+ setZoom(value);
|
|
|
+ })
|
|
|
+
|
|
|
+ const onSliderChange = useCallback((value) => {
|
|
|
+ setRotate(value);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const onButtonClick = useCallback(() => {
|
|
|
+ const canvas = ref.current.getCropperCanvas();
|
|
|
+ const url = canvas.toDataURL();
|
|
|
+ setCropperUrl(url);
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ const preview = useCallback(() => {
|
|
|
+ const previewContainer = document.getElementById('previewWrapper');
|
|
|
+ return previewContainer;
|
|
|
+ }, []);
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div >
|
|
|
+ <Cropper
|
|
|
+ ref={ref}
|
|
|
+ src={"https://lf3-static.bytednsdoc.com/obj/eden-cn/ptlz_zlp/ljhwZthlaukjlkulzlp/root-web-sites/abstract.jpg"}
|
|
|
+ style={containerStyle}
|
|
|
+ rotate={rotate}
|
|
|
+ zoom={zoom}
|
|
|
+ onZoomChange={onZoomChange}
|
|
|
+ preview={preview}
|
|
|
+ />
|
|
|
+ <div style={actionStyle} >
|
|
|
+ <span>旋转</span>
|
|
|
+ <Slider
|
|
|
+ style={{ width: 500}}
|
|
|
+ value={rotate}
|
|
|
+ step={1}
|
|
|
+ min={-360}
|
|
|
+ max={360}
|
|
|
+ onChange={onSliderChange}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <div style={actionStyle} >
|
|
|
+ <span>缩放</span>
|
|
|
+ <Slider
|
|
|
+ style={{ width: 500}}
|
|
|
+ value={zoom}
|
|
|
+ step={0.1}
|
|
|
+ min={0.1}
|
|
|
+ max={3}
|
|
|
+ onChange={onZoomChange}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <br />
|
|
|
+ <div style={{ display: 'flex', }}>
|
|
|
+ <div style={{ width: '50%', flexGrow: 1}}>
|
|
|
+ <strong>实时预览</strong>
|
|
|
+ <div id='previewWrapper' style={{height: 300, marginTop: 8}}/>
|
|
|
+ </div>
|
|
|
+ <div style={{width: '50%', flexGrow: 1, paddingLeft: 10 }}>
|
|
|
+ <Button onClick={onButtonClick}>裁切</Button>
|
|
|
+ <br /><br />
|
|
|
+ <img src={cropperUrl} style={{ width: '90%'}} />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+};
|
|
|
+
|
|
|
+render(<Demo />)
|
|
|
+```
|
|
|
+
|
|
|
### API
|
|
|
|
|
|
| 属性 | 说明 | 类型 | 默认值 |
|
|
@@ -265,6 +361,7 @@ render(<Demo />)
|
|
|
| maxZoom | 最大缩放倍数 | number | 3 |
|
|
|
| minZoom | 最小缩放倍数 | number | 0.1 |
|
|
|
| onZoomChange | 缩放回调 | (zoom: number) => void | - |
|
|
|
+| preview | 指定预览容器 | () => HTMLElement | - |
|
|
|
| rotate | 旋转角度 | number | - |
|
|
|
| shape | 裁切框形状 | 'rect' \| 'round' \| 'roundRect' | 'rect' |
|
|
|
| src | 图片地址 | string | - |
|