Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

polyline.tsx 3.0KB

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