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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { DashStyle, DrawShape, ShapeStyles, ShapeType } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { intersectPolylineBounds } from 'utils/intersections'
  6. import { boundsContain, boundsContainPolygon } from 'utils/bounds'
  7. import getStroke from 'perfect-freehand'
  8. import {
  9. getBoundsCenter,
  10. getBoundsFromPoints,
  11. getRotatedCorners,
  12. getSvgPathFromStroke,
  13. rotateBounds,
  14. translateBounds,
  15. } from 'utils/utils'
  16. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  17. const rotatedCache = new WeakMap<DrawShape, number[][]>([])
  18. const pathCache = new WeakMap<DrawShape['points'], string>([])
  19. const draw = registerShapeUtils<DrawShape>({
  20. boundsCache: new WeakMap([]),
  21. create(props) {
  22. return {
  23. id: uuid(),
  24. type: ShapeType.Draw,
  25. isGenerated: false,
  26. name: 'Draw',
  27. parentId: 'page0',
  28. childIndex: 0,
  29. point: [0, 0],
  30. points: [],
  31. rotation: 0,
  32. isAspectRatioLocked: false,
  33. isLocked: false,
  34. isHidden: false,
  35. ...props,
  36. style: {
  37. ...defaultStyle,
  38. ...props.style,
  39. isFilled: false,
  40. },
  41. }
  42. },
  43. render(shape) {
  44. const { id, points, style } = shape
  45. const styles = getShapeStyle(style)
  46. if (!pathCache.has(points)) {
  47. renderPath(shape, style)
  48. }
  49. if (points.length < 2) {
  50. return (
  51. <circle id={id} r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
  52. )
  53. }
  54. return <path id={id} d={pathCache.get(points)} fill={styles.stroke} />
  55. },
  56. getBounds(shape) {
  57. if (!this.boundsCache.has(shape)) {
  58. const bounds = getBoundsFromPoints(shape.points)
  59. this.boundsCache.set(shape, bounds)
  60. }
  61. return translateBounds(this.boundsCache.get(shape), shape.point)
  62. },
  63. getRotatedBounds(shape) {
  64. const rBounds = translateBounds(
  65. getBoundsFromPoints(shape.points, shape.rotation),
  66. shape.point
  67. )
  68. const bounds = this.getBounds(shape)
  69. const delta = vec.sub(getBoundsCenter(bounds), getBoundsCenter(rBounds))
  70. return translateBounds(rBounds, delta)
  71. },
  72. getCenter(shape) {
  73. const bounds = this.getRotatedBounds(shape)
  74. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  75. },
  76. hitTest(shape, point) {
  77. let pt = vec.sub(point, shape.point)
  78. const min = +getShapeStyle(shape.style).strokeWidth
  79. return shape.points.some(
  80. (curr, i) =>
  81. i > 0 && vec.distanceToLineSegment(shape.points[i - 1], curr, pt) < min
  82. )
  83. },
  84. hitTestBounds(this, shape, brushBounds) {
  85. // Test axis-aligned shape
  86. if (shape.rotation === 0) {
  87. return (
  88. boundsContain(brushBounds, this.getBounds(shape)) ||
  89. intersectPolylineBounds(shape.points, brushBounds).length > 0
  90. )
  91. }
  92. // Test rotated shape
  93. const rBounds = this.getRotatedBounds(shape)
  94. if (!rotatedCache.has(shape)) {
  95. const c = getBoundsCenter(rBounds)
  96. rotatedCache.set(
  97. shape,
  98. shape.points.map((pt) =>
  99. vec.rotWith(vec.add(pt, shape.point), c, shape.rotation)
  100. )
  101. )
  102. }
  103. return (
  104. boundsContain(brushBounds, rBounds) ||
  105. intersectPolylineBounds(rotatedCache.get(shape), brushBounds).length > 0
  106. )
  107. },
  108. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  109. const initialShapeBounds = this.boundsCache.get(initialShape)
  110. shape.points = initialShape.points.map(([x, y]) => {
  111. return [
  112. bounds.width *
  113. (scaleX < 0
  114. ? 1 - x / initialShapeBounds.width
  115. : x / initialShapeBounds.width),
  116. bounds.height *
  117. (scaleY < 0
  118. ? 1 - y / initialShapeBounds.height
  119. : y / initialShapeBounds.height),
  120. ]
  121. })
  122. const newBounds = getBoundsFromPoints(shape.points)
  123. shape.point = vec.sub(
  124. [bounds.minX, bounds.minY],
  125. [newBounds.minX, newBounds.minY]
  126. )
  127. return this
  128. },
  129. applyStyles(shape, style) {
  130. const styles = { ...shape.style, ...style }
  131. styles.isFilled = false
  132. styles.dash = DashStyle.Solid
  133. shape.style = styles
  134. shape.points = [...shape.points]
  135. return this
  136. },
  137. canStyleFill: false,
  138. })
  139. export default draw
  140. const simulatePressureSettings = {
  141. simulatePressure: true,
  142. }
  143. const realPressureSettings = {
  144. easing: (t: number) => t * t,
  145. simulatePressure: false,
  146. // start: { taper: 1 },
  147. // end: { taper: 1 },
  148. }
  149. function renderPath(shape: DrawShape, style: ShapeStyles) {
  150. const styles = getShapeStyle(style)
  151. if (shape.points.length < 2) {
  152. pathCache.set(shape.points, '')
  153. return
  154. }
  155. const options =
  156. shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
  157. const stroke = getStroke(shape.points, {
  158. size: 1 + +styles.strokeWidth * 2,
  159. thinning: 0.85,
  160. end: { taper: +styles.strokeWidth * 20 },
  161. start: { taper: +styles.strokeWidth * 20 },
  162. ...options,
  163. })
  164. pathCache.set(shape.points, getSvgPathFromStroke(stroke))
  165. }