Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

draw.tsx 4.7KB

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