You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyline.tsx 3.1KB

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