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

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