|
|
@@ -13,84 +13,16 @@ export default {
|
|
|
layer: null,
|
|
|
selectedStates: [], // 用于存储每个元素的选中状态
|
|
|
selectedText: [], // 用于存储未选中的元素文本
|
|
|
+ rects:[],//白色rect合集
|
|
|
+ texts:[],//白色text合集
|
|
|
+ redrects:[],//红色rect合集
|
|
|
+ redtexts:[],//白色text合集
|
|
|
};
|
|
|
},
|
|
|
mounted() {
|
|
|
this.initKonva();
|
|
|
},
|
|
|
methods: {
|
|
|
- // initKonva() {
|
|
|
- // // 创建舞台
|
|
|
- // this.stage = new Konva.Stage({
|
|
|
- // container: this.$refs.container, // 容器元素
|
|
|
- // width: 1250,
|
|
|
- // height: 830,
|
|
|
- // fill: 'black'
|
|
|
- // })
|
|
|
-
|
|
|
- // // 创建图层
|
|
|
- // this.layer = new Konva.Layer()
|
|
|
-
|
|
|
- // // 创建底图
|
|
|
- // const bgImage = new Image()
|
|
|
- // bgImage.src = require('@/assets/images/table.png')
|
|
|
- // bgImage.onload = () => {
|
|
|
- // const KnovaImage = new Konva.Image({
|
|
|
- // x: 350,
|
|
|
- // y: 10,
|
|
|
- // image: bgImage,
|
|
|
- // width: 500,
|
|
|
- // height: 790,
|
|
|
- // draggable: false
|
|
|
- // })
|
|
|
- // this.layer.add(KnovaImage)
|
|
|
-
|
|
|
- // this.layer.draw()
|
|
|
- // }
|
|
|
- // // 调用函数
|
|
|
- // const imageSrc = require('@/assets/images/localSetIcon.jpg'); // 图片路径
|
|
|
-
|
|
|
- // this.renderGrid( imageSrc, 6, 3, 450, 100, 120, 100, 50, 50, 60, 25);
|
|
|
- // // 将图层添加到舞台
|
|
|
- // this.stage.add(this.layer)
|
|
|
- // this.layer.draw()
|
|
|
-
|
|
|
- // // 添加鼠标滚轮缩放功能
|
|
|
- // this.stage.on('wheel', (e) => {
|
|
|
- // e.evt.preventDefault()
|
|
|
-
|
|
|
- // const scaleBy = 1.05 // 缩放因子
|
|
|
- // const oldScale = this.stage.scaleX() // 获取当前缩放比例
|
|
|
- // const pointer = this.stage.getPointerPosition() // 获取鼠标位置
|
|
|
-
|
|
|
- // const mousePointTo = {
|
|
|
- // x: (pointer.x - this.stage.x()) / oldScale,
|
|
|
- // y: (pointer.y - this.stage.y()) / oldScale
|
|
|
- // }
|
|
|
-
|
|
|
- // // 根据滚轮滚动的方向决定缩放
|
|
|
- // const newScale = e.evt.deltaY > 0 ? oldScale / scaleBy : oldScale * scaleBy
|
|
|
-
|
|
|
- // this.stage.scale({ x: newScale, y: newScale })
|
|
|
-
|
|
|
- // const newPos = {
|
|
|
- // x: pointer.x - mousePointTo.x * newScale,
|
|
|
- // y: pointer.y - mousePointTo.y * newScale
|
|
|
- // }
|
|
|
- // this.stage.position(newPos)
|
|
|
- // this.stage.batchDraw()
|
|
|
- // })
|
|
|
-
|
|
|
- // this.stage.on('mousedown', function() {
|
|
|
- // this.stage.container().style.cursor = 'move'
|
|
|
- // }.bind(this))
|
|
|
-
|
|
|
- // this.stage.on('mouseup', function() {
|
|
|
- // this.stage.container().style.cursor = 'default'
|
|
|
- // }.bind(this))
|
|
|
- // this.stage.draggable(true)
|
|
|
- // },
|
|
|
-
|
|
|
initKonva() {
|
|
|
// 创建舞台
|
|
|
this.stage = new Konva.Stage({
|
|
|
@@ -160,113 +92,13 @@ export default {
|
|
|
|
|
|
this.layer.draw();
|
|
|
},
|
|
|
- // renderGrid(
|
|
|
- // imageSrc,
|
|
|
- // rows,
|
|
|
- // cols,
|
|
|
- // startX,
|
|
|
- // startY,
|
|
|
- // xOffset,
|
|
|
- // yOffset,
|
|
|
- // imageWidth,
|
|
|
- // imageHeight,
|
|
|
- // rectWidth,
|
|
|
- // rectHeight
|
|
|
- // ) {
|
|
|
- // // 初始化状态数组
|
|
|
- // this.selectedStates = new Array(rows * cols).fill(false);
|
|
|
-
|
|
|
- // for (let row = 0; row < rows; row++) {
|
|
|
- // for (let col = 0; col < cols; col++) {
|
|
|
- // const x = startX + col * xOffset;
|
|
|
- // const y = startY + row * yOffset;
|
|
|
- // const index = row * cols + col;
|
|
|
-
|
|
|
- // const point = new Image();
|
|
|
- // point.src = imageSrc;
|
|
|
- // point.onload = () => {
|
|
|
- // const knovaImage = new Konva.Image({
|
|
|
- // x: x,
|
|
|
- // y: y,
|
|
|
- // image: point,
|
|
|
- // width: imageWidth,
|
|
|
- // height: imageHeight,
|
|
|
- // draggable: false,
|
|
|
- // });
|
|
|
-
|
|
|
- // knovaImage.on("click", () => {
|
|
|
- // // 切换选中状态
|
|
|
- // this.selectedStates[index] = !this.selectedStates[index];
|
|
|
-
|
|
|
- // // 更新 selectedText 数组
|
|
|
- // if (this.selectedStates[index]) {
|
|
|
- // // 元素被选中,替换成选中的图片
|
|
|
- // point.src = require("@/assets/images/localSetSelect.jpg");
|
|
|
-
|
|
|
- // // 检查是否已经在 selectedText 数组中
|
|
|
- // if (
|
|
|
- // !this.selectedText.some((item) => item.value === index + 1)
|
|
|
- // ) {
|
|
|
- // // 元素被选中,添加到 selectedText 数组
|
|
|
- // this.selectedText.push({
|
|
|
- // label: `E-${index + 1}`,
|
|
|
- // value: index + 1,
|
|
|
- // });
|
|
|
- // }
|
|
|
- // } else {
|
|
|
- // // 元素未选中,恢复原图片
|
|
|
- // point.src = imageSrc;
|
|
|
-
|
|
|
- // // 元素未选中,从 selectedText 数组中移除
|
|
|
- // const textIndex = this.selectedText.findIndex(
|
|
|
- // (item) => item.value === index + 1
|
|
|
- // );
|
|
|
- // if (textIndex !== -1) {
|
|
|
- // this.selectedText.splice(textIndex, 1); // 如果元素在 selectedText 中,移除它
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- // knovaImage.image(point); // 更新图片
|
|
|
- // this.layer.draw(); // 重绘图层
|
|
|
-
|
|
|
- // // console.log(this.selectedText, '子组件选中的隔离点');
|
|
|
-
|
|
|
- // // 传递选中的元素文本到父组件
|
|
|
- // this.$emit("selection-changed", this.selectedText);
|
|
|
- // });
|
|
|
-
|
|
|
- // this.layer.add(knovaImage);
|
|
|
-
|
|
|
- // const rect = new Konva.Rect({
|
|
|
- // x: x - 3,
|
|
|
- // y: y + imageHeight + 10,
|
|
|
- // width: rectWidth,
|
|
|
- // height: rectHeight,
|
|
|
- // cornerRadius: 10,
|
|
|
- // stroke: "red",
|
|
|
- // strokeWidth: 2,
|
|
|
- // fill: "white",
|
|
|
- // });
|
|
|
- // this.layer.add(rect);
|
|
|
-
|
|
|
- // const text = new Konva.Text({
|
|
|
- // x: x + 12,
|
|
|
- // y: y + imageHeight + 15,
|
|
|
- // fontSize: 20,
|
|
|
- // text: `E-${index + 1}`,
|
|
|
- // fontFamily: "Calibri",
|
|
|
- // fill: "red",
|
|
|
- // });
|
|
|
- // this.layer.add(text);
|
|
|
-
|
|
|
- // this.layer.draw();
|
|
|
- // };
|
|
|
- // }
|
|
|
- // }
|
|
|
- // },
|
|
|
-
|
|
|
renderGrid(imageSrc) {
|
|
|
- this.selectedStates = [];
|
|
|
+ this.selectedStates = {}; // 用对象来存储选中状态,键为文字内容
|
|
|
+ this.rects = {};
|
|
|
+ this.texts = {};
|
|
|
+ this.redrects = {};
|
|
|
+ this.redtexts = {};
|
|
|
+ this.selectedText = [];
|
|
|
|
|
|
const positions = [
|
|
|
{ row: 2, col: 9 }, // E-1
|
|
|
@@ -292,6 +124,7 @@ export default {
|
|
|
positions.forEach((pos, index) => {
|
|
|
const x = pos.col * 50; // 每个单元格宽度为50
|
|
|
const y = pos.row * 50; // 每个单元格高度为50
|
|
|
+ const labelText = `E-${index + 1}`; // 对应的文字
|
|
|
|
|
|
const point = new Image();
|
|
|
point.src = imageSrc;
|
|
|
@@ -306,30 +139,65 @@ export default {
|
|
|
});
|
|
|
|
|
|
knovaImage.on("click", () => {
|
|
|
- this.selectedStates[index] = !this.selectedStates[index];
|
|
|
-
|
|
|
- if (this.selectedStates[index]) {
|
|
|
- point.src = require("@/assets/images/localSetSelect.jpg");
|
|
|
+ // 切换选中状态,基于文本内容
|
|
|
+ this.selectedStates[labelText] = !this.selectedStates[labelText];
|
|
|
+
|
|
|
+ // 判断当前是否选中
|
|
|
+ if (this.selectedStates[labelText]) {
|
|
|
+ // 选中状态
|
|
|
+ // 显示红色矩形和文字,隐藏普通矩形和文字
|
|
|
+ this.rects[labelText].visible(false);
|
|
|
+ this.texts[labelText].visible(false);
|
|
|
+ this.redrects[labelText].visible(true);
|
|
|
+ this.redtexts[labelText].visible(true);
|
|
|
+
|
|
|
+ // 切换图片为选中状态的图片
|
|
|
+ const selectedImage = new Image();
|
|
|
+ selectedImage.src = require('@/assets/images/localSetSelect.jpg'); // 选中的图片路径
|
|
|
+ selectedImage.onload = () => {
|
|
|
+ knovaImage.image(selectedImage); // 更新图像
|
|
|
+ this.layer.draw(); // 更新图层
|
|
|
+ };
|
|
|
+
|
|
|
+ // 将选中的 labelText 推入数组
|
|
|
this.selectedText.push({
|
|
|
- label: `E-${index + 1}`,
|
|
|
+ label: labelText,
|
|
|
value: index + 1,
|
|
|
});
|
|
|
+
|
|
|
} else {
|
|
|
- point.src = imageSrc;
|
|
|
+ // 取消选中状态
|
|
|
+ // 显示普通矩形和文字,隐藏红色矩形和文字
|
|
|
+ this.rects[labelText].visible(true);
|
|
|
+ this.texts[labelText].visible(true);
|
|
|
+ this.redrects[labelText].visible(false);
|
|
|
+ this.redtexts[labelText].visible(false);
|
|
|
+
|
|
|
+ // 切换图片为未选中状态的图片
|
|
|
+ const normalImage = new Image();
|
|
|
+ normalImage.src = imageSrc; // 未选中的默认图片路径
|
|
|
+ normalImage.onload = () => {
|
|
|
+ knovaImage.image(normalImage); // 更新图像
|
|
|
+ this.layer.draw(); // 更新图层
|
|
|
+ };
|
|
|
+
|
|
|
+ // 从选中数组中移除该项
|
|
|
this.selectedText = this.selectedText.filter(
|
|
|
- (item) => item.value !== index + 1
|
|
|
+ (item) => item.label !== labelText
|
|
|
);
|
|
|
}
|
|
|
|
|
|
- knovaImage.image(point);
|
|
|
+ // 最后确保图层重新渲染
|
|
|
this.layer.draw();
|
|
|
|
|
|
// 传递选中的元素文本到父组件
|
|
|
this.$emit("selection-changed", this.selectedText);
|
|
|
});
|
|
|
|
|
|
+
|
|
|
this.layer.add(knovaImage);
|
|
|
|
|
|
+ // 普通矩形
|
|
|
const rect = new Konva.Rect({
|
|
|
x: x - 3,
|
|
|
y: y + 55,
|
|
|
@@ -341,21 +209,58 @@ export default {
|
|
|
fill: "white",
|
|
|
});
|
|
|
this.layer.add(rect);
|
|
|
+ this.rects[labelText] = rect; // 用文字作为键存储
|
|
|
|
|
|
+ // 普通文字
|
|
|
const text = new Konva.Text({
|
|
|
x: x + 12,
|
|
|
y: y + 60,
|
|
|
fontSize: 20,
|
|
|
- text: `E-${index + 1}`,
|
|
|
+ text: labelText,
|
|
|
fontFamily: "Calibri",
|
|
|
fill: "red",
|
|
|
});
|
|
|
this.layer.add(text);
|
|
|
+ this.texts[labelText] = text; // 用文字作为键存储
|
|
|
+
|
|
|
+ // 红色矩形(初始隐藏)
|
|
|
+ const redrect = new Konva.Rect({
|
|
|
+ x: x - 3,
|
|
|
+ y: y + 55,
|
|
|
+ width: 60,
|
|
|
+ height: 25,
|
|
|
+ cornerRadius: 10,
|
|
|
+ stroke: "red",
|
|
|
+ strokeWidth: 2,
|
|
|
+ fill: "red",
|
|
|
+ visible: false,
|
|
|
+ });
|
|
|
+ this.layer.add(redrect);
|
|
|
+ this.redrects[labelText] = redrect; // 用文字作为键存储
|
|
|
+
|
|
|
+ // 红色文字(初始隐藏)
|
|
|
+ const redtext = new Konva.Text({
|
|
|
+ x: x + 12,
|
|
|
+ y: y + 60,
|
|
|
+ fontSize: 20,
|
|
|
+ text: labelText,
|
|
|
+ fontFamily: "Calibri",
|
|
|
+ fill: "white",
|
|
|
+ visible: false,
|
|
|
+ });
|
|
|
+ this.layer.add(redtext);
|
|
|
+ this.redtexts[labelText] = redtext; // 用文字作为键存储
|
|
|
+
|
|
|
this.layer.draw();
|
|
|
};
|
|
|
});
|
|
|
},
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
// methods结束
|
|
|
},
|
|
|
};
|