You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

draw.tsx 4.9KB

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