Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

line.tsx 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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. <use href="dot" />
  42. </g>
  43. )
  44. },
  45. getBounds(shape) {
  46. if (!this.boundsCache.has(shape)) {
  47. const bounds = {
  48. minX: 0,
  49. maxX: 1,
  50. minY: 0,
  51. maxY: 1,
  52. width: 1,
  53. height: 1,
  54. }
  55. this.boundsCache.set(shape, bounds)
  56. }
  57. return translateBounds(this.boundsCache.get(shape), shape.point)
  58. },
  59. getRotatedBounds(shape) {
  60. return this.getBounds(shape)
  61. },
  62. getCenter(shape) {
  63. return shape.point
  64. },
  65. hitTest(shape, test) {
  66. return true
  67. },
  68. hitTestBounds(this, shape, brushBounds) {
  69. const shapeBounds = this.getBounds(shape)
  70. return (
  71. boundsContained(shapeBounds, brushBounds) ||
  72. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  73. )
  74. },
  75. transform(shape, bounds) {
  76. shape.point = [bounds.minX, bounds.minY]
  77. return this
  78. },
  79. transformSingle(shape, bounds, info) {
  80. return this.transform(shape, bounds, info)
  81. },
  82. canTransform: false,
  83. canChangeAspectRatio: false,
  84. canStyleFill: false,
  85. })
  86. export default line