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.

polyline.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { v4 as uuid } from 'uuid'
  2. import * as vec from 'utils/vec'
  3. import { PolylineShape, ShapeType } from 'types'
  4. import { registerShapeUtils } from './index'
  5. import { intersectPolylineBounds } from 'utils/intersections'
  6. import { boundsContainPolygon } from 'utils/bounds'
  7. import { getBoundsFromPoints, translateBounds } from 'utils/utils'
  8. import { defaultStyle } from 'lib/shape-styles'
  9. const polyline = registerShapeUtils<PolylineShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. seed: Math.random(),
  15. type: ShapeType.Polyline,
  16. isGenerated: false,
  17. name: 'Polyline',
  18. parentId: 'page1',
  19. childIndex: 0,
  20. point: [0, 0],
  21. points: [[0, 0]],
  22. rotation: 0,
  23. isAspectRatioLocked: false,
  24. isLocked: false,
  25. isHidden: false,
  26. style: defaultStyle,
  27. ...props,
  28. }
  29. },
  30. render({ id, points }) {
  31. return <polyline id={id} points={points.toString()} />
  32. },
  33. getBounds(shape) {
  34. if (!this.boundsCache.has(shape)) {
  35. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  36. }
  37. return translateBounds(this.boundsCache.get(shape), shape.point)
  38. },
  39. getRotatedBounds(shape) {
  40. return this.getBounds(shape)
  41. },
  42. getCenter(shape) {
  43. const bounds = this.getBounds(shape)
  44. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  45. },
  46. hitTest(shape, point) {
  47. let pt = vec.sub(point, shape.point)
  48. let prev = shape.points[0]
  49. for (let i = 1; i < shape.points.length; i++) {
  50. let curr = shape.points[i]
  51. if (vec.distanceToLineSegment(prev, curr, pt) < 4) {
  52. return true
  53. }
  54. prev = curr
  55. }
  56. return false
  57. },
  58. hitTestBounds(this, shape, brushBounds) {
  59. const b = this.getBounds(shape)
  60. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  61. const rotatedCorners = [
  62. [b.minX, b.minY],
  63. [b.maxX, b.minY],
  64. [b.maxX, b.maxY],
  65. [b.minX, b.maxY],
  66. ].map((point) => vec.rotWith(point, center, shape.rotation))
  67. return (
  68. boundsContainPolygon(brushBounds, rotatedCorners) ||
  69. intersectPolylineBounds(
  70. shape.points.map((point) => vec.add(point, shape.point)),
  71. brushBounds
  72. ).length > 0
  73. )
  74. },
  75. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  76. const initialShapeBounds = this.getBounds(initialShape)
  77. shape.points = shape.points.map((_, i) => {
  78. const [x, y] = initialShape.points[i]
  79. return [
  80. bounds.width *
  81. (scaleX < 0
  82. ? 1 - x / initialShapeBounds.width
  83. : x / initialShapeBounds.width),
  84. bounds.height *
  85. (scaleY < 0
  86. ? 1 - y / initialShapeBounds.height
  87. : y / initialShapeBounds.height),
  88. ]
  89. })
  90. shape.point = [bounds.minX, bounds.minY]
  91. return this
  92. },
  93. transformSingle(shape, bounds, info) {
  94. this.transform(shape, bounds, info)
  95. return this
  96. },
  97. canTransform: true,
  98. canChangeAspectRatio: true,
  99. canStyleFill: false,
  100. })
  101. export default polyline