ZZJ
2022-06-28 2cb264ec2b7c7dd9798d1821927104fad35bd063
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<template>
  <div
    class="s-cavas"
    :style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
  >
    <canvas
      ref="myCanvas"
      :width="canvasWidth"
      :height="canvasHeight"
      :style="`background:url(${canvasBg}) center / ${canvasWidth}px ${canvasHeight}px no-repeat; background-size: contain;`"
    ></canvas>
 
    <el-tooltip
      content="刷新底图"
      placement="bottom"
      popper-class="atooltip"
      v-if="isShowRefresh"
    >
      <span class="iconfont icongengxin" @click="refresh"></span>
    </el-tooltip>
    <p class="tip" :style="disabled ? `display:block;` : `display:none;`">
      批量配置方式不允许绘制区域,请选择摄像机进行区域绘制
    </p>
    <el-dialog
      title
      :visible.sync="visible"
      width="1150px"
      append-to-body
      :before-close="cancelFunc"
    >
      <canvas-dialog
        ref="bigCanvas"
        :canvasDataToChild="canvasData"
        :snapshot_url="canvasBg"
        @refresh="refresh"
      ></canvas-dialog>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelFunc" size="small">取 消</el-button>
        <el-button type="primary" @click="handleOk" size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import canvasDialog from "./Dialog";
import { updateSnapshot } from "@/api/camera";
export default {
  name: "myCanvas",
  components: {
    canvasDialog,
  },
  props: {
    divId: {
      default: "my-canvas",
      type: String,
    },
    isGB28181: {
      default: false,
      type: Boolean,
    },
    isShowRefresh: {
      default: true,
      type: Boolean,
    },
    sourceType: {
      default: 1,
      type: Number,
    },
    isShowDrawArrow: {
      default: false,
      type: Boolean,
    },
    disabled: {
      default: false,
      type: Boolean,
    },
    canvasDataShow: {
      default: () => {
        return { line: [], rect: [], arrow: [], polygon: [] };
      },
      type: Object,
    },
    currentCameraId: {
      type: String,
      default: "",
    },
    snapshot_url: {
      type: String,
      default: "",
    },
    isLink: {
      type: Boolean,
      default: false,
    },
    loading: {
      type: Boolean,
      default: false,
    },
    canvasWidth: {
      type: Number,
      default: 576,
    },
    canvasHeight: {
      type: Number,
      default: 324,
    },
    showProportion: {
      type: Number,
      default: 1.666,
    },
  },
  computed: {
    canvasBg() {
      if (this.snapshot_url) {
        // 数据栈自动上传处理
        if (this.snapshot_url.indexOf("/opt/vasystem") == 0) {
          return this.snapshot_url.replace("/opt/vasystem", "");
        }
 
        if (this.sourceType == 2) {
          return `${this.snapshot_url}`;
        } else {
          return `/httpImage/${this.snapshot_url}`;
        }
      } else {
        return this.blackImg;
      }
    },
  },
  data() {
    return {
      blackImg: require("../../assets/img/baseimg.png"),
      canvasData: {
        line: [],
        rect: [],
        arrow: [],
        polygon: [],
      }, // 最终输出的画布坐标数据
      canvasPic: new Image(), // 撤销用的图片
      canvasHistory: [], // 历史数据,以供撤销使用
      step: -1, // 记录索引,以供撤销使用
      c: null,
      ctx: null,
      visible: false,
      baseImg: undefined,
      //showProportion: 1.666
    };
  },
  watch: {
    loading: {
      handler(newVal, oldVal) {
        // console.log(newVal,'loading')
        if (newVal) {
          this.baseImg = "";
          this.refresh();
        }
      },
      deep: true,
    },
    canvasDataShow: {
      handler(newVal, oldVal) {
        // console.log(newVal, "canvasDataShow newVal");
        this.canvasData.line = newVal.line;
        this.canvasData.rect = newVal.rect;
        this.canvasData.arrow = newVal.arrow;
        this.canvasData.polygon = newVal.polygon;
        this.clickSelect(this.canvasData);
      },
      deep: true,
    },
  },
  mounted() {
    this.$nextTick(() => {
      // this.c = document.querySelector("#" + this.divId);
      this.c = this.$refs.myCanvas;
      this.ctx = this.c.getContext("2d");
      this.canvasData = JSON.parse(JSON.stringify(this.canvasDataShow));
      this.clickSelect(this.canvasData);
    });
  },
  methods: {
    // 获取canvas底图
    async getCanvasPic() {
      if (TreeDataPool.selectedNode.type == "MENU") {
        return;
      }
      // this.$emit('changeBaseImg',this.currentCameraId)
      this.$emit("changeLoading", true);
      if (this.currentCameraId) {
        let _this = this;
        await updateSnapshot(this.currentCameraId, this.isGB28181)
          .then((res) => {
            if (res.data.cameraId === _this.currentCameraId) {
              this.baseImg = res.data.snapshotUrl;
              this.$emit(
                "refresh",
                res.data.snapshotUrl,
                _this.currentCameraId
              );
              this.$forceUpdate();
              this.$notify({
                type: "success",
                message: "底图已刷新",
              });
            }
          })
          .catch((err) => {
            this.$notify({
              type: "error",
              message: "底图刷新失败",
            });
          });
        this.$emit("changeLoading", false);
      }
    },
    showModal() {
      // console.log(this.canvasData, "点击绘制的时候传递过去的数据");
      this.visible = true;
      this.$nextTick(() => {
        // this.$refs.bigCanvas.delCursor = {}
      });
    },
    cancelFunc() {
      this.visible = false;
      this.$refs.bigCanvas.cancel();
      // console.log("关闭了");
    },
    handleOk() {
      // 判断图形的名字是否重复
      // console.log("ok");
      this.$refs.bigCanvas.changeType("0");
      let repeatName = this.isRepeat();
      if (repeatName !== "") {
        this.$notify({
          type: "error",
          message: repeatName + "图形名称重复,请更正!",
        });
        return;
      }
      this.$notify({
        type: "success",
        message: "已保存绘制!",
      });
      // 每次保存关闭模态窗时都要把模态窗的数据写到父组件里来
      this.canvasData = this.$refs.bigCanvas.canvasData;
      // 顺便甩到更外层去
      this.$emit("fromCanvas", this.$refs.bigCanvas.canvasData);
      // console.log("你好", this.canvasData);
 
      this.clickSelect(this.canvasData);
      this.visible = false;
    },
    // 回显cavas数据
    // 点击选中变色 将当前页面所有路径重绘判断当前鼠标的坐标在哪个图形内 如果不传坐标参数就是回显的方法
    clickSelect(e) {
      this.ctx.clearRect(0, 0, this.c.width, this.c.height);
      let _this = this; // 集合中遍历需要将this转存一下使用
      _this.canvasData.line.forEach(function (v, i) {
        _this.ctx.strokeStyle = "yellow";
        _this.ctx.beginPath();
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        );
        _this.ctx.lineTo(
          v.location[1].x / _this.showProportion,
          v.location[1].y / _this.showProportion
        );
        _this.ctx.stroke();
        _this.showRemarks(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion,
          v.name
        );
        _this.c.style.cursor = "default";
        if (e && _this.ctx.isPointInStroke(e.offsetX, e.offsetY)) {
          // 如果传入了事件坐标,就用isPointInStroke判断一下
          // 如果当前环境覆盖了该坐标,就将图形的index放到数组里
          // 当鼠标移入之后将当前的模式切换为选中模式
          _this.type = "0";
          _this.delCursor.type = "1";
          _this.delCursor.index = i;
          // 将当前元素标红
          _this.ctx.strokeStyle = "red";
          _this.ctx.beginPath();
          _this.ctx.moveTo(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion
          );
          _this.ctx.lineTo(
            v.location[1].x / _this.showProportion,
            v.location[1].y / _this.showProportion
          );
          _this.ctx.stroke();
          _this.showRemarks(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion,
            v.name
          );
          _this.c.style.cursor = "pointer";
        }
      });
      _this.canvasData.rect.forEach(function (v, i) {
        _this.ctx.strokeStyle = "yellow";
        _this.ctx.beginPath();
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        );
        _this.ctx.lineTo(
          v.location[1].x / _this.showProportion,
          v.location[1].y / _this.showProportion
        );
        _this.ctx.lineTo(
          v.location[2].x / _this.showProportion,
          v.location[2].y / _this.showProportion
        );
        _this.ctx.lineTo(
          v.location[3].x / _this.showProportion,
          v.location[3].y / _this.showProportion
        );
        _this.ctx.lineTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        );
        _this.ctx.stroke();
        _this.showRemarks(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion,
          v.name
        );
        _this.c.style.cursor = "default";
        if (e && _this.ctx.isPointInPath(e.offsetX, e.offsetY)) {
          // 如果传入了事件坐标,就用isPointInStroke判断一下
          // 当鼠标移入之后将当前的模式切换为选中模式
          _this.type = "0";
          _this.delCursor.type = "2";
          _this.delCursor.index = i;
          // 将当前元素标红
          _this.ctx.strokeStyle = "red";
          _this.ctx.beginPath();
          _this.ctx.moveTo(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion
          );
          _this.ctx.lineTo(
            v.location[1].x / _this.showProportion,
            v.location[1].y / _this.showProportion
          );
          _this.ctx.lineTo(
            v.location[2].x / _this.showProportion,
            v.location[2].y / _this.showProportion
          );
          _this.ctx.lineTo(
            v.location[3].x / _this.showProportion,
            v.location[3].y / _this.showProportion
          );
          _this.ctx.lineTo(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion
          );
          _this.ctx.stroke();
          _this.showRemarks(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion,
            v.name
          );
          _this.c.style.cursor = "pointer";
        }
      });
      _this.canvasData.arrow.forEach(function (v, i) {
        _this.ctx.strokeStyle = "yellow";
        // _this.ctx.beginPath()
        // _this.ctx.moveTo(v.location[0].x / 2, v.location[0].y / 2)
        // _this.ctx.lineTo(v.location[1].x / 2, v.location[1].y / 2)
        // _this.ctx.stroke()
        _this.drawArrow(
          _this.ctx,
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion,
          v.location[1].x / _this.showProportion,
          v.location[1].y / _this.showProportion,
          20,
          30,
          "yellow"
        ); // 绘制方法
        _this.showRemarks(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion,
          v.name
        );
        _this.c.style.cursor = "default";
        if (e && _this.ctx.isPointInStroke(e.offsetX, e.offsetY)) {
          // 如果传入了事件坐标,就用isPointInStroke判断一下
          // 如果当前环境覆盖了该坐标,就将图形的index放到数组里
          // 当鼠标移入之后将当前的模式切换为选中模式
          _this.type = "0";
          _this.delCursor.type = "4";
          _this.delCursor.index = i;
          // 将当前元素标红
          _this.ctx.strokeStyle = "red";
          //   _this.ctx.beginPath()
          //   _this.ctx.moveTo(v.location[0].x / _this.showProportion, v.location[0].y / _this.showProportion)
          //   _this.ctx.lineTo(v.location[1].x / _this.showProportion, v.location[1].y / _this.showProportion)
          //   _this.ctx.stroke()
          _this.drawArrow(
            _this.ctx,
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion,
            v.location[1].x / _this.showProportion,
            v.location[1].y / _this.showProportion,
            20,
            30,
            "red"
          ); // 绘制方法
          _this.showRemarks(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion,
            v.name
          );
          _this.c.style.cursor = "pointer";
        }
      });
      _this.canvasData.polygon.forEach(function (v, i) {
        if (v.location.length === 0) {
          return;
        }
        _this.ctx.strokeStyle = "yellow";
        _this.ctx.beginPath();
        _this.ctx.moveTo(
          v.location[0].x / _this.showProportion,
          v.location[0].y / _this.showProportion
        );
        for (let i = 1; i < v.location.length; i++) {
          _this.ctx.lineTo(
            v.location[i].x / _this.showProportion,
            v.location[i].y / _this.showProportion
          );
        }
        _this.ctx.closePath();
        _this.ctx.stroke();
        _this.showRemarks(
          v.location[v.location.length - 1].x / _this.showProportion,
          v.location[v.location.length - 1].y / _this.showProportion,
          v.name
        );
        _this.c.style.cursor = "default";
        if (e && _this.ctx.isPointInPath(e.offsetX, e.offsetY)) {
          // 如果传入了事件坐标,就用isPointInStroke判断一下
          // 如果当前环境覆盖了该坐标,就将图形的index放到数组里
          // 当鼠标移入之后将当前的模式切换为选中模式
          _this.type = "0";
          _this.delCursor.type = "5";
          _this.delCursor.index = i;
          // 将当前元素标红
          _this.ctx.strokeStyle = "red";
          _this.ctx.beginPath();
          _this.ctx.moveTo(
            v.location[0].x / _this.showProportion,
            v.location[0].y / _this.showProportion
          );
          for (let i = 1; i < v.location.length; i++) {
            _this.ctx.lineTo(
              v.location[i].x / _this.showProportion,
              v.location[i].y / _this.showProportion
            );
          }
          _this.ctx.closePath();
          _this.ctx.stroke();
          _this.showRemarks(
            v.location[v.location.length - 1].x / _this.showProportion,
            v.location[v.location.length - 1].y / _this.showProportion,
            v.name
          );
          _this.c.style.cursor = "pointer";
        }
      });
    },
    // 回显图形备注
    showRemarks(x, y, remarks) {
      this.ctx.moveTo(x, y - 10); // 因为放大之后是y-20,所以缩小版的为y-10
      this.ctx.fillStyle = "green"; // 设置填充颜色为绿色
      this.ctx.font = '10px "微软雅黑"'; // 设置字体
      this.ctx.textBaseline = "bottom"; // 设置字体底线对齐绘制基线
      this.ctx.textAlign = "left"; // 设置字体对齐的方式
      this.ctx.fillText(remarks, x, y - 10); // 填充文字
    },
    // 箭头绘制函数
    drawArrow(ctx, fromX, fromY, toX, toY, theta, headlen, width, color) {
      // ctx:Canvas绘图环境
      // fromX, fromY:起点坐标(也可以换成p1,只不过它是一个数组)
      // toX, toY:终点坐标 (也可以换成p2,只不过它是一个数组)
      // theta:三角斜边一直线夹角
      // headlen:三角斜边长度
      // width:箭头线宽度
      // color:箭头颜色
      theta = typeof theta !== "undefined" ? theta : 30;
      headlen = typeof theta !== "undefined" ? headlen : 10;
      width = typeof width !== "undefined" ? width : 1;
      color = typeof color !== "undefined" ? color : "yellow";
      // 计算各角度和对应的P2,P3坐标
      let angle = (Math.atan2(fromY - toY, fromX - toX) * 180) / Math.PI;
      let angle1 = ((angle + theta) * Math.PI) / 180;
      let angle2 = ((angle - theta) * Math.PI) / 180;
      let topX = headlen * Math.cos(angle1);
      let topY = headlen * Math.sin(angle1);
      let botX = headlen * Math.cos(angle2);
      let botY = headlen * Math.sin(angle2);
 
      ctx.save();
      ctx.beginPath();
      let arrowX = fromX - topX;
      let arrowY = fromY - topY;
      ctx.moveTo(arrowX, arrowY);
      ctx.moveTo(fromX, fromY);
      ctx.lineTo(toX, toY);
      arrowX = toX + topX;
      arrowY = toY + topY;
      ctx.moveTo(arrowX, arrowY);
      ctx.lineTo(toX, toY);
      arrowX = toX + botX;
      arrowY = toY + botY;
      ctx.lineTo(arrowX, arrowY);
      ctx.strokeStyle = color;
      ctx.lineWidth = width;
      ctx.stroke();
      ctx.restore();
    },
    async refresh() {
      // console.log(this.loading,'刷新底图',this.snapshot_url)
      if (!this.currentCameraId) {
        return false;
      }
      // await this.$emit('changeLoading', true)
      this.getCanvasPic();
    },
    // 判断图形名称是否重复
    isRepeat() {
      let _this = this; // 集合中遍历需要将this转存一下使用
      let nameArray = [];
      for (const v of _this.$refs.bigCanvas.canvasData.line) {
        for (const item of nameArray) {
          if (v.name === item) {
            return v.name;
          }
        }
        nameArray.push(v.name);
      }
      for (const v of _this.$refs.bigCanvas.canvasData.arrow) {
        for (const item of nameArray) {
          if (v.name === item) {
            return v.name;
          }
        }
        nameArray.push(v.name);
      }
      for (const v of _this.$refs.bigCanvas.canvasData.rect) {
        for (const item of nameArray) {
          if (v.name === item) {
            return v.name;
          }
        }
        nameArray.push(v.name);
      }
      for (const v of _this.$refs.bigCanvas.canvasData.polygon) {
        for (const item of nameArray) {
          if (v.name === item) {
            return v.name;
          }
        }
        nameArray.push(v.name);
      }
      return "";
    },
  },
};
</script>
<style lang="scss" scoped>
canvas {
  position: absolute;
  top: 0;
  left: 0;
}
.s-cavas {
  // width: 576px;
  // height: 324px;
  margin-top: 10px;
  position: relative;
  overflow: auto;
  @media screen {
  }
  .tip {
    position: absolute;
    width: 350px;
    left: 80px;
    top: 90px;
    color: white;
    font-size: 1.5rem;
  }
  span {
    position: relative;
    left: 265px;
    top: 6px;
    font-size: 2.5rem;
    cursor: pointer;
    color: #3d68e1;
  }
}
.img-icon {
  width: 600px;
  height: 480px;
  background-size: cover;
  /* 抑制选中 */
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
}
</style>