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

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