-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 添加编辑多边形plugin功能 * refactor: 使用mixinState.mSelectOneType判定编辑多边形按钮的显示与否 * feat: 添加动态创建多边形插件 * feat: 添加绘制多边形DrawPolygonPlugin * refactor: 抽离shiftAngle方法用于shiftKey的时候纠正角度 * refactor: 修改绘制polygon的交互逻辑 * feat: 添加shiftKey纠正点位和onEnd监听,并完善交互逻辑 * feat: 添加自由绘制功能 * refactor: 完善历史记录和绘制多边形结合交互 * refactor: 完善判定开启和关闭特殊tool时的副作用 * feat: 添加pathTextPlugin用户绘制路径文字 * feat: 添加裁切图片的功能 * refactor: 完善裁切shell的附带信息 * refactor: 选中裁切object时,逻辑状态改为取消选中状态 --------- Co-authored-by: weicheng.liang <[email protected]>
- Loading branch information
Showing
8 changed files
with
312 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { fabric } from 'fabric'; | ||
import Editor from '../Editor'; | ||
import { getPolygonVertices } from '../../../src/utils/math'; | ||
import { get, set } from 'lodash-es'; | ||
type IEditor = Editor; | ||
|
||
const getBounds = (activeObject: fabric.Object) => { | ||
const { left = 0, top = 0 } = activeObject; | ||
return { | ||
width: activeObject.getScaledWidth(), | ||
height: activeObject.getScaledHeight(), | ||
left, | ||
top, | ||
}; | ||
}; | ||
const bindInfo = (shell: fabric.Object, activeObject: fabric.Object) => { | ||
bindFlagToObject(shell); | ||
bindFlagToObject(shell, 'targetId', get(activeObject, 'id')); | ||
bindFlagToObject(shell, 'targetType', get(activeObject, 'type')); | ||
}; | ||
const bindFlagToObject = (activeObject: fabric.Object, key = 'clip', value: any = true) => { | ||
set(activeObject, key, value); | ||
}; | ||
const createRectClip = (activeObject: fabric.Object, inverted: boolean) => { | ||
const { width = 0, height = 0, left = 0, top = 0 } = getBounds(activeObject); | ||
const clipW = Math.round(width / 2); | ||
const clipH = Math.round(height / 2); | ||
const shell = new fabric.Rect({ | ||
width: clipW, | ||
height: clipH, | ||
fill: 'rgba(0,0,0,0)', | ||
originX: 'center', | ||
originY: 'center', | ||
left: left + width / 2, | ||
top: top + height / 2, | ||
}); | ||
bindInfo(shell, activeObject); | ||
const clipPath = new fabric.Rect({ | ||
absolutePositioned: true, | ||
width: shell.width, | ||
height: shell.height, | ||
originX: 'center', | ||
originY: 'center', | ||
left: shell.left, | ||
top: shell.top, | ||
inverted: inverted, | ||
}); | ||
return { clipPath, shell }; | ||
}; | ||
const createCircleClip = (activeObject: fabric.Object, inverted: boolean) => { | ||
const point = activeObject.getCenterPoint(); | ||
const { width } = getBounds(activeObject); | ||
const shell = new fabric.Circle({ | ||
fill: 'rgba(0,0,0,0)', | ||
originX: 'center', | ||
originY: 'center', | ||
left: point.x, | ||
top: point.y, | ||
radius: width / 2, | ||
}); | ||
bindInfo(shell, activeObject); | ||
const clipPath = new fabric.Circle({ | ||
absolutePositioned: true, | ||
originX: 'center', | ||
originY: 'center', | ||
left: shell.left, | ||
top: shell.top, | ||
radius: width / 2, | ||
inverted: inverted, | ||
}); | ||
return { shell, clipPath }; | ||
}; | ||
const createTriClip = (activeObject: fabric.Object, inverted: boolean) => { | ||
const point = activeObject.getCenterPoint(); | ||
const { width = 0, height = 0 } = getBounds(activeObject); | ||
const clipW = Math.round(width / 2); | ||
const clipH = Math.round(height / 2); | ||
const shell = new fabric.Triangle({ | ||
fill: 'rgba(0,0,0,0)', | ||
originX: 'center', | ||
originY: 'center', | ||
left: point.x, | ||
top: point.y, | ||
width: clipW, | ||
height: clipH, | ||
}); | ||
bindInfo(shell, activeObject); | ||
const clipPath = new fabric.Triangle({ | ||
absolutePositioned: true, | ||
originX: 'center', | ||
originY: 'center', | ||
left: shell.left, | ||
top: shell.top, | ||
width: shell.width, | ||
height: shell.height, | ||
inverted: inverted, | ||
}); | ||
return { shell, clipPath }; | ||
}; | ||
const createPolygonClip = (activeObject: fabric.Object, inverted: boolean) => { | ||
const point = activeObject.getCenterPoint(); | ||
const points = getPolygonVertices(5, 200); | ||
const shell = new fabric.Polygon(points, { | ||
fill: 'rgba(0,0,0,0)', | ||
originY: 'center', | ||
originX: 'center', | ||
left: point.x, | ||
top: point.y, | ||
}); | ||
bindInfo(shell, activeObject); | ||
const clipPath = new fabric.Polygon([...points], { | ||
absolutePositioned: true, | ||
originX: 'center', | ||
originY: 'center', | ||
left: shell.left, | ||
top: shell.top, | ||
inverted: inverted, | ||
}); | ||
return { shell, clipPath }; | ||
}; | ||
export default class SimpleClipImagePlugin { | ||
static pluginName = 'SimpleClipImagePlugin'; | ||
// static events = ['sizeChange']; | ||
static apis = ['addClipPathToImage', 'removeClip']; | ||
constructor(public canvas: fabric.Canvas, public editor: IEditor) {} | ||
addClipPathToImage(value: string) { | ||
const activeObject = this.canvas.getActiveObjects()[0]; | ||
if (activeObject && activeObject.type === 'image') { | ||
let clip: { shell: fabric.Object; clipPath: fabric.Object } | null = null; | ||
const [name, inverted] = value.split('-'); | ||
const isInverted = !!inverted; | ||
switch (name) { | ||
case 'polygon': | ||
clip = createPolygonClip(activeObject, isInverted); | ||
break; | ||
case 'rect': | ||
clip = createRectClip(activeObject, isInverted); | ||
break; | ||
case 'circle': | ||
clip = createCircleClip(activeObject, isInverted); | ||
break; | ||
case 'triangle': | ||
clip = createTriClip(activeObject, isInverted); | ||
break; | ||
} | ||
if (clip == null) return; | ||
const { shell, clipPath } = clip; | ||
shell.on('moving', () => { | ||
clipPath.setPositionByOrigin(shell.getCenterPoint(), 'center', 'center'); | ||
activeObject.set('dirty', true); | ||
}); | ||
shell.on('rotating', () => { | ||
clipPath.set({ angle: shell.angle }); | ||
activeObject.set('dirty', true); | ||
}); | ||
shell.on('scaling', () => { | ||
clipPath.set({ scaleX: shell.scaleX, scaleY: shell.scaleY }); | ||
clipPath.setPositionByOrigin(shell.getCenterPoint(), 'center', 'center'); | ||
activeObject.set('dirty', true); | ||
}); | ||
shell.on('deselected', () => { | ||
const position = activeObject.toLocalPoint(shell.getCenterPoint(), 'center', 'center'); | ||
clipPath.set({ | ||
absolutePositioned: false, | ||
left: position.x, | ||
top: position.y, | ||
}); | ||
if (clipPath instanceof fabric.Circle) { | ||
clipPath.set({ radius: (shell as fabric.Circle).getRadiusX(), scaleY: 1, scaleX: 1 }); | ||
} else { | ||
clipPath.set({ | ||
width: shell.getScaledWidth(), | ||
height: shell.getScaledHeight(), | ||
scaleX: 1, | ||
scaleY: 1, | ||
}); | ||
} | ||
this.canvas.remove(shell); | ||
this.canvas.requestRenderAll(); | ||
}); | ||
activeObject.set({ clipPath: clipPath }); | ||
this.canvas.add(shell); | ||
this.canvas.setActiveObject(shell); | ||
} | ||
} | ||
removeClip() { | ||
const activeObject = this.canvas.getActiveObjects()[0]; | ||
if (activeObject && activeObject.type === 'image') { | ||
activeObject.set({ clipPath: undefined }); | ||
activeObject.set('dirty', true); | ||
this.canvas.requestRenderAll(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<template> | ||
<div v-if="mixinState.mSelectMode === 'one' && type === 'image'" class="attr-item-box"> | ||
<div class="bg-item ivu-mb-8"> | ||
<Dropdown style="width: 270px" @on-click="addClipPath"> | ||
<Button type="text" long>{{ $t('createClip') }}</Button> | ||
<template #list> | ||
<DropdownMenu> | ||
<DropdownItem v-for="item in options" :key="item.value" :name="item.value"> | ||
{{ item.label }} | ||
</DropdownItem> | ||
</DropdownMenu> | ||
</template> | ||
</Dropdown> | ||
</div> | ||
<div class="bg-item"> | ||
<Button @click="removeClip" type="text" long>{{ $t('removeClip') }}</Button> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup name="ReplaceImg"> | ||
import useSelect from '@/hooks/select'; | ||
import { useI18n } from 'vue-i18n'; | ||
const update = getCurrentInstance(); | ||
// const canvasEditor = inject('canvasEditor'); | ||
const { mixinState, canvasEditor } = useSelect(); | ||
const { t } = useI18n(); | ||
const type = ref(''); | ||
const options = [ | ||
{ | ||
label: t('polygonClip'), | ||
value: 'polygon', | ||
}, | ||
{ | ||
label: t('rectClip'), | ||
value: 'rect', | ||
}, | ||
{ | ||
label: t('circleClip'), | ||
value: 'circle', | ||
}, | ||
{ | ||
label: t('triangleClip'), | ||
value: 'triangle', | ||
}, | ||
{ | ||
label: t('polygonClipInverted'), | ||
value: 'polygon-inverted', | ||
}, | ||
{ | ||
label: t('rectClipInverted'), | ||
value: 'rect-inverted', | ||
}, | ||
{ | ||
label: t('circleClipInverted'), | ||
value: 'circle-inverted', | ||
}, | ||
{ | ||
label: t('triangleClipInverted'), | ||
value: 'triangle-inverted', | ||
}, | ||
]; | ||
const addClipPath = async (name) => { | ||
canvasEditor.addClipPathToImage(name); | ||
}; | ||
const removeClip = () => { | ||
canvasEditor.removeClip(); | ||
}; | ||
const init = () => { | ||
const activeObject = canvasEditor.canvas.getActiveObjects()[0]; | ||
if (activeObject) { | ||
type.value = activeObject.type; | ||
update?.proxy?.$forceUpdate(); | ||
} | ||
}; | ||
onMounted(() => { | ||
canvasEditor.on('selectOne', init); | ||
}); | ||
onBeforeUnmount(() => { | ||
canvasEditor.off('selectOne', init); | ||
}); | ||
</script> | ||
<style lang="less" scoped> | ||
.attr-item-box { | ||
margin-top: 8px; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters