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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { uniqueId } from 'utils'
  2. import vec from 'utils/vec'
  3. import { DashStyle, DrawShape, ShapeStyles, ShapeType } from 'types'
  4. import { intersectPolylineBounds } from 'utils/intersections'
  5. import getStroke, { getStrokePoints } from 'perfect-freehand'
  6. import {
  7. getBoundsCenter,
  8. getBoundsFromPoints,
  9. getSvgPathFromStroke,
  10. translateBounds,
  11. boundsContain,
  12. } from 'utils'
  13. import { defaultStyle, getShapeStyle } from 'state/shape-styles'
  14. import { registerShapeUtils } from './register'
  15. const rotatedCache = new WeakMap<DrawShape, number[][]>([])
  16. const pathCache = new WeakMap<DrawShape['points'], string>([])
  17. const polygonCache = new WeakMap<DrawShape['points'], string>([])
  18. const draw = registerShapeUtils<DrawShape>({
  19. boundsCache: new WeakMap([]),
  20. canStyleFill: true,
  21. create(props) {
  22. return {
  23. id: uniqueId(),
  24. type: ShapeType.Draw,
  25. isGenerated: false,
  26. name: 'Draw',
  27. parentId: 'page1',
  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. },
  40. }
  41. },
  42. shouldRender(shape, prev) {
  43. return shape.points !== prev.points || shape.style !== prev.style
  44. },
  45. render(shape) {
  46. const { id, points, style } = shape
  47. const styles = getShapeStyle(style)
  48. if (!pathCache.has(points)) {
  49. renderPath(shape, style)
  50. }
  51. if (points.length > 0 && points.length < 3) {
  52. return (
  53. <g id={id}>
  54. <circle r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
  55. </g>
  56. )
  57. }
  58. const shouldFill =
  59. points.length > 3 &&
  60. vec.dist(points[0], points[points.length - 1]) < +styles.strokeWidth * 2
  61. if (shouldFill && !polygonCache.has(points)) {
  62. renderFill(shape, style)
  63. }
  64. return (
  65. <g id={id}>
  66. {shouldFill && (
  67. <path
  68. d={polygonCache.get(points)}
  69. fill={styles.fill}
  70. strokeWidth="0"
  71. stroke="none"
  72. />
  73. )}
  74. <path d={pathCache.get(points)} fill={styles.stroke} />
  75. </g>
  76. )
  77. },
  78. getBounds(shape) {
  79. if (!this.boundsCache.has(shape)) {
  80. const bounds = getBoundsFromPoints(shape.points)
  81. this.boundsCache.set(shape, bounds)
  82. }
  83. return translateBounds(this.boundsCache.get(shape), shape.point)
  84. },
  85. getRotatedBounds(shape) {
  86. return translateBounds(
  87. getBoundsFromPoints(shape.points, shape.rotation),
  88. shape.point
  89. )
  90. },
  91. getCenter(shape) {
  92. return getBoundsCenter(this.getBounds(shape))
  93. },
  94. hitTest(shape, point) {
  95. const pt = vec.sub(point, shape.point)
  96. const min = +getShapeStyle(shape.style).strokeWidth
  97. return shape.points.some(
  98. (curr, i) =>
  99. i > 0 && vec.distanceToLineSegment(shape.points[i - 1], curr, pt) < min
  100. )
  101. },
  102. hitTestBounds(this, shape, brushBounds) {
  103. // Test axis-aligned shape
  104. if (shape.rotation === 0) {
  105. return (
  106. boundsContain(brushBounds, this.getBounds(shape)) ||
  107. intersectPolylineBounds(
  108. shape.points,
  109. translateBounds(brushBounds, vec.neg(shape.point))
  110. ).length > 0
  111. )
  112. }
  113. // Test rotated shape
  114. const rBounds = this.getRotatedBounds(shape)
  115. if (!rotatedCache.has(shape)) {
  116. const c = getBoundsCenter(getBoundsFromPoints(shape.points))
  117. rotatedCache.set(
  118. shape,
  119. shape.points.map((pt) => vec.rotWith(pt, c, shape.rotation))
  120. )
  121. }
  122. return (
  123. boundsContain(brushBounds, rBounds) ||
  124. intersectPolylineBounds(
  125. rotatedCache.get(shape),
  126. translateBounds(brushBounds, vec.neg(shape.point))
  127. ).length > 0
  128. )
  129. },
  130. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  131. const initialShapeBounds = this.boundsCache.get(initialShape)
  132. shape.points = initialShape.points.map(([x, y, r]) => {
  133. return [
  134. bounds.width *
  135. (scaleX < 0 // * sin?
  136. ? 1 - x / initialShapeBounds.width
  137. : x / initialShapeBounds.width),
  138. bounds.height *
  139. (scaleY < 0 // * cos?
  140. ? 1 - y / initialShapeBounds.height
  141. : y / initialShapeBounds.height),
  142. r,
  143. ]
  144. })
  145. const newBounds = getBoundsFromPoints(shape.points)
  146. shape.point = vec.sub(
  147. [bounds.minX, bounds.minY],
  148. [newBounds.minX, newBounds.minY]
  149. )
  150. return this
  151. },
  152. applyStyles(shape, style) {
  153. const styles = { ...shape.style, ...style }
  154. styles.dash = DashStyle.Solid
  155. shape.style = styles
  156. return this
  157. },
  158. onSessionComplete(shape) {
  159. const bounds = this.getBounds(shape)
  160. const [x1, y1] = vec.sub([bounds.minX, bounds.minY], shape.point)
  161. shape.points = shape.points.map(([x0, y0, p]) => [x0 - x1, y0 - y1, p])
  162. this.translateTo(shape, vec.add(shape.point, [x1, y1]))
  163. return this
  164. },
  165. })
  166. export default draw
  167. const simulatePressureSettings = {
  168. simulatePressure: true,
  169. }
  170. const realPressureSettings = {
  171. easing: (t: number) => t * t,
  172. simulatePressure: false,
  173. start: { taper: 1 },
  174. end: { taper: 1 },
  175. }
  176. function renderPath(shape: DrawShape, style: ShapeStyles) {
  177. const styles = getShapeStyle(style)
  178. if (shape.points.length < 2) {
  179. pathCache.set(shape.points, '')
  180. return
  181. }
  182. const options =
  183. shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
  184. const stroke = getStroke(shape.points, {
  185. size: 1 + +styles.strokeWidth * 2,
  186. thinning: 0.85,
  187. end: { taper: +styles.strokeWidth * 20 },
  188. start: { taper: +styles.strokeWidth * 20 },
  189. ...options,
  190. })
  191. pathCache.set(shape.points, getSvgPathFromStroke(stroke))
  192. }
  193. function renderFill(shape: DrawShape, style: ShapeStyles) {
  194. const styles = getShapeStyle(style)
  195. if (shape.points.length < 2) {
  196. polygonCache.set(shape.points, '')
  197. return
  198. }
  199. return polygonCache.set(
  200. shape.points,
  201. getSvgPathFromStroke(
  202. getStrokePoints(shape.points, {
  203. size: 1 + +styles.strokeWidth * 2,
  204. thinning: 0.85,
  205. end: { taper: +styles.strokeWidth * 20 },
  206. start: { taper: +styles.strokeWidth * 20 },
  207. }).map((pt) => pt.point)
  208. )
  209. )
  210. }