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.4KB

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