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

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