You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

polyline.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { getFromCache, uniqueId } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { PolylineShape, ShapeType } from 'types'
  4. import { intersectPolylineBounds } from 'utils/intersections'
  5. import {
  6. boundsContainPolygon,
  7. getBoundsFromPoints,
  8. translateBounds,
  9. } from 'utils'
  10. import { defaultStyle, getShapeStyle } from 'state/shape-styles'
  11. import { registerShapeUtils } from './register'
  12. const polyline = registerShapeUtils<PolylineShape>({
  13. boundsCache: new WeakMap([]),
  14. defaultProps: {
  15. id: uniqueId(),
  16. type: ShapeType.Polyline,
  17. name: 'Polyline',
  18. parentId: 'page1',
  19. childIndex: 0,
  20. point: [0, 0],
  21. points: [[0, 0]],
  22. rotation: 0,
  23. style: defaultStyle,
  24. },
  25. shouldRender(shape, prev) {
  26. return shape.points !== prev.points || shape.style !== prev.style
  27. },
  28. render(shape, { isDarkMode }) {
  29. const { points, style } = shape
  30. const styles = getShapeStyle(style, isDarkMode)
  31. return (
  32. <polyline
  33. points={points.toString()}
  34. stroke={styles.stroke}
  35. strokeWidth={styles.strokeWidth * 1.618}
  36. fill={shape.style.isFilled ? styles.fill : 'none'}
  37. pointerEvents={style.isFilled ? 'all' : 'stroke'}
  38. strokeLinecap="round"
  39. strokeLinejoin="round"
  40. />
  41. )
  42. },
  43. getBounds(shape) {
  44. const bounds = getFromCache(this.boundsCache, shape, (cache) => {
  45. cache.set(shape, getBoundsFromPoints(shape.points))
  46. })
  47. return translateBounds(bounds, shape.point)
  48. },
  49. getRotatedBounds(shape) {
  50. return this.getBounds(shape)
  51. },
  52. getCenter(shape) {
  53. const bounds = this.getBounds(shape)
  54. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  55. },
  56. hitTest() {
  57. return true
  58. },
  59. hitTestBounds(this, shape, brushBounds) {
  60. const b = this.getBounds(shape)
  61. const center = [b.minX + b.width / 2, b.minY + b.height / 2]
  62. const rotatedCorners = [
  63. [b.minX, b.minY],
  64. [b.maxX, b.minY],
  65. [b.maxX, b.maxY],
  66. [b.minX, b.maxY],
  67. ].map((point) => vec.rotWith(point, center, shape.rotation))
  68. return (
  69. boundsContainPolygon(brushBounds, rotatedCorners) ||
  70. intersectPolylineBounds(
  71. shape.points.map((point) => vec.add(point, shape.point)),
  72. brushBounds
  73. ).length > 0
  74. )
  75. },
  76. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  77. const initialShapeBounds = this.getBounds(initialShape)
  78. shape.points = shape.points.map((_, i) => {
  79. const [x, y] = initialShape.points[i]
  80. return [
  81. bounds.width *
  82. (scaleX < 0
  83. ? 1 - x / initialShapeBounds.width
  84. : x / initialShapeBounds.width),
  85. bounds.height *
  86. (scaleY < 0
  87. ? 1 - y / initialShapeBounds.height
  88. : y / initialShapeBounds.height),
  89. ]
  90. })
  91. shape.point = [bounds.minX, bounds.minY]
  92. return this
  93. },
  94. transformSingle(shape, bounds, info) {
  95. this.transform(shape, bounds, info)
  96. return this
  97. },
  98. canTransform: true,
  99. canChangeAspectRatio: true,
  100. canStyleFill: true,
  101. })
  102. export default polyline