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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { uniqueId } from 'utils/utils'
  2. import 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: uniqueId(),
  24. seed: Math.random(),
  25. type: ShapeType.Draw,
  26. isGenerated: false,
  27. name: 'Draw',
  28. parentId: 'page1',
  29. childIndex: 0,
  30. point: [0, 0],
  31. points: [],
  32. rotation: 0,
  33. isAspectRatioLocked: false,
  34. isLocked: false,
  35. isHidden: false,
  36. ...props,
  37. style: {
  38. ...defaultStyle,
  39. ...props.style,
  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 > 0 && points.length < 3) {
  50. return (
  51. <circle id={id} r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
  52. )
  53. }
  54. return (
  55. <g id={id}>
  56. {points.length > 3 &&
  57. vec.dist(points[0], points[points.length - 1]) < 8 && (
  58. <polyline
  59. points={points.map((pt) => pt.slice(0, 2)).join(',')}
  60. fill={styles.fill}
  61. stroke="none"
  62. strokeWidth={0}
  63. />
  64. )}
  65. <path d={pathCache.get(points)} fill={styles.stroke} />
  66. </g>
  67. )
  68. },
  69. getBounds(shape) {
  70. if (!this.boundsCache.has(shape)) {
  71. const bounds = getBoundsFromPoints(shape.points)
  72. this.boundsCache.set(shape, bounds)
  73. }
  74. return translateBounds(this.boundsCache.get(shape), shape.point)
  75. },
  76. getRotatedBounds(shape) {
  77. return translateBounds(
  78. getBoundsFromPoints(shape.points, shape.rotation),
  79. shape.point
  80. )
  81. },
  82. getCenter(shape) {
  83. return getBoundsCenter(this.getBounds(shape))
  84. },
  85. hitTest(shape, point) {
  86. let pt = vec.sub(point, shape.point)
  87. const min = +getShapeStyle(shape.style).strokeWidth
  88. return shape.points.some(
  89. (curr, i) =>
  90. i > 0 && vec.distanceToLineSegment(shape.points[i - 1], curr, pt) < min
  91. )
  92. },
  93. hitTestBounds(this, shape, brushBounds) {
  94. // Test axis-aligned shape
  95. if (shape.rotation === 0) {
  96. return (
  97. boundsContain(brushBounds, this.getBounds(shape)) ||
  98. intersectPolylineBounds(
  99. shape.points,
  100. translateBounds(brushBounds, vec.neg(shape.point))
  101. ).length > 0
  102. )
  103. }
  104. // Test rotated shape
  105. const rBounds = this.getRotatedBounds(shape)
  106. if (!rotatedCache.has(shape)) {
  107. const c = getBoundsCenter(getBoundsFromPoints(shape.points))
  108. rotatedCache.set(
  109. shape,
  110. shape.points.map((pt) => vec.rotWith(pt, c, shape.rotation))
  111. )
  112. }
  113. return (
  114. boundsContain(brushBounds, rBounds) ||
  115. intersectPolylineBounds(
  116. rotatedCache.get(shape),
  117. translateBounds(brushBounds, vec.neg(shape.point))
  118. ).length > 0
  119. )
  120. },
  121. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  122. const initialShapeBounds = this.boundsCache.get(initialShape)
  123. shape.points = initialShape.points.map(([x, y, r]) => {
  124. return [
  125. bounds.width *
  126. (scaleX < 0 // * sin?
  127. ? 1 - x / initialShapeBounds.width
  128. : x / initialShapeBounds.width),
  129. bounds.height *
  130. (scaleY < 0 // * cos?
  131. ? 1 - y / initialShapeBounds.height
  132. : y / initialShapeBounds.height),
  133. r,
  134. ]
  135. })
  136. const newBounds = getBoundsFromPoints(shape.points)
  137. shape.point = vec.sub(
  138. [bounds.minX, bounds.minY],
  139. [newBounds.minX, newBounds.minY]
  140. )
  141. return this
  142. },
  143. applyStyles(shape, style) {
  144. const styles = { ...shape.style, ...style }
  145. styles.dash = DashStyle.Solid
  146. shape.style = styles
  147. shape.points = [...shape.points]
  148. return this
  149. },
  150. onSessionComplete(shape) {
  151. const bounds = this.getBounds(shape)
  152. const [x1, y1] = vec.sub([bounds.minX, bounds.minY], shape.point)
  153. shape.points = shape.points.map(([x0, y0, p]) => [x0 - x1, y0 - y1, p])
  154. this.translateTo(shape, vec.add(shape.point, [x1, y1]))
  155. return this
  156. },
  157. canStyleFill: true,
  158. })
  159. export default draw
  160. const simulatePressureSettings = {
  161. simulatePressure: true,
  162. }
  163. const realPressureSettings = {
  164. easing: (t: number) => t * t,
  165. simulatePressure: false,
  166. start: { taper: 1 },
  167. end: { taper: 1 },
  168. }
  169. function renderPath(shape: DrawShape, style: ShapeStyles) {
  170. const styles = getShapeStyle(style)
  171. if (shape.points.length < 2) {
  172. pathCache.set(shape.points, '')
  173. return
  174. }
  175. const options =
  176. shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
  177. const stroke = getStroke(shape.points, {
  178. size: 1 + +styles.strokeWidth * 2,
  179. thinning: 0.85,
  180. end: { taper: +styles.strokeWidth * 20 },
  181. start: { taper: +styles.strokeWidth * 20 },
  182. ...options,
  183. })
  184. pathCache.set(shape.points, getSvgPathFromStroke(stroke))
  185. }