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

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