闫增涛
2025-04-14 d7e53e63dd6c435e226d9f08cde31ca35131c911
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
<template>
  <div class="page-con">
    <div class="canvas-box">
      <div class="canvas-header">
        <el-icon @click="closeDialog"><CloseBold /></el-icon>
      </div>
      <div class="canvas-body">
        <canvas id="canvasBoard" width="1000" height="750"></canvas>
      </div>
    </div>
    <div class="canvas-tool">
      <div class="brushBox">
        <div class="toolList">
          <el-popover placement="top" width="250" trigger="click">
            <div class="popinnerBox">
              <div class="brush">
                <div class="thickness">
                  <div
                    :class="toolData.thicknessActive == '1' ? 'small active hover' : 'small hover'"
                    @click="selectThickness('1')"
                  ></div>
                  <div
                    :class="toolData.thicknessActive == '3' ? 'middle active hover' : 'middle hover'"
                    @click="selectThickness('3')"
                  ></div>
                  <div
                    :class="toolData.thicknessActive == '5' ? 'large active hover' : 'large hover'"
                    @click="selectThickness('5')"
                  ></div>
                </div>
                <div class="lineStyle">
                  <div class="lineTypeBox">
                    <div
                      :class="
                        toolData.lineTypeActive == 'solid' ? 'typeItem lineTypeActive hover' : 'typeItem hover'
                      "
                      @click="selectLineType('solid')"
                    >
                      <div class="solid"></div>
                      <div class="activeIcon" v-if="toolData.lineTypeActive == 'solid'">
                        <img :src="xuanzhong" />
                      </div>
                    </div>
                    <div
                      :class="
                        toolData.lineTypeActive == 'dashed' ? 'typeItem lineTypeActive hover' : 'typeItem hover'
                      "
                      @click="selectLineType('dashed')"
                    >
                      <div class="dashed"></div>
                      <div class="activeIcon" v-if="toolData.lineTypeActive == 'dashed'">
                        <img :src="xuanzhong" />
                      </div>
                    </div>
                  </div>
                  <div class="colorSelectBox">
                    <div
                      v-for="item in colorList"
                      :key="item.key"
                      class="flex1 hover"
                      @click="lineColorSelect(item)"
                    >
                      <div :style="{ background: item.key }" class="scribeItem">
                        <img :src="xuanzhong" v-if="item.key == toolData.lineColorActive" />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <template #reference>
              <div class="floatToolItem hover" @click="toolSelectHandle('huabi')">
                <el-tooltip class="box-item" effect="dark" content="画笔" placement="bottom">
                  <img :src="huabi2" alt="" class="imgBox" />
                </el-tooltip>
              </div>
            </template>
          </el-popover>
          <el-popover placement="top" width="250" trigger="click">
            <div class="popinnerBox">
              <div class="lineStyle">
                <div class="lineTypeBox">
                  <div
                    :class="
                      toolData.fontSizeActive == '12' ? 'typeItem lineTypeActive hover' : 'typeItem hover'
                    "
                    @click="selectfontSize('12')"
                  >
                    小
                    <div class="activeIcon" v-if="toolData.fontSizeActive == '12'">
                      <img :src="xuanzhong" />
                    </div>
                  </div>
                  <div
                    :class="
                      toolData.fontSizeActive == '16' ? 'typeItem lineTypeActive hover' : 'typeItem hover'
                    "
                    @click="selectfontSize('16')"
                  >
                    中
                    <div class="activeIcon" v-if="toolData.fontSizeActive == '16'">
                      <img :src="xuanzhong" />
                    </div>
                  </div>
                  <div
                    :class="
                      toolData.fontSizeActive == '20' ? 'typeItem lineTypeActive hover' : 'typeItem hover'
                    "
                    @click="selectfontSize('20')"
                  >
                    大
                    <div class="activeIcon" v-if="toolData.fontSizeActive == '20'">
                      <img :src="xuanzhong" />
                    </div>
                  </div>
                </div>
                <div class="lineStyle">
                  <div class="colorSelectBox">
                    <div
                      v-for="item in colorList"
                      :key="item.key"
                      class="flex1 hover"
                      @click="lineColorSelect(item)"
                    >
                      <div :style="{ background: item.key }" class="scribeItem">
                        <img :src="xuanzhong" v-if="item.key == toolData.lineColorActive" />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <template #reference>
              <div class="floatToolItem hover" @click="toolSelectHandle('wenzi')">
                <el-tooltip class="box-item" effect="dark" content="文字" placement="bottom">
                  <img :src="wenzi2" alt="" class="imgBox" />
                </el-tooltip>
              </div>
            </template>
          </el-popover>
          <el-popover placement="top" width="250" trigger="click">
            <div class="popinnerBox">
              <div class="graphBox">
                <div class="graphSelect">
                  <div class="flex1">
                    <div :class="toolData.graphType == 'square' ? 'acitveGraphType' : ''">
                      <div class="square hover" @click="graphSelect('square')"></div>
                    </div>
                  </div>
                  <div class="flex1">
                    <div :class="toolData.graphType == 'rotundity' ? 'acitveGraphType' : ''">
                      <div class="rotundity hover" @click="graphSelect('rotundity')"></div>
                    </div>
                  </div>
                  <div class="flex1">
                    <div :class="toolData.graphType == 'triangle' ? 'acitveGraphType' : ''">
                      <div class="triangle hover" @click="graphSelect('triangle')"></div>
                    </div>
                  </div>
                  <div class="flex1">
                    <div :class="toolData.graphType == 'lineSegment' ? 'acitveGraphType' : ''">
                      <div class="lineSegment hover" @click="graphSelect('lineSegment')">/</div>
                    </div>
                  </div>
                </div>
                <div class="lineStyle">
                  <div class="colorSelectBox">
                    <div
                      v-for="item in colorList"
                      :key="item.key"
                      class="flex1 hover"
                      @click="lineColorSelect(item)"
                    >
                      <div :style="{ background: item.key }" class="scribeItem">
                        <img :src="xuanzhong" v-if="item.key == toolData.lineColorActive" />
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <template #reference>
              <div class="floatToolItem hover" @click="toolSelectHandle('tuxing')">
                <el-tooltip class="box-item" effect="dark" content="图形" placement="bottom">
                  <img :src="tuxing" alt="" class="imgBox" />
                </el-tooltip>
              </div>
            </template>
          </el-popover>
          <div class="floatToolItem hover" @click="toolSelectHandle('chexiao')">
            <el-tooltip class="box-item" effect="dark" content="撤销" placement="bottom">
              <img :src="clearPrevious" alt="" class="imgBox" />
            </el-tooltip>
          </div>
          <!-- <div class="floatToolItem" @click="toolSelectHandle('xiangpi')">
            <div class="imgBox">
              <img :src="clear" alt="" />
            </div>
          </div> -->
          <div class="floatToolItem hover" @click="toolSelectHandle('qingchu')">
            <el-tooltip class="box-item" effect="dark" content="清除" placement="bottom">
              <img :src="qingchu2" alt="" class="imgBox" />
            </el-tooltip>
          </div>
          <div class="layOutTool"></div>
          <div class="floatToolItem hover" @click="toolSelectHandle('tuichu')">
            <el-tooltip class="box-item" effect="dark" content="退出" placement="bottom">
              <img :src="tuichu" alt="" class="imgBox" />
            </el-tooltip>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { requestCtx } from '../../assets/js/config'
import { defineProps, inject, defineEmits, onMounted, reactive, ref, watch } from 'vue'
import clearPrevious from '@/assets/images/operation/clearPrevious.png'
import clear from '@/assets/images/operation/clear.png'
import tuichu from '@/assets/images/operation/tuichu.png'
import qingchu2 from '@/assets/images/operation/qingchu.png'
import tuxing from '@/assets/images/operation/tuxing.png'
import xuanzhong from '@/assets/images/operation/xuanzhong.png'
import huabi2 from '@/assets/images/operation/huabi.svg'
import wenzi2 from '@/assets/images/operation/wenzi.png'
 
let canvas: any = null
let text: any = null
let activeEl: any = null
function canvasOnMouseDownDel(opt) {
  // 右键,且在元素上右键
  // button: 1-左键;2-中键;3-右键
  // 在画布上右键,target 为 null
  if (opt.button === 1 && opt.target) {
    // 获取当前元素
    activeEl = opt.target
  }
}
onMounted(() => {
  canvas = new fabric.Canvas('canvasBoard')
  // 按下鼠标
  canvas.on('mouse:down', canvasOnMouseDownDel)
  canvas.setZoom(1); // 设置画布缩放比例为1
  canvas.absolutePan({ x: 0, y: 0 });
})
const emit = defineEmits(['whiteBoardClose'])
const closeDialog = () => {
  emit('whiteBoardClose', false)
}
 
//画笔操作栏事件
const toolData = reactive({
  activeTool: '',
  fontSizeActive: '16', // 文字操作文字大小
  thicknessActive: '3', //画笔选中粗细
  lineTypeActive: 'solid', //画笔选中线类型
  lineColorActive: '#333', //画笔选中线颜色
  graphType: '' //图形类型,方形,圆形...
})
const colorList = reactive([
  {
    label: '黄色',
    key: '#F5E12A'
  },
  {
    label: '绿色',
    key: '#76F0AE'
  },
  {
    label: '蓝色',
    key: '#59CFF5'
  },
  {
    label: '紫色',
    key: '#CAA5FC'
  },
  {
    label: '粉色',
    key: '#F5A0B9'
  }
])
const toolSelectHandle = (title) => {
  toolData.activeTool = title
  toolData.lineColorActive = ''
  canvas.isDrawingMode = false
  switch (title) {
    case 'huabi':
      canvas.skipTargetFind = false
      canvas.isDrawingMode = true
      toolData.graphType = ''
      canvas.freeDrawingBrush.color = '#333'
      canvas.freeDrawingBrush.width = '3'
      canvas.on('mouse:down', '') // 鼠标在画布上按下
      canvas.on('mouse:up', '')
      canvas.on('mouse:move', '')
      break
    case 'wenzi':
      canvas.skipTargetFind = false
      canvas.isDrawingMode = false
      toolData.graphType = ''
      text = new fabric.IText('请输入文本', {
        left: 50,
        top: 50,
        fontSize: toolData.fontSizeActive
      })
      canvas.add(text)
      canvas.on('mouse:down', '') // 鼠标在画布上按下
      canvas.on('mouse:up', '')
      canvas.on('mouse:move', '')
      break
    case 'tuxing':
      canvas.isDrawingMode = false
      canvas.selectionColor = 'transparent'
      canvas.skipTargetFind = true
      toolData.lineColorActive = '#000'
      break
    case 'chexiao':
      canvas.skipTargetFind = false
      canvas.isDrawingMode = false
      canvas.remove(activeEl)
      canvas.requestRenderAll()
      activeEl = null
      break
    case 'tuichu':
      canvas.skipTargetFind = false
      canvas.isDrawingMode = false
      emit('whiteBoardClose', false)
      break
    case 'qingchu':
      canvas.clear()
      break
  }
}
 
//选中画笔粗细
const selectThickness = (str) => {
  toolData.thicknessActive = str
  canvas.freeDrawingBrush.width = toolData.thicknessActive
}
//画笔线类型
const selectLineType = (str) => {
  toolData.lineTypeActive = str
  if (toolData.lineTypeActive == 'dashed') {
    canvas.freeDrawingBrush.strokeDashArray = [10]
  } else {
    canvas.freeDrawingBrush.strokeDashArray = null
  }
}
 
//画笔/文字/图形颜色选择
const lineColorSelect = (item) => {
  toolData.lineColorActive = item.key
  if ((toolData.activeTool == 'huabi')) {
    canvas.freeDrawingBrush.color = item.key
  }
  if ((toolData.activeTool == 'wenzi')) {
    text.fill = toolData.lineColorActive
  }
}
 
//文字大小
const selectfontSize = (str) => {
  toolData.fontSizeActive = str
  text.fontSize = toolData.fontSizeActive
}
 
//选择图形
const graphSelect = (type) => {
  toolData.graphType = type
  switch (type) {
    case 'square':
      canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下
      canvas.on('mouse:up', canvasMouseUp)
      canvas.selectionBorderColor = toolData.lineColorActive
      break
    case 'rotundity':
      canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下
      canvas.on('mouse:up', canvasMouseUp)
      canvas.on('mouse:move', canvasMouseMove)
      break
    case 'triangle':
      canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下
      canvas.on('mouse:up', canvasMouseUp)
      canvas.on('mouse:move', canvasMouseMove)
      break
    case 'lineSegment':
      canvas.on('mouse:down', canvasMouseDown) // 鼠标在画布上按下
      canvas.on('mouse:move', canvasMouseMove) // 鼠标在画布上移动
      canvas.on('mouse:up', canvasMouseUp)
      break
  }
}
 
let downPoint = null // 鼠标按下的坐标
let upPoint = null
let currentCircle = null // 临时圆,创建圆的时候使用
let currentTriangle = null // 临时三角形
let currentLine = null // 临时线段
// 鼠标在画布上按下
function canvasMouseDown(e) {
  downPoint = e.absolutePointer
  if (toolData.graphType === 'rotundity') {
    currentCircle = new fabric.Circle({
      top: downPoint.y,
      left: downPoint.x,
      radius: 0,
      fill: 'transparent',
      stroke: toolData.lineColorActive
    })
 
    canvas.add(currentCircle)
  }
  if (toolData.graphType === 'triangle') {
    currentTriangle = new fabric.Triangle({
      top: downPoint.y,
      left: downPoint.x,
      width: 0,
      height: 0,
      fill: 'transparent',
      stroke: toolData.lineColorActive
    })
    canvas.add(currentTriangle)
  }
  if (toolData.graphType === 'lineSegment') {
    currentLine = new fabric.Line(
      [
        downPoint.x,
        downPoint.y, // 起始点坐标
        downPoint.x,
        downPoint.y // 结束点坐标
      ],
      {
        stroke: toolData.lineColorActive // 笔触颜色
      }
    )
    canvas.add(currentLine)
  }
}
// 鼠标在画布上松开
function canvasMouseUp(e) {
  if (toolData.graphType === 'square') {
    // 创建矩形
    createRect(e.absolutePointer)
  }
 
  if (toolData.graphType === 'rotundity') {
    if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) {
      canvas.remove(currentCircle)
    } else {
      if (currentCircle) {
        currentCircle.set('stroke', toolData.lineColorActive)
      }
    }
    currentCircle = null
  }
 
  if (toolData.graphType === 'triangle') {
    if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) {
      canvas.remove(currentTriangle)
    } else {
      currentTriangle.set('stroke', toolData.lineColorActive)
    }
    currentTriangle = null
  }
 
  if (toolData.graphType === 'lineSegment') {
    if (JSON.stringify(downPoint) === JSON.stringify(upPoint)) {
      canvas.remove(currentLine)
    } else {
      currentLine.set('stroke', toolData.lineColorActive)
    }
    currentLine = null
  }
}
// 创建矩形
function createRect(pointer) {
  // 点击事件,不生成矩形
  if (JSON.stringify(downPoint) === JSON.stringify(pointer)) {
    return
  }
 
  // 创建矩形
  // 矩形参数计算
  let top = Math.min(downPoint.y, pointer.y)
  let left = Math.min(downPoint.x, pointer.x)
  let width = Math.abs(downPoint.x - pointer.x)
  let height = Math.abs(downPoint.y - pointer.y)
 
  // 矩形对象
  const rect = new fabric.Rect({
    top,
    left,
    width,
    height,
    fill: 'transparent',
    stroke: toolData.lineColorActive
  })
  // 将矩形添加到画布上
  canvas.add(rect)
  downPoint = null
}
// 鼠标在画布上移动
function canvasMouseMove(e) {
  if (toolData.graphType === 'rotundity' && currentCircle) {
    const currentPoint = e.absolutePointer
 
    let radius =
      Math.min(Math.abs(downPoint.x - currentPoint.x), Math.abs(downPoint.y - currentPoint.y)) / 2
    let top = currentPoint.y > downPoint.y ? downPoint.y : downPoint.y - radius * 2
    let left = currentPoint.x > downPoint.x ? downPoint.x : downPoint.x - radius * 2
 
    currentCircle.set('radius', radius)
    currentCircle.set('top', top)
    currentCircle.set('left', left)
 
    canvas.requestRenderAll()
  }
  if (toolData.graphType === 'triangle' && currentTriangle) {
    const currentPoint = e.absolutePointer
 
    let width = Math.abs(downPoint.x - currentPoint.x)
    let height = Math.abs(downPoint.y - currentPoint.y)
 
    let top = currentPoint.y > downPoint.y ? downPoint.y : currentPoint.y
    let left = currentPoint.x > downPoint.x ? downPoint.x : currentPoint.x
 
    currentTriangle.set('width', width)
    currentTriangle.set('height', height)
    currentTriangle.set('top', top)
    currentTriangle.set('left', left)
    canvas.requestRenderAll()
  }
  if (toolData.graphType === 'lineSegment' && currentLine) {
    const currentPoint = e.absolutePointer
 
    currentLine.set('x2', currentPoint.x)
    currentLine.set('y2', currentPoint.y)
 
    canvas.requestRenderAll()
  }
}
 
 
</script>
<style scoped lang="less">
.page-con {
  height: 100%;
  width: 100%;
 
  // position: relative;
  .canvas-header {
    text-align: right;
    padding: 10px;
    background-color: #f1f1f1;
    border-radius: 5px 5px 0 0;
  }
  .canvas-box {
    width: 1000px;
    margin: 20px auto;
    background-color: #fff;
    border-radius: 5px;
  }
  .brushBox {
    width: 290px;
    margin: 0 auto;
    border-radius: 5px;
    background: #fff;
    overflow: hidden;
    box-shadow: 0px 0px 6px 1px rgba(0, 0, 0, 0.3);
 
    .toolList {
      display: flex;
      justify-content: center;
      align-items: center;
 
      .floatToolItem {
        margin: 6px 8px;
        padding: 5px;
        width: 30px;
        border-radius: 5px;
        display: flex;
        justify-content: center;
        align-items: center;
        // flex: 1;
        font-size: 14px;
 
        .imgBox {
          height: 18px;
          width: 18px;
          text-align: center;
          margin: 0 auto;
        }
      }
 
      .floatToolItem:hover {
        background-color: rgba(44, 44, 44, 0.2);
      }
 
      .layOutTool {
        height: 20px;
        margin-right: 5px;
        border-left: 1px solid #e0e0e0;
      }
    }
  }
}
</style>