您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

polyline.tsx 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. rotateTo(shape, rotation) {
  76. shape.rotation = rotation
  77. return this
  78. },
  79. translateTo(shape, point) {
  80. shape.point = point
  81. return this
  82. },
  83. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  84. const initialShapeBounds = this.getBounds(initialShape)
  85. shape.points = shape.points.map((_, i) => {
  86. const [x, y] = initialShape.points[i]
  87. return [
  88. bounds.width *
  89. (scaleX < 0
  90. ? 1 - x / initialShapeBounds.width
  91. : x / initialShapeBounds.width),
  92. bounds.height *
  93. (scaleY < 0
  94. ? 1 - y / initialShapeBounds.height
  95. : y / initialShapeBounds.height),
  96. ]
  97. })
  98. shape.point = [bounds.minX, bounds.minY]
  99. return this
  100. },
  101. transformSingle(shape, bounds, info) {
  102. this.transform(shape, bounds, info)
  103. return this
  104. },
  105. setParent(shape, parentId) {
  106. shape.parentId = parentId
  107. return this
  108. },
  109. setChildIndex(shape, childIndex) {
  110. shape.childIndex = childIndex
  111. return this
  112. },
  113. canTransform: true,
  114. canChangeAspectRatio: true,
  115. })
  116. export default polyline