fix(zhst/meta): 修改圈选重新渲染,透出重新绘制方法

This commit is contained in:
NICE CODE BY DEV 2024-05-13 19:36:45 +08:00
parent 3083ef7865
commit 0f0644495a
2 changed files with 59 additions and 35 deletions

View File

@ -99,6 +99,7 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
const currentShapeRef = useRef<any>(null) const currentShapeRef = useRef<any>(null)
const imageRef = useRef<HTMLDivElement>(null) const imageRef = useRef<HTMLDivElement>(null)
const viewerRef = useRef<any>(null) const viewerRef = useRef<any>(null)
const currentFabricRef = useRef<CanvasPro>(null)
// 自定义弹框 // 自定义弹框
const alginContainerRef: any = useRef(null); const alginContainerRef: any = useRef(null);
@ -116,16 +117,16 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
// 监听形状选择事件 // 监听形状选择事件
addEventListenerWrapper(imageRef.current, EVENT_SHAPE_SELECT, (e: { detail: any; }) => { addEventListenerWrapper(imageRef.current, EVENT_SHAPE_SELECT, (e: { detail: any; }) => {
// 选中的od // 选中的od
const id = e.detail; const id = e.detail;
if (id) { if (id) {
const selectRectData = odList!.filter(_od => _od.id === id)?.[0] const selectRectData = odList!.filter(_od => _od.id === id)?.[0]
const _data = percentToLength(selectRectData, viewerRef.current.canvas) const _data = percentToLength(selectRectData, viewerRef.current.canvas)
const imageRect = getImageDataByPosition( const imageRect = getImageDataByPosition(
{ x: _data.x, y: _data.y, w: _data.w, h: _data.h }, { x: _data.x, y: _data.y, w: _data.w, h: _data.h },
{ canvas: viewerRef.current.canvas } { canvas: viewerRef.current.canvas }
) )
id && onShapeSelected?.(id, { ..._data, imageRect, originData: selectRectData }) id && onShapeSelected?.(id, { ..._data, imageRect, originData: selectRectData })
} }
}) })
return () => { return () => {
@ -141,18 +142,20 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
// 初始化 - 编辑器 // 初始化 - 编辑器
useEffect(() => { useEffect(() => {
const _viewer = viewerRef?.current || {} const _viewer = viewerRef?.current || {}
// 判断是否可编辑 // 判断是否可编辑 - 非编辑态
if (!editAble) { if (!editAble) {
_viewer.clearShape()
// 判定是否存在od框 // 判定是否存在od框
odList && odList.forEach(_od => { odList && odList.forEach(_od => {
_viewer?.addShape?.(_od); _viewer?.addShape?.(_od);
}) })
return return
} else { } else {
_viewer?.clearShape?.(); // 编辑态
const { targetTransform = {} } = _viewer const { targetTransform = {} } = _viewer
if (type === 'rect') { if (type === 'rect') {
_viewer.clearShape()
currentShapeRef.current = initRect() currentShapeRef.current = initRect()
// 矩形 - 开始绘制实践 // 矩形 - 开始绘制实践
cropStartRef.current = addEventListenerWrapper(imageRef.current, EVENT_CROP_START, () => { cropStartRef.current = addEventListenerWrapper(imageRef.current, EVENT_CROP_START, () => {
@ -198,7 +201,7 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
const { containerData = {}, targetTransform = {} } = viewer const { containerData = {}, targetTransform = {} } = viewer
const imageSize = viewer.getImgSize() const imageSize = viewer.getImgSize()
let currentFabric: CanvasPro = new fabric.Canvas( currentFabricRef.current = new fabric.Canvas(
canvasRef.current, canvasRef.current,
{ {
backgroundColor: 'transparent', backgroundColor: 'transparent',
@ -207,6 +210,7 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
selection: false, selection: false,
} }
) )
const currentFabric = currentFabricRef.current
// 事件监听: 鼠标抬起事件 // 事件监听: 鼠标抬起事件
currentFabric.on('mouse:down', function(options) { currentFabric.on('mouse:down', function(options) {
@ -306,6 +310,10 @@ const CropperImage = forwardRef<CropperImageRefProps, CropperImageProps>((props,
viewerRef, viewerRef,
rotateTo: (val) => { rotateTo: (val) => {
viewerRef.current.rotateTo((pre: any) => pre + val) viewerRef.current.rotateTo((pre: any) => pre + val)
},
clearShape: () => {
viewerRef.current?.clearShape?.();
currentFabricRef.current?.clear()
} }
})); }));

View File

@ -8,6 +8,23 @@ export default () => {
const [editAble, setEditAble] = useState(false) const [editAble, setEditAble] = useState(false)
const cropperRef = useRef(null) const cropperRef = useRef(null)
const [imgUrl, setImgUrl] = useState('') const [imgUrl, setImgUrl] = useState('')
const [odList, setOdList] = useState([
{
"id": "123",
"x": 0.5519352,
"y": 0.2965385,
"w": 0.05185461,
"h": 0.24698898,
selectAble: false,
},
{
"id": "456",
"x": 0.58543766,
"y": 0.3203356,
"w": 0.052037954,
"h": 0.2664015
}
])
const [selectedItem, setSelectedItem] = useState<any>() const [selectedItem, setSelectedItem] = useState<any>()
return ( return (
@ -15,27 +32,8 @@ export default () => {
<Space> <Space>
<Button onClick={() => setType('line')}></Button> <Button onClick={() => setType('line')}></Button>
<Button onClick={() => setType('rect')}></Button> <Button onClick={() => setType('rect')}></Button>
<Button onClick={() => setEditAble(pre => !pre)}>{!editAble ? '不可编辑' : '可编辑'}</Button> <Button onClick={() => {
<Button setOdList([
onClick={() => {
console.log('cropperRef?.current', cropperRef?.current.rotateTo(90))
console.log('cropper', cropperRef?.current)
}}
></Button>
</Space>
<div style={{ width: 800, height: 600 }}>
<CropperImage
ref={cropperRef}
type={type}
odList={[
{
"id": "123",
"x": 0.5519352,
"y": 0.2965385,
"w": 0.05185461,
"h": 0.24698898,
selectAble: false,
},
{ {
"id": "456", "id": "456",
"x": 0.58543766, "x": 0.58543766,
@ -43,7 +41,25 @@ export default () => {
"w": 0.052037954, "w": 0.052037954,
"h": 0.2664015 "h": 0.2664015
} }
]} ])
}}></Button>
<Button onClick={() => setEditAble(pre => !pre)}>{!editAble ? '不可编辑' : '可编辑'}</Button>
<Button
onClick={() => {
cropperRef?.current.rotateTo(90)
}}
></Button>
<Button
onClick={() => {
cropperRef?.current.clearShape()
}}
></Button>
</Space>
<div style={{ width: 800, height: 600 }}>
<CropperImage
ref={cropperRef}
type={type}
odList={odList}
editAble={editAble} editAble={editAble}
url="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png" url="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
onMouseUp={data => console.log('箭头绘制结束:', data)} onMouseUp={data => console.log('箭头绘制结束:', data)}