您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

draw.tsx 5.1KB

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