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.2KB

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