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.

line.tsx 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { uniqueId } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { LineShape, ShapeType } from 'types'
  4. import { intersectCircleBounds } from 'utils/intersections'
  5. import { ThinLine } from 'components/canvas/misc'
  6. import { translateBounds, boundsContained } from 'utils'
  7. import { defaultStyle } from 'state/shape-styles'
  8. import { registerShapeUtils } from './register'
  9. const line = registerShapeUtils<LineShape>({
  10. boundsCache: new WeakMap([]),
  11. defaultProps: {
  12. id: uniqueId(),
  13. type: ShapeType.Line,
  14. isGenerated: false,
  15. name: 'Line',
  16. parentId: 'page1',
  17. childIndex: 0,
  18. point: [0, 0],
  19. direction: [0, 0],
  20. rotation: 0,
  21. isAspectRatioLocked: false,
  22. isLocked: false,
  23. isHidden: false,
  24. style: defaultStyle,
  25. },
  26. shouldRender(shape, prev) {
  27. return shape.direction !== prev.direction || shape.style !== prev.style
  28. },
  29. render({ id, direction }) {
  30. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 10000))
  31. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 10000))
  32. return (
  33. <g id={id}>
  34. <ThinLine x1={x1} y1={y1} x2={x2} y2={y2} />
  35. <use href="dot" />
  36. </g>
  37. )
  38. },
  39. getBounds(shape) {
  40. if (!this.boundsCache.has(shape)) {
  41. const bounds = {
  42. minX: 0,
  43. maxX: 1,
  44. minY: 0,
  45. maxY: 1,
  46. width: 1,
  47. height: 1,
  48. }
  49. this.boundsCache.set(shape, bounds)
  50. }
  51. return translateBounds(this.boundsCache.get(shape), shape.point)
  52. },
  53. getRotatedBounds(shape) {
  54. return this.getBounds(shape)
  55. },
  56. getCenter(shape) {
  57. return shape.point
  58. },
  59. hitTest() {
  60. return true
  61. },
  62. hitTestBounds(this, shape, brushBounds) {
  63. const shapeBounds = this.getBounds(shape)
  64. return (
  65. boundsContained(shapeBounds, brushBounds) ||
  66. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  67. )
  68. },
  69. transform(shape, bounds) {
  70. shape.point = [bounds.minX, bounds.minY]
  71. return this
  72. },
  73. transformSingle(shape, bounds, info) {
  74. return this.transform(shape, bounds, info)
  75. },
  76. canTransform: false,
  77. canChangeAspectRatio: false,
  78. canStyleFill: false,
  79. })
  80. export default line