|
|
@@ -5,14 +5,7 @@
|
|
|
<script lang="ts" setup>
|
|
|
import Konva from 'konva';
|
|
|
import { ref, onMounted, onUnmounted } from 'vue';
|
|
|
- import {
|
|
|
- GROUP_NAME,
|
|
|
- POLYGON_NAME,
|
|
|
- Points,
|
|
|
- ServerLine,
|
|
|
- ToolObjectItem,
|
|
|
- toolObject,
|
|
|
- } from './constants';
|
|
|
+ import { GROUP_NAME, POLYGON_NAME, Points, ToolObjectItem, toolObject } from './constants';
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
import { getDefaultScale } from './utils';
|
|
|
import { Group } from 'konva/lib/Group';
|
|
|
@@ -20,6 +13,8 @@
|
|
|
const mapRef = ref<HTMLCanvasElement | null>(null);
|
|
|
let currentTool: ToolObjectItem = toolObject[1];
|
|
|
|
|
|
+ let isEdit = true;
|
|
|
+
|
|
|
let stage: Konva.Stage | null = null;
|
|
|
let layer: Konva.Layer | null = null;
|
|
|
let currentDrawingShape: Konva.Group | null = null; //现在绘画的图形
|
|
|
@@ -77,6 +72,7 @@
|
|
|
stage?.on('mousedown', (e) => {
|
|
|
//鼠标左键开始
|
|
|
console.log('stage mousedown', e);
|
|
|
+ if (!isEdit) return;
|
|
|
if (e.evt.button == 0) {
|
|
|
if (e.target === stage) {
|
|
|
stageMousedown(currentTool!);
|
|
|
@@ -100,6 +96,7 @@
|
|
|
});
|
|
|
//鼠标移动
|
|
|
stage?.on('mousemove', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
if (currentTool && drawing) {
|
|
|
//绘画中
|
|
|
stageMousemove();
|
|
|
@@ -107,6 +104,8 @@
|
|
|
});
|
|
|
//鼠标放开
|
|
|
stage?.on('mouseup', (e: Konva.KonvaEventObject<any>) => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
if (e.evt.button == 0) {
|
|
|
if (drawing) {
|
|
|
stageMouseup(e);
|
|
|
@@ -125,6 +124,8 @@
|
|
|
container.tabIndex = 1;
|
|
|
container?.focus();
|
|
|
container?.addEventListener('keydown', (e) => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
//删除的快捷键
|
|
|
if (e.keyCode === 46) {
|
|
|
removeCurrent();
|
|
|
@@ -143,7 +144,7 @@
|
|
|
};
|
|
|
|
|
|
/** 设置当前选中的group */
|
|
|
- function setCurrentGroup(group: Konva.Group) {
|
|
|
+ function setCurrentGroup(group: Konva.Group | null) {
|
|
|
currentDrawingShape = group;
|
|
|
setGroupActive(group);
|
|
|
currentDel = group;
|
|
|
@@ -155,6 +156,8 @@
|
|
|
* @param e 传入的event对象
|
|
|
*/
|
|
|
function stageMousedown(currentTool: ToolObjectItem) {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
console.log('stagemousedown');
|
|
|
|
|
|
//如果数组长度小于2,初始化多边形和顶点,使它们成为一组,否则什么都不做
|
|
|
@@ -221,6 +224,8 @@
|
|
|
* @param e 传入的event对象
|
|
|
*/
|
|
|
function stageMousemove() {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
const container = stage?.container();
|
|
|
if (!container) return;
|
|
|
|
|
|
@@ -252,6 +257,8 @@
|
|
|
* @param e 传入的event对象
|
|
|
*/
|
|
|
function stageMouseup(e: Konva.KonvaEventObject<any>) {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
if (e.evt.button == 2) {
|
|
|
if (polygonPoints.length != 0) {
|
|
|
//最好使用konva提供的鼠标xy点坐标
|
|
|
@@ -381,6 +388,8 @@
|
|
|
c.style.cursor = 'pointer';
|
|
|
});
|
|
|
circle.on('mousedown', (e) => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
console.log('circle,');
|
|
|
if (!drawing) {
|
|
|
circle.draggable(true);
|
|
|
@@ -392,11 +401,15 @@
|
|
|
e.cancelBubble = true;
|
|
|
});
|
|
|
circle.on('mouseleave', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
const c = stage?.container();
|
|
|
if (!c) return;
|
|
|
c.style.cursor = 'crosshair';
|
|
|
});
|
|
|
circle.on('dragstart', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
switch (currentTool?.type) {
|
|
|
case 'poly':
|
|
|
//查找拖拽了多边形的哪个点
|
|
|
@@ -416,6 +429,8 @@
|
|
|
}
|
|
|
});
|
|
|
circle.on('dragmove', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
switch (currentTool?.type) {
|
|
|
case 'poly':
|
|
|
var x = circle.x();
|
|
|
@@ -429,6 +444,8 @@
|
|
|
}
|
|
|
});
|
|
|
circle.on('dragend', (e) => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
switch (currentTool?.type) {
|
|
|
case 'poly':
|
|
|
//使所有顶点在顶层显示
|
|
|
@@ -459,7 +476,7 @@
|
|
|
}
|
|
|
|
|
|
/** 设置当前的group为active */
|
|
|
- function setGroupActive(activeGroup: Group) {
|
|
|
+ function setGroupActive(activeGroup: Group | null) {
|
|
|
const groups = stage?.find(GROUP_NAME) as Konva.Group[];
|
|
|
if (!groups) return;
|
|
|
/** 将其他组的线条设为非高亮 */
|
|
|
@@ -473,7 +490,7 @@
|
|
|
(circle as Konva.Circle).hide();
|
|
|
});
|
|
|
});
|
|
|
-
|
|
|
+ if (!activeGroup) return;
|
|
|
const thisLine = activeGroup.findOne(POLYGON_NAME) as Konva.Line;
|
|
|
if (thisLine) {
|
|
|
thisLine.stroke(currentTool.activeColor);
|
|
|
@@ -497,9 +514,9 @@
|
|
|
points: points,
|
|
|
/* fill: currentTool.color, */
|
|
|
stroke: currentTool.color,
|
|
|
- strokeWidth: 1,
|
|
|
+ strokeWidth: 3,
|
|
|
draggable: false,
|
|
|
- opacity: 0.5,
|
|
|
+ opacity: 1,
|
|
|
lineCap: 'round',
|
|
|
lineJoin: 'round',
|
|
|
closed: true,
|
|
|
@@ -509,12 +526,16 @@
|
|
|
setCurrentGroup(group);
|
|
|
const pParent = group;
|
|
|
pParent?.on('mouseleave', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
const c = stage?.container();
|
|
|
if (c) {
|
|
|
c.style.cursor = 'crosshair';
|
|
|
}
|
|
|
});
|
|
|
pParent?.on('mousedown', (e) => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
console.log('group mouse down');
|
|
|
if (e.evt.button == 0) {
|
|
|
//绘画结束
|
|
|
@@ -541,6 +562,8 @@
|
|
|
});
|
|
|
|
|
|
pParent?.on('dragend', () => {
|
|
|
+ if (!isEdit) return;
|
|
|
+
|
|
|
console.log('dragend');
|
|
|
/** 这里可以把工具的类型用枚举值定义 */
|
|
|
|
|
|
@@ -628,12 +651,24 @@
|
|
|
return stage?.toObject();
|
|
|
};
|
|
|
|
|
|
+ /** 退出编辑模式 */
|
|
|
+ const exitEditMode = () => {
|
|
|
+ setCurrentGroup(null);
|
|
|
+ isEdit = false;
|
|
|
+ };
|
|
|
+ /** 进入编辑模式 */
|
|
|
+ const setEditMode = () => {
|
|
|
+ isEdit = true;
|
|
|
+ };
|
|
|
+
|
|
|
defineExpose({
|
|
|
remove: removeCurrent,
|
|
|
toObject,
|
|
|
toRawObject,
|
|
|
createGroupByPoints,
|
|
|
initStageByJSON,
|
|
|
+ exitEditMode,
|
|
|
+ setEditMode,
|
|
|
});
|
|
|
</script>
|
|
|
|
|
|
@@ -644,6 +679,7 @@
|
|
|
top: 0;
|
|
|
width: 100%;
|
|
|
height: 100%;
|
|
|
- border: 2px solid #0f0;
|
|
|
+ z-index: 8;
|
|
|
+ /* border: 2px solid #0f0; */
|
|
|
}
|
|
|
</style>
|