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

polyline.tsx 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(shape, bounds, { initialShape, scaleX, scaleY }) {
  86. const initialShapeBounds = this.getBounds(initialShape)
  87. shape.points = shape.points.map((_, i) => {
  88. const [x, y] = initialShape.points[i]
  89. return [
  90. bounds.width *
  91. (scaleX < 0
  92. ? 1 - x / initialShapeBounds.width
  93. : x / initialShapeBounds.width),
  94. bounds.height *
  95. (scaleY < 0
  96. ? 1 - y / initialShapeBounds.height
  97. : y / initialShapeBounds.height),
  98. ]
  99. })
  100. shape.point = [bounds.minX, bounds.minY]
  101. return shape
  102. },
  103. transformSingle(shape, bounds, info) {
  104. return this.transform(shape, bounds, info)
  105. },
  106. canTransform: true,
  107. canChangeAspectRatio: true,
  108. })
  109. export default polyline