Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { uniqueId } from 'utils/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. defaultProps: {
  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. style: defaultStyle,
  35. },
  36. shouldRender(shape, prev) {
  37. return shape.points !== prev.points || shape.style !== prev.style
  38. },
  39. render(shape) {
  40. const { id, points, style } = shape
  41. const styles = getShapeStyle(style)
  42. if (!pathCache.has(points)) {
  43. renderPath(shape, style)
  44. }
  45. if (points.length > 0 && points.length < 3) {
  46. return (
  47. <g id={id}>
  48. <circle r={+styles.strokeWidth * 0.618} fill={styles.stroke} />
  49. </g>
  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. return this
  151. },
  152. onSessionComplete(shape) {
  153. const bounds = this.getBounds(shape)
  154. const [x1, y1] = vec.sub([bounds.minX, bounds.minY], shape.point)
  155. shape.points = shape.points.map(([x0, y0, p]) => [x0 - x1, y0 - y1, p])
  156. this.translateTo(shape, vec.add(shape.point, [x1, y1]))
  157. return this
  158. },
  159. })
  160. export default draw
  161. const simulatePressureSettings = {
  162. simulatePressure: true,
  163. }
  164. const realPressureSettings = {
  165. easing: (t: number) => t * t,
  166. simulatePressure: false,
  167. start: { taper: 1 },
  168. end: { taper: 1 },
  169. }
  170. function renderPath(shape: DrawShape, style: ShapeStyles) {
  171. const styles = getShapeStyle(style)
  172. if (shape.points.length < 2) {
  173. pathCache.set(shape.points, '')
  174. return
  175. }
  176. const options =
  177. shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
  178. const stroke = getStroke(shape.points, {
  179. size: 1 + +styles.strokeWidth * 2,
  180. thinning: 0.85,
  181. end: { taper: +styles.strokeWidth * 20 },
  182. start: { taper: +styles.strokeWidth * 20 },
  183. ...options,
  184. })
  185. pathCache.set(shape.points, getSvgPathFromStroke(stroke))
  186. }
  187. function renderFill(shape: DrawShape, style: ShapeStyles) {
  188. const styles = getShapeStyle(style)
  189. if (shape.points.length < 2) {
  190. polygonCache.set(shape.points, '')
  191. return
  192. }
  193. return polygonCache.set(
  194. shape.points,
  195. getSvgPathFromStroke(
  196. getStrokePoints(shape.points, {
  197. size: 1 + +styles.strokeWidth * 2,
  198. thinning: 0.85,
  199. end: { taper: +styles.strokeWidth * 20 },
  200. start: { taper: +styles.strokeWidth * 20 },
  201. }).map((pt) => pt.point)
  202. )
  203. )
  204. }