index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <template>
  2. <div id="container" ref="container"></div>
  3. </template>
  4. <script>
  5. import Konva from 'konva'
  6. import {
  7. selectIsIsolationPointById,
  8. getIsIsolationPointPage
  9. } from '@/api/mes/spm/segregationPoint'
  10. import { mapGetters } from 'vuex'
  11. export default {
  12. name: 'KonvaExample',
  13. props: {
  14. points: {
  15. type: Object,
  16. default: null
  17. }
  18. },
  19. data() {
  20. return {
  21. stage: null,
  22. layer: null,
  23. selectedStates: [], // 用于存储每个元素的选中状态
  24. selectedText: [], // 用于存储未选中的元素文本
  25. rects: [], //白色rect合集
  26. texts: [], //白色text合集
  27. redrects: [], //红色rect合集
  28. redtexts: [] //白色text合集
  29. }
  30. },
  31. watch: {
  32. 'getMapData': {
  33. handler(newval) {
  34. if (newval) {
  35. this.$nextTick(() => {
  36. this.initKonva()
  37. })
  38. }
  39. }
  40. }
  41. },
  42. mounted() {
  43. this.initKonva()
  44. console.log(this.points, 'points')
  45. // console.log(
  46. // this.getSelectSopPoints,
  47. // this.getSopEdit,
  48. // this.getSopLook,
  49. // this.getMapData,
  50. // 'getSopEdit - getSelectSopPoints'
  51. // )
  52. },
  53. computed: {
  54. ...mapGetters('sopSelectPoints', [
  55. 'getSelectSopPoints',
  56. 'getSopEdit',
  57. 'getSopLook',
  58. 'getMapData'
  59. ])
  60. },
  61. methods: {
  62. initKonva() {
  63. // 创建舞台
  64. this.stage = new Konva.Stage({
  65. container: this.$refs.container, // 容器元素
  66. width: 1270,
  67. height: 830
  68. })
  69. // 创建图层
  70. this.layer = new Konva.Layer()
  71. // 创建底图
  72. const bgImage = new Image()
  73. bgImage.src = require('@/assets/images/table.png')
  74. bgImage.onload = () => {
  75. const knovaImage = new Konva.Image({
  76. x: 330,
  77. y: 10,
  78. image: bgImage,
  79. width: 500,
  80. height: 790,
  81. draggable: false
  82. })
  83. this.layer.add(knovaImage)
  84. this.layer.draw()
  85. }
  86. // 绘制无限网格
  87. this.drawGrid(50, 50, '#e0e0e0') // 每个单元格50x50,浅灰色网格
  88. // 渲染数据
  89. const imageSrc = require('@/assets/images/localSetIcon.jpg') // 图片路径
  90. this.renderGrid(imageSrc, 6, 3, 450, 100, 120, 100, 50, 50, 60, 25)
  91. // 将图层添加到舞台
  92. this.stage.add(this.layer)
  93. this.layer.draw()
  94. // 禁止舞台拖拽
  95. this.stage.draggable(false)
  96. },
  97. // 绘制无限网格
  98. drawGrid(cellWidth, cellHeight, gridColor) {
  99. const width = 1270
  100. const height = 830
  101. // 绘制竖线
  102. for (let i = 0; i <= width; i += cellWidth) {
  103. const verticalLine = new Konva.Line({
  104. points: [i, 0, i, height],
  105. stroke: gridColor,
  106. strokeWidth: 1
  107. })
  108. this.layer.add(verticalLine)
  109. }
  110. // 绘制横线
  111. for (let j = 0; j <= height; j += cellHeight) {
  112. const horizontalLine = new Konva.Line({
  113. points: [0, j, width, j],
  114. stroke: gridColor,
  115. strokeWidth: 1
  116. })
  117. this.layer.add(horizontalLine)
  118. }
  119. this.layer.draw()
  120. },
  121. renderGrid(imageSrc) {
  122. this.selectedStates = {}; // 用对象来存储选中状态,键为文字内容
  123. this.rects = {};
  124. this.texts = {};
  125. this.redrects = {};
  126. this.redtexts = {};
  127. this.selectedText = [];
  128. let positions;
  129. // 判断getMapData是否已为数组
  130. if (Array.isArray(this.getMapData)) {
  131. // 如果是数组,则直接使用
  132. positions = this.getMapData;
  133. } else {
  134. // 如果不是数组,则尝试将其从JSON字符串转换为数组或其他对象
  135. try {
  136. positions = JSON.parse(this.getMapData);
  137. } catch (e) {
  138. console.error("Error parsing getMapData:", e);
  139. // 可以在这里处理错误情况,例如将positions设置为空数组等
  140. positions = [];
  141. }
  142. }
  143. console.log(positions, 'positions');
  144. // 检查 this.getSelectSopPoints 是否有内容
  145. const isLocked = this.getSelectSopPoints.length > 0;
  146. // 添加或移除全局点击事件监听器 this.getSopEdit这是vuex里判断是否可以选择隔离点的操作
  147. if (isLocked && this.getSopEdit === true) {
  148. this.layer.on('click', (e) => {
  149. e.cancelBubble = true; // 阻止事件冒泡
  150. });
  151. } else {
  152. this.layer.off('click'); // 移除全局点击事件监听器
  153. }
  154. positions.forEach((pos, index) => {
  155. const x = pos.col * 50; // 每个单元格宽度为50
  156. const y = pos.row * 50; // 每个单元格高度为50
  157. const labelText = pos.pointName; // 对应的文字
  158. const point = new Image();
  159. point.src = pos.pointIcon;
  160. point.onload = () => {
  161. const knovaImage = new Konva.Image({
  162. x: x,
  163. y: y,
  164. image: point,
  165. width: 50,
  166. height: 50,
  167. draggable: false
  168. });
  169. // 添加点击事件,仅当 getSopEdit 为 true 时才允许点击
  170. if (this.getSopEdit === true && this.getSopLook === false) {
  171. knovaImage.on('click', () => {
  172. // 切换选中状态,基于文本内容
  173. this.selectedStates[labelText] = !this.selectedStates[labelText];
  174. if (this.selectedStates[labelText]) {
  175. // 选中状态,显示红色矩形和文字,切换为选中的图片
  176. this.rects[labelText].visible(false);
  177. this.texts[labelText].visible(false);
  178. this.redrects[labelText].visible(true);
  179. this.redtexts[labelText].visible(true);
  180. const selectedImage = new Image();
  181. selectedImage.src = require('@/assets/images/localSetSelect.jpg');
  182. selectedImage.onload = () => {
  183. knovaImage.image(selectedImage); // 更新图像
  184. this.layer.draw(); // 更新图层
  185. };
  186. // 获取隔离点信息,并将选中的 labelText 推入数组
  187. this.$nextTick(() => {
  188. if (
  189. this.$route.query.sopId !== null ||
  190. this.$route.query.ticketId !== null
  191. ) {
  192. const newItem = {
  193. pointName: pos.pointName,
  194. pointId: pos.pointId,
  195. pointType: pos.pointTypeName,
  196. powerType: pos.powerTypeName,
  197. prePointId: pos.prePointId,
  198. };
  199. // 去重处理
  200. if (!this.selectedText.some(item => item.pointId === newItem.pointId)) {
  201. this.selectedText.push(newItem);
  202. }
  203. console.log(this.selectedText, 'selectedText');
  204. this.$emit('selection-changed', this.selectedText);
  205. }
  206. });
  207. } else {
  208. // 取消选中状态,恢复普通矩形和文字
  209. this.rects[labelText].visible(true);
  210. this.texts[labelText].visible(true);
  211. this.redrects[labelText].visible(false);
  212. this.redtexts[labelText].visible(false);
  213. const normalImage = new Image();
  214. normalImage.src = imageSrc; // 未选中的默认图片路径
  215. normalImage.onload = () => {
  216. knovaImage.image(normalImage); // 更新图像
  217. this.layer.draw(); // 更新图层
  218. };
  219. // 从选中数组中移除该项
  220. this.selectedText = this.selectedText.filter(
  221. (item) => item.pointName !== labelText
  222. );
  223. }
  224. // 确保图层重新渲染
  225. this.layer.draw();
  226. this.$emit('selection-changed', this.selectedText);
  227. });
  228. }
  229. this.layer.add(knovaImage);
  230. // 普通矩形
  231. const rect = new Konva.Rect({
  232. x: x - 3,
  233. y: y + 55,
  234. width: 60,
  235. height: 25,
  236. cornerRadius: 10,
  237. stroke: 'red',
  238. strokeWidth: 2,
  239. fill: 'white'
  240. });
  241. this.layer.add(rect);
  242. this.rects[labelText] = rect; // 用文字作为键存储
  243. // 普通文字
  244. const text = new Konva.Text({
  245. x: x + 12,
  246. y: y + 60,
  247. fontSize: 20,
  248. text: labelText,
  249. fontFamily: 'Calibri',
  250. fill: 'red'
  251. });
  252. this.layer.add(text);
  253. this.texts[labelText] = text; // 用文字作为键存储
  254. // 红色矩形(初始隐藏)
  255. const redrect = new Konva.Rect({
  256. x: x - 3,
  257. y: y + 55,
  258. width: 60,
  259. height: 25,
  260. cornerRadius: 10,
  261. stroke: 'red',
  262. strokeWidth: 2,
  263. fill: 'red',
  264. visible: false
  265. });
  266. this.layer.add(redrect);
  267. this.redrects[labelText] = redrect; // 用文字作为键存储
  268. // 红色文字(初始隐藏)
  269. const redtext = new Konva.Text({
  270. x: x + 12,
  271. y: y + 60,
  272. fontSize: 20,
  273. text: labelText,
  274. fontFamily: 'Calibri',
  275. fill: 'white',
  276. visible: false
  277. });
  278. this.layer.add(redtext);
  279. this.redtexts[labelText] = redtext; // 用文字作为键存储
  280. // 检查 this.getSelectSopPoints 是否包含当前点的 pointId
  281. if (pos.state) {
  282. console.log('选中的隔离点渲染',pos)
  283. // 设置为选中状态
  284. this.selectedStates[labelText] = true;
  285. this.rects[labelText].visible(false);
  286. this.texts[labelText].visible(false);
  287. this.redrects[labelText].visible(true);
  288. this.redtexts[labelText].visible(true);
  289. // 切换图片为选中状态的图片
  290. const selectedImage = new Image();
  291. selectedImage.src = require('@/assets/images/localSetSelect.jpg'); // 选中的图片路径
  292. selectedImage.onload = () => {
  293. knovaImage.image(selectedImage); // 更新图像
  294. this.layer.draw(); // 更新图层
  295. };
  296. // 将选中的 labelText 推入数组
  297. const newItem = {
  298. pointName: pos.pointName,
  299. pointId: pos.pointId,
  300. pointType: pos.pointTypeName,
  301. powerType: pos.powerTypeName,
  302. prePointId: pos.prePointId,
  303. };
  304. // 去重处理
  305. if (!this.selectedText.some(item => item.pointId === newItem.pointId)) {
  306. this.selectedText.push(newItem);
  307. }
  308. } else {
  309. // 设置为未选中状态
  310. this.selectedStates[labelText] = false;
  311. this.rects[labelText].visible(true);
  312. this.texts[labelText].visible(true);
  313. this.redrects[labelText].visible(false);
  314. this.redtexts[labelText].visible(false);
  315. }
  316. // 触发父组件的 selection-changed 事件
  317. this.$emit('selection-changed', this.selectedText);
  318. this.layer.draw();
  319. }
  320. });
  321. },
  322. // methods结束
  323. }
  324. }
  325. </script>
  326. <style scoped lang="scss">
  327. #container {
  328. width: 100%;
  329. height: 100%;
  330. }
  331. </style>