File size: 1,700 Bytes
89ce340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<template>
  <svg class="grid-lines">
    <path 
      :style="{
        transform: `scale(${canvasScale})`,
      }" 
      :d="path" 
      fill="none" 
      :stroke="gridColor" 
      stroke-width="0.3" 
      stroke-dasharray="5"
    ></path>
  </svg>
</template>

<script lang="ts" setup>
import { computed } from 'vue'
import tinycolor from 'tinycolor2'
import { storeToRefs } from 'pinia'
import { useMainStore, useSlidesStore } from '@/store'
import type { SlideBackground } from '@/types/slides'

const { canvasScale, gridLineSize } = storeToRefs(useMainStore())
const { currentSlide, viewportRatio, viewportSize } = storeToRefs(useSlidesStore())

const background = computed<SlideBackground | undefined>(() => currentSlide.value?.background)

// 计算网格线的颜色,避免与背景的颜色太接近
const gridColor = computed(() => {
  const bgColor = background.value?.color || '#fff'
  const colorList = ['#000', '#fff']
  return tinycolor.mostReadable(bgColor, colorList, { includeFallbackColors: true }).setAlpha(.5).toRgbString()
})

// 网格路径
const path = computed(() => {
  const maxX = viewportSize.value
  const maxY = viewportSize.value * viewportRatio.value

  let p = ''
  for (let i = 0; i <= Math.floor(maxY / gridLineSize.value); i++) {
    p += `M0 ${i * gridLineSize.value} L${maxX} ${i * gridLineSize.value} `
  }
  for (let i = 0; i <= Math.floor(maxX / gridLineSize.value); i++) {
    p += `M${i * gridLineSize.value} 0 L${i * gridLineSize.value} ${maxY} `
  }
  return p
})
</script>

<style lang="scss" scoped>
.grid-lines {
  width: 100%;
  height: 100%;
  overflow: visible;
  z-index: 999;
  pointer-events: none;

  @include absolute-0();
}
</style>