Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

draw.tsx 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { DrawShape, ShapeType } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { intersectPolylineBounds } from 'utils/intersections'
  6. import { boundsContainPolygon } from 'utils/bounds'
  7. import getStroke from 'perfect-freehand'
  8. import {
  9. getBoundsFromPoints,
  10. getSvgPathFromStroke,
  11. translateBounds,
  12. } from 'utils/utils'
  13. import { DotCircle } from 'components/canvas/misc'
  14. import { shades } from 'lib/colors'
  15. const pathCache = new WeakMap<number[][], string>([])
  16. const draw = registerShapeUtils<DrawShape>({
  17. boundsCache: new WeakMap([]),
  18. create(props) {
  19. return {
  20. id: uuid(),
  21. type: ShapeType.Draw,
  22. isGenerated: false,
  23. name: 'Draw',
  24. parentId: 'page0',
  25. childIndex: 0,
  26. point: [0, 0],
  27. points: [[0, 0]],
  28. rotation: 0,
  29. ...props,
  30. style: {
  31. strokeWidth: 2,
  32. strokeLinecap: 'round',
  33. strokeLinejoin: 'round',
  34. ...props.style,
  35. stroke: 'transparent',
  36. },
  37. }
  38. },
  39. render(shape) {
  40. const { id, point, points } = shape
  41. if (points.length < 2) {
  42. return <DotCircle id={id} cx={point[0]} cy={point[1]} r={3} />
  43. }
  44. if (!pathCache.has(points)) {
  45. pathCache.set(points, getSvgPathFromStroke(getStroke(points)))
  46. }
  47. return <path id={id} d={pathCache.get(points)} />
  48. },
  49. applyStyles(shape, style) {
  50. Object.assign(shape.style, style)
  51. return this
  52. },
  53. getBounds(shape) {
  54. if (!this.boundsCache.has(shape)) {
  55. const bounds = getBoundsFromPoints(shape.points)
  56. this.boundsCache.set(shape, bounds)
  57. }
  58. return translateBounds(this.boundsCache.get(shape), shape.point)
  59. },
  60. getRotatedBounds(shape) {
  61. return this.getBounds(shape)
  62. },
  63. getCenter(shape) {
  64. const bounds = this.getBounds(shape)
  65. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  66. },
  67. hitTest(shape, point) {
  68. let pt = vec.sub(point, shape.point)
  69. let prev = shape.points[0]
  70. for (let i = 1; i < shape.points.length; i++) {
  71. let curr = shape.points[i]
  72. if (vec.distanceToLineSegment(prev, curr, pt) < 4) {
  73. return true
  74. }
  75. prev = curr
  76. }
  77. return false
  78. },
  79. hitTestBounds(this, shape, brushBounds) {
  80. const b = this.getBounds(shape)
  81. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  82. const rotatedCorners = [
  83. [b.minX, b.minY],
  84. [b.maxX, b.minY],
  85. [b.maxX, b.maxY],
  86. [b.minX, b.maxY],
  87. ].map((point) => vec.rotWith(point, center, shape.rotation))
  88. return (
  89. boundsContainPolygon(brushBounds, rotatedCorners) ||
  90. intersectPolylineBounds(
  91. shape.points.map((point) => vec.add(point, shape.point)),
  92. brushBounds
  93. ).length > 0
  94. )
  95. },
  96. rotateTo(shape, rotation) {
  97. shape.rotation = rotation
  98. return this
  99. },
  100. translateTo(shape, point) {
  101. shape.point = point
  102. return this
  103. },
  104. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  105. const initialShapeBounds = this.boundsCache.get(initialShape)
  106. shape.points = initialShape.points.map(([x, y]) => {
  107. return [
  108. bounds.width *
  109. (scaleX < 0
  110. ? 1 - x / initialShapeBounds.width
  111. : x / initialShapeBounds.width),
  112. bounds.height *
  113. (scaleY < 0
  114. ? 1 - y / initialShapeBounds.height
  115. : y / initialShapeBounds.height),
  116. ]
  117. })
  118. const newBounds = getBoundsFromPoints(shape.points)
  119. shape.point = vec.sub(
  120. [bounds.minX, bounds.minY],
  121. [newBounds.minX, newBounds.minY]
  122. )
  123. return this
  124. },
  125. transformSingle(shape, bounds, info) {
  126. this.transform(shape, bounds, info)
  127. return this
  128. },
  129. setParent(shape, parentId) {
  130. shape.parentId = parentId
  131. return this
  132. },
  133. setChildIndex(shape, childIndex) {
  134. shape.childIndex = childIndex
  135. return this
  136. },
  137. setPoints(shape, points) {
  138. // const bounds = getBoundsFromPoints(points)
  139. // const corner = [bounds.minX, bounds.minY]
  140. // const nudged = points.map((point) => vec.sub(point, corner))
  141. // this.boundsCache.set(shape, translategetBoundsFromPoints(nudged))
  142. // shape.point = vec.add(shape.point, corner)
  143. shape.points = points
  144. return this
  145. },
  146. canTransform: true,
  147. canChangeAspectRatio: true,
  148. })
  149. export default draw