3328350766
6 天以前 761eb03d6b3bebd0b197179564c84d89d3d12a0d
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
<template>
  <div
    id="wrapper"
    class="wrapper vue-ruler-wrapper"
    :style="{
      width: width + thick + 'px',
      height: height + thick + 'px'
    }"
  >
    <i
      :class="{
        'iconfont-bigscreen': true,
        'icon-eye': isShowReferLine,
        'icon-eye-close': !isShowReferLine
      }"
      @click="handleCornerClick"
    />
    <SketchRule
      :key="scale"
      :lang="lang"
      :thick="thick"
      :scale="scale"
      :width="width"
      :height="height"
      :start-x="startX"
      :start-y="startY"
      :shadow="shadow"
      :hor-line-arr="[...lines.h, ...presetLines.h]"
      :ver-line-arr="[...lines.v, ...presetLines.v]"
      :palette="Palette"
      :is-show-refer-line="isShowReferLine"
      :corner-active="cornerActive"
      @handleLine="handleLine"
      @onCornerClick="handleCornerClick"
    />
    <div
      id="screens"
      ref="screensRef"
      :style="{
        width: innerWidth + 'px',
        height: innerHeight + 'px'
      }"
      @scroll="throttleScroll"
    >
      <div
        id="screen-container"
        ref="containerRef"
        class="screen-container grid-bg"
        :style="containerRefStyle"
      >
        <div
          id="canvas"
          :style="canvasStyle"
        >
          <slot />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import SketchRule from 'vue-sketch-ruler'
import { mapState, mapMutations } from 'vuex'
import throttle from 'lodash/throttle'
export default {
  components: {
    SketchRule
  },
  props: {
    width: {
      type: Number,
      default: 500
    },
    height: {
      type: Number,
      default: 400
    },
    pageWidth: {
      type: Number,
      default: 1920
    },
    pageHeight: {
      type: Number,
      default: 1080
    }
  },
  data () {
    return {
      canvasLeft: 0, // 存储画布到视口的left距离
      canvasTop: 0, // 存储画布到视口的top距离
      isDrag: false, // 小地图白块是否拖拽
      startX: 0,
      startY: 0,
      lines: {
        h: [],
        v: []
      },
      thick: 20,
      lang: 'zh-CN', // 中英文
      isShowRuler: true, // 显示标尺
      isShowReferLine: true, // 显示参考线
      cornerActive: true, // 左上角激活状态
      Palette: {
        bgColor: 'rgba(225,225,225, 0)',
        longfgColor: '#BABBBC',
        shortfgColor: '#C8CDD0',
        fontColor: '#7D8694',
        shadowColor: 'transparent',
        lineColor: '#0089d0',
        borderColor: '#transparent',
        cornerActiveColor: 'rgb(235, 86, 72, 0.6)'
      },
 
      containerRefStyle: {
        width: this.width + 'px',
        height: this.height + 'px'
      },
      innerHeight: 0,
      innerWidth: 0
    }
  },
  watch: {
    // 缩放改变的时候,改变startX,startY
    scale (scale) {
      // 防抖调用方法
      this.throttleScroll()
    },
    pageWidth (pageWidth) {
      if (this.fitZoom === this.zoom) {
        this.initZoom()
      }
    },
    pageHeight (pageHeight) {
      if (this.fitZoom === this.zoom) {
        this.initZoom()
      }
    }
  },
  computed: {
    ...mapState('bigScreen', {
      scale: state => state.zoom / 100,
      fitZoom: state => state.fitZoom,
      zoom: state => state.zoom
    }),
    presetLines () {
      const presetLine = this.$store.state.bigScreen.presetLine
      // { type: 'h', site: y || 0 },
      const v = presetLine?.filter(p => p.type === 'h')?.map(p => p.site)
      const h = presetLine?.filter(p => p.type === 'v')?.map(p => p.site)
      return {
        h,
        v
      }
    },
    shadow () {
      return {
        x: 0,
        y: 0,
        width: this.width,
        height: this.height
      }
    },
    canvasStyle () {
      return {
        width: this.width + 'px',
        height: this.height + 'px',
        transform: `scale(${this.scale})`,
        transformOrigin: '0 0 0'
      }
    }
  },
  mounted () {
    // 初始化canvasLeft canvasTop
    const canvasRect = document.querySelector('#canvas').getBoundingClientRect()
    this.canvasLeft = canvasRect.left
    this.canvasTop = canvasRect.top
    // 监听屏幕改变
    this.listenSize()
    this.initRuleHeight()
    this.throttleScroll()
    this.throttleDrag()
  },
  methods: {
    ...mapMutations('bigScreen', [
      'changeZoom',
      'changeFitZoom'
    ]),
    throttleDrag () {
      throttle(() => {
        this.dragSelection()
        this.viewMapDrag()
      }, 100)()
    },
    // 绑定滑块拖拽效果
    dragSelection () {
      const that = this
      const draggableElement = document.getElementById('selectionWin')
      const dragContainer = document.getElementById('selectWin')
      const screenElement = document.getElementById('screens')
      const screenContainer = document.getElementById('screen-container')
      const maxContainer = document.querySelector('.bs-page-design-wrap')
      // 鼠标按下的位置
      let posX, posY
      // 白色拖拽块相对于父盒子初始位置
      let initialX, initialY
      // 滚动条初始位置
      let scrollTop, scrollLeft
      draggableElement.addEventListener('mousedown', function (event) {
        that.isDrag = true
        posX = event.clientX
        posY = event.clientY
        initialX = draggableElement.getBoundingClientRect().left - dragContainer.getBoundingClientRect().left
        initialY = draggableElement.getBoundingClientRect().top - dragContainer.getBoundingClientRect().top
        scrollLeft = screenElement.scrollLeft
        scrollTop = screenElement.scrollTop
        maxContainer.addEventListener('mousemove', function (event) {
          // 在鼠标移动过程中判断出鼠标左键未点击,则停止拖拽
          if (event.buttons !== 1) {
            that.isDrag = false
          }
          if (that.isDrag) {
            event.preventDefault()
            // 鼠标移动距离
            let moveX = event.clientX - posX
            let moveY = event.clientY - posY
            // 避免白色拖拽移出边框
            if (moveX < -initialX) {
              moveX = -initialX
            } else if (moveX > dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width - initialX) {
              moveX = dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width - initialX
            }
            if (moveY < -initialY) {
              moveY = -initialY
            } else if (moveY > dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height - initialY) {
              moveY = dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height - initialY
            }
            const newX = moveX + initialX
            const newY = moveY + initialY
            // 移动拖拽白块
            draggableElement.style.left = newX + 'px'
            draggableElement.style.top = newY + 'px'
            // 移动比例
            const percentageX = moveX / (dragContainer.getBoundingClientRect().width - draggableElement.getBoundingClientRect().width)
            const percentageY = moveY / (dragContainer.getBoundingClientRect().height - draggableElement.getBoundingClientRect().height)
            // 进度条需要滚动的距离
            const scrollTopLength = percentageY * (screenContainer.getBoundingClientRect().height - screenElement.getBoundingClientRect().height)
            const scrollLeftLength = percentageX * (screenContainer.getBoundingClientRect().width - screenElement.getBoundingClientRect().width)
            screenElement.scrollLeft = scrollLeft + scrollLeftLength
            screenElement.scrollTop = scrollTop + scrollTopLength
          }
        })
      })
      maxContainer.addEventListener('mouseup', function (event) {
        that.isDrag = false
      })
      draggableElement.addEventListener('mouseup', function () {
        that.isDrag = false
      })
      // 禁止H5自带的拖拽事件
      draggableElement.ondragstart = function (ev) {
        ev.preventDefault()
      }
      draggableElement.ondragend = function (ev) {
        ev.preventDefault()
      }
      screenElement.ondragstart = function (ev) {
        ev.preventDefault()
      }
      screenElement.ondragend = function (ev) {
        ev.preventDefault()
      }
    },
    // 小地图拖拽
    viewMapDrag () {
      const mapElement = document.getElementById('minimap')
      const mapDragElement = document.getElementById('mapHeader')
      const pageElement = document.querySelector('.bs-page-design-wrap')
      let mapDrag = false
      let curX, curY
      let curBottom, curRight
      mapDragElement.addEventListener('mousedown', function (event) {
        mapDrag = true
        curX = event.clientX
        curY = event.clientY
        curBottom = window.getComputedStyle(mapElement).bottom
        curRight = window.getComputedStyle(mapElement).right
        pageElement.addEventListener('mousemove', function (event) {
          if (mapDrag) {
            event.preventDefault()
            const dragX = event.clientX - curX
            const dragY = event.clientY - curY
            mapElement.style.bottom = parseInt(curBottom) - dragY + 'px'
            mapElement.style.right = parseInt(curRight) - dragX + 'px'
          }
        })
      })
      pageElement.addEventListener('mouseup', function () {
        mapDrag = false
      })
      mapDragElement.addEventListener('mouseup', function () {
        mapDrag = false
      })
    },
    listenSize () {
      window.onresize = throttle(() => {
        this.initRuleHeight()
      }, 100)
    },
    initRuleHeight () {
      setTimeout(() => {
        const screensRect = document
          .querySelector('.grid-wrap-box')
          ?.getBoundingClientRect()
        if (!screensRect) {
          return
        }
 
        // 30是grid-wrap-box的底部工具栏高度
        this.innerHeight = screensRect.height
        this.innerWidth = screensRect.width
        this.diffX = this.width - screensRect.width
        this.diffY = this.height - screensRect.height
 
        this.containerRefStyle = {
          width: this.diffX > 0 ? ((this.width + this.diffX + this.thick + 30) + 'px') : (this.width + 'px'),
          height: this.diffY > 0 ? ((this.height + this.diffY + this.thick + 30) + 'px') : (this.height + 'px')
        }
        if (this.fitZoom === this.zoom) {
          this.initZoom()
        }
      })
    },
    handleLine (lines) {
      this.lines = lines
    },
    handleCornerClick () {
      this.isShowReferLine = !this.isShowReferLine
      this.cornerActive = !this.cornerActive
    },
    throttleScroll () {
      throttle(() => {
        this.handleScroll()
      }, 100)()
    },
    handleScroll () {
      const screensRect = document
        .querySelector('#screens')
        .getBoundingClientRect()
      const canvasRect = document
        .querySelector('#canvas')
        .getBoundingClientRect()
      // const container = document.querySelector('#selectWin').getBoundingClientRect()
      const screenContainer = document.querySelector('#screen-container').getBoundingClientRect()
      const draggableElement = document.getElementById('selectionWin')
      // 标尺开始的刻度
      const startX = (screensRect.left + this.thick - canvasRect.left) / this.scale
      const startY = (screensRect.top + this.thick - canvasRect.top) / this.scale
 
      this.startX = startX >> 0
      this.startY = startY >> 0
 
      this.$emit('changeStart', {
        x: this.startX * this.scale + 50 - this.thick,
        y: this.startY * this.scale + 50 - this.thick
      })
      // 拖动进度条移动小地图
      if (!this.isDrag) {
        const leftDrag = canvasRect.left - this.canvasLeft
        const topDrag = canvasRect.top - this.canvasTop
        // 小方块需要移动的距离
        const leftLength = leftDrag / (screenContainer.width - screensRect.width - 9) * (150 - 30)
        const topLength = topDrag / (screenContainer.height - screensRect.height - 9) * (150 - 30)
        draggableElement.style.left = -leftLength + 'px'
        draggableElement.style.top = -topLength + 'px'
      }
    },
    // 保证画布能完整展示大屏
    initZoom () {
      // 横向比例
      const xRadio = this.innerWidth / (this.pageWidth + 120)
      // 纵向比例
      const yRadio = this.innerHeight / (this.pageHeight + 120)
      // 取最小的适应比例
      const scale = Math.floor(Math.min(xRadio * 100, yRadio * 100))
      if (scale < 100) {
        this.changeZoom(scale)
        this.changeFitZoom(scale)
      } else {
        this.changeZoom(100)
        this.changeFitZoom(100)
      }
    }
  }
</script>
<style lang="scss" scoped>
@import '../../BigScreenDesign/fonts/iconfont.css';
.wrapper {
  box-sizing: border-box;
  position: absolute;
 
  .iconfont-bigscreen {
    position: absolute;
    left: 0;
    top: 0;
    font-size: 16px;
    color: #fff;
    z-index: 999;
    cursor: pointer;
  }
}
 
#screens {
  position: absolute;
  overflow: scroll;
 
  // 滚动条美化,始终在最下方和最右方
  &::-webkit-scrollbar {
    width: 10px;
    height: 10px;
  }
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: #232832 !important;
  }
  &::-webkit-scrollbar-track {
    border-radius: 10px;
    background-color: transparent !important;
  }
}
 
.screen-container {
  position: absolute;
  overflow:hidden;
  width: 6000px;
  height: 6000px;
}
 
.scale-value {
  position: absolute;
  left: 0;
  bottom: 100%;
}
 
.button {
  position: absolute;
  left: 100px;
  bottom: 100%;
}
 
.button-ch {
  position: absolute;
  left: 200px;
  bottom: 100%;
}
.button-en {
  position: absolute;
  left: 230px;
  bottom: 100%;
}
 
#canvas {
  position: absolute;
  top: 50px;
  left: 50px;
}
::v-deep .line {
  border-left: 1px dashed #0089d0 !important;
  border-top: 1px dashed #0089d0 !important;
}
::v-deep .action {
  .value {
    background: #409EFF;
    padding: 4px;
    color: #fff;
  }
 
  .del {
    color: #409EFF;
  }
}
::v-deep .ruler, ::v-deep .corner {
  background: #151a26;
}
::v-deep .corner {
  z-index: 999;
  background: #151a26 !important;
}
 
::v-deep .mb-ruler {
  z-index: 998
}
.grid-bg {
  background-color: #2a2e33 !important;
  background-image: url(./images/canvas-bg.png);
    background-repeat: repeat;
    word-spacing: 10px;
}
 
::v-deep .lines {
  .line {
    .action {
      .del {
        color: #ffffff;
      }
 
      .value {
        color: #ffffff;
      }
    }
  }
}
</style>