index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. bgrects:[],//白色底部
  28. redrects: [], //红色rect合集
  29. redtexts: [], //白色text合集
  30. }
  31. },
  32. watch: {
  33. 'getMapData': {
  34. handler(newval) {
  35. if (newval) {
  36. this.$nextTick(()=>{
  37. this.initKonva()
  38. })
  39. }
  40. }
  41. },
  42. },
  43. mounted() {
  44. // this.initKonva() //暂时先注释掉这里 为了避免多次重复渲染
  45. console.log(this.points, 'points')
  46. console.log(
  47. this.getSelectSopPoints,
  48. this.getSopEdit,
  49. this.getSopLook,
  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.bgrects={}
  126. this.redrects = {}
  127. this.redtexts = {}
  128. this.selectedText = []
  129. // const positions = JSON.parse(this.points.map);
  130. console.log(this.getMapData,',apData')
  131. let positions;
  132. // 判断getMapData是否已为数组
  133. if (Array.isArray(this.getMapData)) {
  134. // 如果是数组,则直接使用
  135. positions = this.getMapData;
  136. } else {
  137. // 如果不是数组,则尝试将其从JSON字符串转换为数组或其他对象
  138. try {
  139. positions = JSON.parse(this.getMapData);
  140. } catch (e) {
  141. console.error("Error parsing getMapData:", e);
  142. // 可以在这里处理错误情况,例如将positions设置为空数组等
  143. positions = [];
  144. }
  145. }
  146. // 检查 this.getSelectSopPoints 是否有内容
  147. const isLocked = this.getSelectSopPoints.length > 0
  148. // 添加或移除全局点击事件监听器 this.getSopEdit这是vuex里判断是否可以选择隔离点的操作
  149. if (isLocked && this.getSopEdit == true) {
  150. this.layer.on('click', (e) => {
  151. e.cancelBubble = true // 阻止事件冒泡
  152. // e.stopPropagation(); // 阻止事件传播
  153. })
  154. } else {
  155. this.layer.off('click') // 移除全局点击事件监听器
  156. }
  157. positions.forEach((pos, index) => {
  158. const x = pos.col * 50 // 每个单元格宽度为50
  159. const y = pos.row * 50 // 每个单元格高度为50
  160. const labelText = pos.pointName // 对应的文字
  161. const point = new Image()
  162. point.src = pos.pointIcon
  163. // point.src = imageSrc;
  164. point.onload = () => {
  165. const knovaImage = new Konva.Image({
  166. x: x,
  167. y: y,
  168. image: point,
  169. width: 50,
  170. height: 50,
  171. draggable: false
  172. })
  173. // 添加点击事件,仅当 getSopEdit 为 true 时才允许点击a
  174. if (this.getSopEdit == true && this.getSopLook == false) {
  175. knovaImage.on('click', () => {
  176. // 切换选中状态,基于文本内容
  177. this.selectedStates[labelText] = !this.selectedStates[labelText]
  178. if (this.selectedStates[labelText]) {
  179. // 选中状态,显示红色矩形和文字,切换为选中的图片
  180. this.rects[labelText].visible(true)
  181. this.texts[labelText].visible(true)
  182. this.redrects[labelText].visible(true)
  183. this.redtexts[labelText].visible(true)
  184. const selectedImage = new Image()
  185. selectedImage.src = pos.pointIcon
  186. selectedImage.onload = () => {
  187. knovaImage.image(selectedImage) // 更新图像
  188. this.layer.draw() // 更新图层
  189. }
  190. // 获取隔离点信息,并将选中的 labelText 推入数组
  191. this.$nextTick(() => {
  192. if (
  193. this.$route.query.sopId !== null ||
  194. this.$route.query.ticketId !== null
  195. ) {
  196. this.selectedText.push({
  197. pointName: pos.pointName,
  198. pointId: pos.pointId,
  199. pointType: pos.pointTypeName,
  200. powerType: pos.powerTypeName,
  201. prePointId:pos.prePointId
  202. })
  203. this.$emit('selection-changed', this.selectedText)
  204. }
  205. })
  206. } else {
  207. // 取消选中状态,恢复普通矩形和文字,切换为未选中的图片
  208. this.rects[labelText].visible(true)
  209. this.texts[labelText].visible(true)
  210. this.redrects[labelText].visible(false)
  211. this.redtexts[labelText].visible(false)
  212. const normalImage = new Image()
  213. normalImage.src = pos.pointIcon
  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. // 底部白色背景
  230. const bgrect=new Konva.Rect({
  231. x: x - 6,
  232. y: y - 5,
  233. width: 62,
  234. height: 80,
  235. cornerRadius: 5,
  236. stroke: 'white',
  237. strokeWidth: 2,
  238. fill: 'white'
  239. })
  240. this.layer.add(bgrect)
  241. this.bgrects[labelText] = bgrect // 用文字作为键存储
  242. // 普通矩形
  243. const rect = new Konva.Rect({
  244. x: x + 0,
  245. y: y - 1,
  246. width: 50,
  247. height: 72,
  248. cornerRadius: 5,
  249. stroke: 'red',
  250. strokeWidth: 2,
  251. fill: 'white'
  252. })
  253. this.layer.add(rect)
  254. this.rects[labelText] = rect // 用文字作为键存储
  255. // 先加底部白色 再添加图片
  256. this.layer.add(knovaImage)
  257. // // 普通矩形
  258. // const rect = new Konva.Rect({
  259. // x: x ,
  260. // y: y + 45,
  261. // width: 50,
  262. // height: 25,
  263. // cornerRadius: 0,
  264. // stroke: 'red',
  265. // strokeWidth: 2,
  266. // fill: 'white'
  267. // })
  268. // this.layer.add(rect)
  269. // this.rects[labelText] = rect // 用文字作为键存储
  270. //
  271. // // 普通文字
  272. // const text = new Konva.Text({
  273. // x: x + 8,
  274. // y: y + 50,
  275. // fontSize: 20,
  276. // text: labelText,
  277. // fontFamily: 'Calibri',
  278. // fill: 'red'
  279. // })
  280. // this.layer.add(text)
  281. // this.texts[labelText] = text // 用文字作为键存储
  282. // // 红色矩形(初始隐藏)
  283. // const redrect = new Konva.Rect({
  284. // x: x - 3,
  285. // y: y + 55,
  286. // width: 60,
  287. // height: 25,
  288. // cornerRadius: 10,
  289. // stroke: 'red',
  290. // strokeWidth: 2,
  291. // fill: 'red',
  292. // visible: false
  293. // })
  294. // this.layer.add(redrect)
  295. // this.redrects[labelText] = redrect // 用文字作为键存储
  296. //
  297. // // 红色文字(初始隐藏)
  298. // const redtext = new Konva.Text({
  299. // x: x + 12,
  300. // y: y + 60,
  301. // fontSize: 20,
  302. // text: labelText,
  303. // fontFamily: 'Calibri',
  304. // fill: 'white',
  305. // visible: false
  306. // })
  307. // this.layer.add(redtext)
  308. // this.redtexts[labelText] = redtext // 用文字作为键存储
  309. // 普通文字
  310. const text = new Konva.Text({
  311. x: x + 8,
  312. y: y + 50,
  313. fontSize: 20,
  314. text: labelText,
  315. fontFamily: 'Calibri',
  316. fill: 'red'
  317. })
  318. this.layer.add(text)
  319. this.texts[labelText] = text // 用文字作为键存储
  320. // 覆盖层(表示选中状态)
  321. const redrect = new Konva.Rect({
  322. x: x - 7,
  323. y: y - 6,
  324. width: 64.1,
  325. height: 82,
  326. cornerRadius: 5,
  327. fill: 'rgba(97, 97, 97, 0.5)', // 半透明灰色
  328. visible: false, // 初始状态隐藏
  329. listening:false
  330. });
  331. this.layer.add(redrect)
  332. this.redrects[labelText] = redrect // 用文字作为键存储
  333. // 创建对号文本
  334. const redtext = new Konva.Text({
  335. x: x - 5 + 42 / 2, // 水平居中
  336. y: y + 62 / 2, // 垂直居中
  337. fontSize: 24, // 根据需要调整字体大小
  338. text: '✔',
  339. fontFamily: 'Arial',
  340. fill: 'white',
  341. align: 'center',
  342. verticalAlign: 'middle',
  343. visible: false,//初始隐藏状态
  344. listening:false,
  345. });
  346. this.layer.add(redtext);
  347. this.redtexts[labelText] = redtext; // 用文字作为键存储
  348. // 检查 this.getSelectSopPoints 是否包含当前点的 pointId
  349. if (pos.state) {
  350. console.log('我走这里了-------')
  351. // 设置为选中状态
  352. this.selectedStates[labelText] = true
  353. this.rects[labelText].visible(true)
  354. this.texts[labelText].visible(true)
  355. this.redrects[labelText].visible(true)
  356. this.redtexts[labelText].visible(true)
  357. // 切换图片为选中状态的图片
  358. const selectedImage = new Image()
  359. selectedImage.src = pos.pointIcon // 选中的图片路径
  360. selectedImage.onload = () => {
  361. knovaImage.image(selectedImage) // 更新图像
  362. this.layer.draw() // 更新图层
  363. }
  364. // 将选中的 labelText 推入数组
  365. this.selectedText.push({
  366. pointName: pos.pointName,
  367. pointId: pos.pointId,
  368. pointType: pos.pointTypeName,
  369. powerType: pos.powerTypeName,
  370. prePointId:pos.prePointId
  371. })
  372. } else {
  373. // 设置为未选中状态
  374. this.selectedStates[labelText] = false
  375. this.rects[labelText].visible(true)
  376. this.texts[labelText].visible(true)
  377. this.redrects[labelText].visible(false)
  378. this.redtexts[labelText].visible(false)
  379. }
  380. // 触发父组件的 selection-changed 事件
  381. this.$emit('selection-changed', this.selectedText)
  382. this.layer.draw()
  383. }
  384. })
  385. }
  386. // methods结束
  387. }
  388. }
  389. </script>
  390. <style scoped lang="scss">
  391. #container {
  392. width: 100%;
  393. height: 100%;
  394. }
  395. </style>