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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. return translateBounds(
  65. getBoundsFromPoints(shape.points, shape.rotation),
  66. shape.point
  67. )
  68. },
  69. getCenter(shape) {
  70. return getBoundsCenter(this.getBounds(shape))
  71. },
  72. hitTest(shape, point) {
  73. let pt = vec.sub(point, shape.point)
  74. const min = +getShapeStyle(shape.style).strokeWidth
  75. return shape.points.some(
  76. (curr, i) =>
  77. i > 0 && vec.distanceToLineSegment(shape.points[i - 1], curr, pt) < min
  78. )
  79. },
  80. hitTestBounds(this, shape, brushBounds) {
  81. // Test axis-aligned shape
  82. if (shape.rotation === 0) {
  83. return (
  84. boundsContain(brushBounds, this.getBounds(shape)) ||
  85. intersectPolylineBounds(shape.points, brushBounds).length > 0
  86. )
  87. }
  88. // Test rotated shape
  89. const rBounds = this.getRotatedBounds(shape)
  90. if (!rotatedCache.has(shape)) {
  91. const c = getBoundsCenter(rBounds)
  92. rotatedCache.set(
  93. shape,
  94. shape.points.map((pt) =>
  95. vec.rotWith(vec.add(pt, shape.point), c, shape.rotation)
  96. )
  97. )
  98. }
  99. return (
  100. boundsContain(brushBounds, rBounds) ||
  101. intersectPolylineBounds(rotatedCache.get(shape), brushBounds).length > 0
  102. )
  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 // * sin?
  110. ? 1 - x / initialShapeBounds.width
  111. : x / initialShapeBounds.width),
  112. bounds.height *
  113. (scaleY < 0 // * cos?
  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. applyStyles(shape, style) {
  126. const styles = { ...shape.style, ...style }
  127. styles.isFilled = false
  128. styles.dash = DashStyle.Solid
  129. shape.style = styles
  130. shape.points = [...shape.points]
  131. return this
  132. },
  133. canStyleFill: false,
  134. })
  135. export default draw
  136. const simulatePressureSettings = {
  137. simulatePressure: true,
  138. }
  139. const realPressureSettings = {
  140. easing: (t: number) => t * t,
  141. simulatePressure: false,
  142. // start: { taper: 1 },
  143. // end: { taper: 1 },
  144. }
  145. function renderPath(shape: DrawShape, style: ShapeStyles) {
  146. const styles = getShapeStyle(style)
  147. if (shape.points.length < 2) {
  148. pathCache.set(shape.points, '')
  149. return
  150. }
  151. const options =
  152. shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
  153. const stroke = getStroke(shape.points, {
  154. size: 1 + +styles.strokeWidth * 2,
  155. thinning: 0.85,
  156. end: { taper: +styles.strokeWidth * 20 },
  157. start: { taper: +styles.strokeWidth * 20 },
  158. ...options,
  159. })
  160. pathCache.set(shape.points, getSvgPathFromStroke(stroke))
  161. }