Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

polyline.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 {
  7. boundsCollide,
  8. boundsContained,
  9. boundsContainPolygon,
  10. } from "utils/bounds"
  11. import { getBoundsFromPoints, translateBounds } from "utils/utils"
  12. const polyline = registerShapeUtils<PolylineShape>({
  13. boundsCache: new WeakMap([]),
  14. create(props) {
  15. return {
  16. id: uuid(),
  17. type: ShapeType.Polyline,
  18. isGenerated: false,
  19. name: "Polyline",
  20. parentId: "page0",
  21. childIndex: 0,
  22. point: [0, 0],
  23. points: [[0, 0]],
  24. rotation: 0,
  25. style: {},
  26. ...props,
  27. }
  28. },
  29. render({ id, points }) {
  30. return <polyline id={id} points={points.toString()} />
  31. },
  32. getBounds(shape) {
  33. if (!this.boundsCache.has(shape)) {
  34. const bounds = getBoundsFromPoints(shape.points)
  35. this.boundsCache.set(shape, bounds)
  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. rotate(shape) {
  76. return shape
  77. },
  78. translate(shape, delta) {
  79. shape.point = vec.add(shape.point, delta)
  80. return shape
  81. },
  82. scale(shape, scale: number) {
  83. return shape
  84. },
  85. transform(
  86. shape,
  87. bounds,
  88. { initialShape, initialShapeBounds, isFlippedX, isFlippedY }
  89. ) {
  90. shape.points = shape.points.map((_, i) => {
  91. const [x, y] = initialShape.points[i]
  92. return [
  93. bounds.width *
  94. (isFlippedX
  95. ? 1 - x / initialShapeBounds.width
  96. : x / initialShapeBounds.width),
  97. bounds.height *
  98. (isFlippedY
  99. ? 1 - y / initialShapeBounds.height
  100. : y / initialShapeBounds.height),
  101. ]
  102. })
  103. shape.point = [bounds.minX, bounds.minY]
  104. return shape
  105. },
  106. transformSingle(shape, bounds, info) {
  107. return this.transform(shape, bounds, info)
  108. },
  109. canTransform: true,
  110. })
  111. export default polyline