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

polyline.tsx 3.3KB

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