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

polyline.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. import { defaultStyle } from 'lib/shape-styles'
  9. const polyline = registerShapeUtils<PolylineShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. type: ShapeType.Polyline,
  15. isGenerated: false,
  16. name: 'Polyline',
  17. parentId: 'page0',
  18. childIndex: 0,
  19. point: [0, 0],
  20. points: [[0, 0]],
  21. rotation: 0,
  22. isAspectRatioLocked: false,
  23. isLocked: false,
  24. isHidden: false,
  25. style: defaultStyle,
  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. this.boundsCache.set(shape, getBoundsFromPoints(shape.points))
  35. }
  36. return translateBounds(this.boundsCache.get(shape), shape.point)
  37. },
  38. getRotatedBounds(shape) {
  39. return this.getBounds(shape)
  40. },
  41. getCenter(shape) {
  42. const bounds = this.getBounds(shape)
  43. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  44. },
  45. hitTest(shape, point) {
  46. let pt = vec.sub(point, shape.point)
  47. let prev = shape.points[0]
  48. for (let i = 1; i < shape.points.length; i++) {
  49. let curr = shape.points[i]
  50. if (vec.distanceToLineSegment(prev, curr, pt) < 4) {
  51. return true
  52. }
  53. prev = curr
  54. }
  55. return false
  56. },
  57. hitTestBounds(this, shape, brushBounds) {
  58. const b = this.getBounds(shape)
  59. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  60. const rotatedCorners = [
  61. [b.minX, b.minY],
  62. [b.maxX, b.minY],
  63. [b.maxX, b.maxY],
  64. [b.minX, b.maxY],
  65. ].map((point) => vec.rotWith(point, center, shape.rotation))
  66. return (
  67. boundsContainPolygon(brushBounds, rotatedCorners) ||
  68. intersectPolylineBounds(
  69. shape.points.map((point) => vec.add(point, shape.point)),
  70. brushBounds
  71. ).length > 0
  72. )
  73. },
  74. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  75. const initialShapeBounds = this.getBounds(initialShape)
  76. shape.points = shape.points.map((_, i) => {
  77. const [x, y] = initialShape.points[i]
  78. return [
  79. bounds.width *
  80. (scaleX < 0
  81. ? 1 - x / initialShapeBounds.width
  82. : x / initialShapeBounds.width),
  83. bounds.height *
  84. (scaleY < 0
  85. ? 1 - y / initialShapeBounds.height
  86. : y / initialShapeBounds.height),
  87. ]
  88. })
  89. shape.point = [bounds.minX, bounds.minY]
  90. return this
  91. },
  92. transformSingle(shape, bounds, info) {
  93. this.transform(shape, bounds, info)
  94. return this
  95. },
  96. canTransform: true,
  97. canChangeAspectRatio: true,
  98. canStyleFill: false,
  99. })
  100. export default polyline