Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

draw.tsx 6.1KB

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