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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 { DotCircle, 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. type: ShapeType.Line,
  17. isGenerated: false,
  18. name: 'Line',
  19. parentId: 'page0',
  20. childIndex: 0,
  21. point: [0, 0],
  22. direction: [0, 0],
  23. rotation: 0,
  24. isAspectRatioLocked: false,
  25. isLocked: false,
  26. isHidden: false,
  27. ...props,
  28. style: {
  29. ...defaultStyle,
  30. ...props.style,
  31. isFilled: false,
  32. },
  33. }
  34. },
  35. render({ id, direction }) {
  36. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 10000))
  37. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 10000))
  38. return (
  39. <g id={id}>
  40. <ThinLine x1={x1} y1={y1} x2={x2} y2={y2} />
  41. <DotCircle cx={0} cy={0} r={3} />
  42. </g>
  43. )
  44. },
  45. applyStyles(shape, style) {
  46. Object.assign(shape.style, style)
  47. return this
  48. },
  49. getBounds(shape) {
  50. if (!this.boundsCache.has(shape)) {
  51. const bounds = {
  52. minX: 0,
  53. maxX: 1,
  54. minY: 0,
  55. maxY: 1,
  56. width: 1,
  57. height: 1,
  58. }
  59. this.boundsCache.set(shape, bounds)
  60. }
  61. return translateBounds(this.boundsCache.get(shape), shape.point)
  62. },
  63. getRotatedBounds(shape) {
  64. return this.getBounds(shape)
  65. },
  66. getCenter(shape) {
  67. return shape.point
  68. },
  69. hitTest(shape, test) {
  70. return true
  71. },
  72. hitTestBounds(this, shape, brushBounds) {
  73. const shapeBounds = this.getBounds(shape)
  74. return (
  75. boundsContained(shapeBounds, brushBounds) ||
  76. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  77. )
  78. },
  79. rotateTo(shape) {
  80. return this
  81. },
  82. translateTo(shape, point) {
  83. shape.point = vec.toPrecision(point)
  84. return this
  85. },
  86. transform(shape, bounds) {
  87. shape.point = [bounds.minX, bounds.minY]
  88. return this
  89. },
  90. transformSingle(shape, bounds, info) {
  91. return this.transform(shape, bounds, info)
  92. },
  93. setProperty(shape, prop, value) {
  94. shape[prop] = value
  95. return this
  96. },
  97. canTransform: false,
  98. canChangeAspectRatio: false,
  99. canStyleFill: false,
  100. })
  101. export default line