MapData.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <div class="mapdata">
  3. <div id="container" ref="container" style="width:1200px"></div>
  4. <div class="left">
  5. <el-input v-model="value" type="textarea" :rows="38" ></el-input>
  6. <el-button @click="refresh" type="primary" style="width: 80px;align-self: flex-end">刷新</el-button>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. import Konva from "konva";
  12. import { getWorkareaInfo,updateWorkarea } from '@/api/mes/wa/workarea'
  13. export default {
  14. name: "KonvaExample",
  15. data() {
  16. return {
  17. stage: null,
  18. layer: null,
  19. selectedStates: [], // 用于存储每个元素的选中状态
  20. selectedText: [], // 用于存储未选中的元素文本
  21. rects: [], // 白色r ect合集
  22. texts: [], // 白色text合集
  23. redrects: [], // 红色rect合集
  24. redtexts: [], // 白色text合集
  25. value:'',
  26. form:{},//拿到单个数据
  27. };
  28. },
  29. watch:{
  30. '$route.query.workareaId':{
  31. handler(newValue){
  32. if(newValue){
  33. this.getWorkAreaInfo()
  34. this.initKonva();
  35. }
  36. }
  37. }
  38. },
  39. mounted() {
  40. this.$nextTick(()=>{
  41. this.getWorkAreaInfo()
  42. })
  43. console.log(this.$route.query.workareaId,'workareaId');
  44. },
  45. methods: {
  46. getWorkAreaInfo(){
  47. const workareaId=this.$route.query.workareaId;
  48. getWorkareaInfo(workareaId).then(response => {
  49. console.log(response,'工作区域预览接口调用');
  50. this.form=response.data;
  51. if (response.data.map) {
  52. try {
  53. let map = JSON.parse(response.data.map)
  54. this.value = JSON.stringify(map, null, 4)
  55. } catch (err) {}
  56. }
  57. this.initKonva();
  58. // console.log(this.value,'Value')
  59. })
  60. },
  61. refresh(){
  62. this.initKonva();
  63. this.form.map=this.value
  64. updateWorkarea(this.form).then(response => {
  65. console.log(response,'修改车间区域地图 ');
  66. })
  67. },
  68. initKonva() {
  69. // 创建舞台
  70. this.stage = new Konva.Stage({
  71. container: this.$refs.container, // 容器元素
  72. width: 1200,
  73. height: 830,
  74. });
  75. // 创建图层
  76. this.layer = new Konva.Layer();
  77. // 创建底图
  78. const bgImage = new Image();
  79. bgImage.src = require("@/assets/images/table.png");
  80. bgImage.onload = () => {
  81. const knovaImage = new Konva.Image({
  82. x: 330,
  83. y: 10,
  84. image: bgImage,
  85. width: 500,
  86. height: 790,
  87. draggable: false,
  88. });
  89. this.layer.add(knovaImage);
  90. this.layer.draw();
  91. };
  92. // 绘制无限网格
  93. this.drawGrid(50, 50, "#e0e0e0"); // 每个单元格50x50,浅灰色网格
  94. // 渲染数据
  95. const imageSrc = require("@/assets/images/localSetIcon.jpg"); // 图片路径
  96. this.renderGrid(imageSrc, 6, 3, 450, 100, 120, 100, 50, 50, 60, 25);
  97. // 将图层添加到舞台
  98. this.stage.add(this.layer);
  99. this.layer.draw();
  100. // 禁止舞台拖拽
  101. this.stage.draggable(false);
  102. },
  103. // 绘制无限网格
  104. drawGrid(cellWidth, cellHeight, gridColor) {
  105. const width = 1200;
  106. const height = 830;
  107. // 绘制竖线
  108. for (let i = 0; i <= width; i += cellWidth) {
  109. const verticalLine = new Konva.Line({
  110. points: [i, 0, i, height],
  111. stroke: gridColor,
  112. strokeWidth: 1,
  113. });
  114. this.layer.add(verticalLine);
  115. }
  116. // 绘制横线
  117. for (let j = 0; j <= height; j += cellHeight) {
  118. const horizontalLine = new Konva.Line({
  119. points: [0, j, width, j],
  120. stroke: gridColor,
  121. strokeWidth: 1,
  122. });
  123. this.layer.add(horizontalLine);
  124. }
  125. this.layer.draw();
  126. },
  127. renderGrid(imageSrc) {
  128. this.selectedStates = []; // 用数组来存储选中状态
  129. this.rects = [];
  130. this.texts = [];
  131. this.redrects = [];
  132. this.redtexts = [];
  133. this.selectedText = [];
  134. const positions=JSON.parse(this.value)
  135. // const positions = [
  136. // { row: 2, col: 9, pointName: "E-1" },
  137. // { row: 2, col: 11, pointName: "E-2" },
  138. // { row: 2, col: 13, pointName: "E-3" },
  139. // { row: 4, col: 9, pointName: "E-4" },
  140. // { row: 4, col: 11, pointName: "E-5" },
  141. // { row: 4, col: 13, pointName: "E-6" },
  142. // { row: 6, col: 9, pointName: "E-7" },
  143. // { row: 6, col: 11, pointName: "E-8" },
  144. // { row: 6, col: 13, pointName: "E-9" },
  145. // { row: 8, col: 9, pointName: "E-10" },
  146. // { row: 8, col: 11, pointName: "E-11" },
  147. // { row: 8, col: 13, pointName: "E-12" },
  148. // { row: 10, col: 9, pointName: "E-13" },
  149. // { row: 10, col: 11, pointName: "E-14" },
  150. // { row: 10, col: 13, pointName: "E-15" },
  151. // { row: 12, col: 9, pointName: "E-16" },
  152. // { row: 12, col: 11, pointName: "E-17" },
  153. // { row: 12, col: 13, pointName: "E-18" },
  154. // ];
  155. positions.forEach((pos, index) => {
  156. const x = pos.col * 50; // 每个单元格宽度为50
  157. const y = pos.row * 50; // 每个单元格高度为50
  158. const labelText = pos.pointName; // 对应的文字
  159. const point = new Image();
  160. point.src = imageSrc;
  161. point.onload = () => {
  162. const knovaImage = new Konva.Image({
  163. x: x,
  164. y: y,
  165. image: point,
  166. width: 50,
  167. height: 50,
  168. draggable: false,
  169. });
  170. this.layer.add(knovaImage);
  171. // 普通矩形
  172. const rect = new Konva.Rect({
  173. x: x - 3,
  174. y: y + 55,
  175. width: 60,
  176. height: 25,
  177. cornerRadius: 10,
  178. stroke: "red",
  179. strokeWidth: 2,
  180. fill: "white",
  181. });
  182. this.layer.add(rect);
  183. this.rects.push(rect); // 用数组存储
  184. // 普通文字
  185. const text = new Konva.Text({
  186. x: x + 12,
  187. y: y + 60,
  188. fontSize: 20,
  189. text: labelText,
  190. fontFamily: "Calibri",
  191. fill: "red",
  192. });
  193. this.layer.add(text);
  194. this.texts.push(text); // 用数组存储
  195. this.layer.draw();
  196. };
  197. });
  198. },
  199. },
  200. };
  201. </script>
  202. <style scoped lang="scss">
  203. #container {
  204. width: 100%;
  205. height: 100%;
  206. }
  207. .mapdata{
  208. width: 100%;
  209. height: 100%;
  210. display: flex;
  211. }
  212. .left{
  213. flex: 1;
  214. display: flex;
  215. flex-direction: column;
  216. }
  217. </style>