GridLines.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <template>
  2. <SvgWrapper class="grid-lines">
  3. <path
  4. :style="{
  5. transform: `scale(${canvasScale})`,
  6. }"
  7. :d="path"
  8. fill="none"
  9. :stroke="gridColor"
  10. stroke-width="0.3"
  11. shape-rendering="crispEdges"
  12. stroke-dasharray="5"
  13. ></path>
  14. </SvgWrapper>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, computed } from 'vue'
  18. import tinycolor from 'tinycolor2'
  19. import { useStore } from '@/store'
  20. import { VIEWPORT_SIZE, VIEWPORT_ASPECT_RATIO } from '@/configs/canvas'
  21. import { SlideBackground } from '@/types/slides'
  22. export default defineComponent({
  23. name: 'grid-lines',
  24. setup() {
  25. const store = useStore()
  26. const canvasScale = computed(() => store.state.canvasScale)
  27. const background = computed<SlideBackground | undefined>(() => store.getters.currentSlide.background)
  28. const gridColor = computed(() => {
  29. if(!background.value || background.value.type === 'image') return 'rgba(100, 100, 100, 0.5)'
  30. const color = background.value.color
  31. const rgba = tinycolor(color).toRgb()
  32. const newRgba = {
  33. r: rgba.r > 128 ? rgba.r - 128 : rgba.r + 127,
  34. g: rgba.g > 128 ? rgba.g - 128 : rgba.g + 127,
  35. b: rgba.b > 128 ? rgba.b - 128 : rgba.b + 127,
  36. a: 0.5
  37. }
  38. return `rgba(${[newRgba.r, newRgba.g, newRgba.b, newRgba.a].join(',')})`
  39. })
  40. const gridSize = 50
  41. const getPath = () => {
  42. const maxX = VIEWPORT_SIZE
  43. const maxY = VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO
  44. let path = ''
  45. for(let i = 0; i <= Math.floor(maxY / gridSize); i++) {
  46. path += `M0 ${i * gridSize}, L${maxX} ${i * gridSize}`
  47. }
  48. for(let i = 0; i <= Math.floor(maxX / gridSize); i++) {
  49. path += `M${i * gridSize} 0, L${i * gridSize} ${maxY}`
  50. }
  51. return path
  52. }
  53. return {
  54. canvasScale,
  55. gridColor,
  56. width: VIEWPORT_SIZE,
  57. height: VIEWPORT_SIZE * VIEWPORT_ASPECT_RATIO,
  58. path: getPath(),
  59. }
  60. },
  61. })
  62. </script>
  63. <style lang="scss" scoped>
  64. .grid-lines {
  65. width: 100%;
  66. height: 100%;
  67. position: absolute;
  68. top: 0;
  69. left: 0;
  70. bottom: 0;
  71. right: 0;
  72. overflow: visible;
  73. }
  74. </style>