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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { DashStyle, DrawShape, ShapeType } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { intersectPolylineBounds } from 'utils/intersections'
  6. import { boundsContainPolygon } from 'utils/bounds'
  7. import getStroke from 'perfect-freehand'
  8. import {
  9. getBoundsCenter,
  10. getBoundsFromPoints,
  11. getSvgPathFromStroke,
  12. translateBounds,
  13. } from 'utils/utils'
  14. import styled from 'styles'
  15. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  16. const pathCache = new WeakMap<DrawShape['points'], string>([])
  17. const draw = registerShapeUtils<DrawShape>({
  18. boundsCache: new WeakMap([]),
  19. create(props) {
  20. return {
  21. id: uuid(),
  22. type: ShapeType.Draw,
  23. isGenerated: false,
  24. name: 'Draw',
  25. parentId: 'page0',
  26. childIndex: 0,
  27. point: [0, 0],
  28. points: [],
  29. rotation: 0,
  30. isAspectRatioLocked: false,
  31. isLocked: false,
  32. isHidden: false,
  33. ...props,
  34. style: {
  35. ...defaultStyle,
  36. ...props.style,
  37. isFilled: false,
  38. },
  39. }
  40. },
  41. render(shape) {
  42. const { id, points, style } = shape
  43. const styles = getShapeStyle(style)
  44. if (!pathCache.has(points)) {
  45. pathCache.set(
  46. points,
  47. getSvgPathFromStroke(
  48. getStroke(points, {
  49. size: +styles.strokeWidth * 2,
  50. thinning: 0.9,
  51. end: { taper: 100 },
  52. start: { taper: 40 },
  53. })
  54. )
  55. )
  56. }
  57. if (points.length < 2) {
  58. return (
  59. <circle id={id} r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
  60. )
  61. }
  62. return <path id={id} d={pathCache.get(points)} fill={styles.stroke} />
  63. },
  64. getBounds(shape) {
  65. if (!this.boundsCache.has(shape)) {
  66. const bounds = getBoundsFromPoints(shape.points)
  67. this.boundsCache.set(shape, bounds)
  68. }
  69. return translateBounds(this.boundsCache.get(shape), shape.point)
  70. },
  71. getRotatedBounds(shape) {
  72. const bounds =
  73. this.boundsCache.get(shape) || getBoundsFromPoints(shape.points)
  74. const center = getBoundsCenter(bounds)
  75. const rotatedPts = shape.points.map((pt) =>
  76. vec.rotWith(pt, center, shape.rotation)
  77. )
  78. const rotatedBounds = translateBounds(
  79. getBoundsFromPoints(rotatedPts),
  80. shape.point
  81. )
  82. return rotatedBounds
  83. },
  84. getCenter(shape) {
  85. const bounds = this.getRotatedBounds(shape)
  86. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  87. },
  88. hitTest(shape, point) {
  89. let 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. const b = this.getBounds(shape)
  98. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  99. const rotatedCorners = [
  100. [b.minX, b.minY],
  101. [b.maxX, b.minY],
  102. [b.maxX, b.maxY],
  103. [b.minX, b.maxY],
  104. ].map((point) => vec.rotWith(point, center, shape.rotation))
  105. return (
  106. boundsContainPolygon(brushBounds, rotatedCorners) ||
  107. intersectPolylineBounds(
  108. shape.points.map((point) => vec.add(point, shape.point)),
  109. brushBounds
  110. ).length > 0
  111. )
  112. },
  113. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  114. const initialShapeBounds = this.boundsCache.get(initialShape)
  115. shape.points = initialShape.points.map(([x, y]) => {
  116. return [
  117. bounds.width *
  118. (scaleX < 0
  119. ? 1 - x / initialShapeBounds.width
  120. : x / initialShapeBounds.width),
  121. bounds.height *
  122. (scaleY < 0
  123. ? 1 - y / initialShapeBounds.height
  124. : y / initialShapeBounds.height),
  125. ]
  126. })
  127. const newBounds = getBoundsFromPoints(shape.points)
  128. shape.point = vec.sub(
  129. [bounds.minX, bounds.minY],
  130. [newBounds.minX, newBounds.minY]
  131. )
  132. return this
  133. },
  134. applyStyles(shape, style) {
  135. Object.assign(shape.style, style)
  136. shape.style.isFilled = false
  137. shape.style.dash = DashStyle.Solid
  138. return this
  139. },
  140. canStyleFill: false,
  141. })
  142. export default draw
  143. const DrawPath = styled('path', {
  144. strokeWidth: 0,
  145. })