Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

draw.tsx 5.1KB

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