Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 } from "components/canvas/misc"
  8. import { translateBounds } from "utils/utils"
  9. const line = registerShapeUtils<LineShape>({
  10. boundsCache: new WeakMap([]),
  11. create(props) {
  12. return {
  13. id: uuid(),
  14. type: ShapeType.Line,
  15. isGenerated: false,
  16. name: "Line",
  17. parentId: "page0",
  18. childIndex: 0,
  19. point: [0, 0],
  20. direction: [0, 0],
  21. rotation: 0,
  22. style: {
  23. fill: "#c6cacb",
  24. stroke: "#000",
  25. },
  26. ...props,
  27. }
  28. },
  29. render({ id, direction }) {
  30. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 100000))
  31. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 100000))
  32. return (
  33. <g id={id}>
  34. <line x1={x1} y1={y1} x2={x2} y2={y2} />
  35. <DotCircle cx={0} cy={0} r={3} />
  36. </g>
  37. )
  38. },
  39. applyStyles(shape, style) {
  40. Object.assign(shape.style, style)
  41. return this
  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(shape, test) {
  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. rotateTo(shape) {
  74. return this
  75. },
  76. translateTo(shape, point) {
  77. shape.point = point
  78. return this
  79. },
  80. transform(shape, bounds) {
  81. shape.point = [bounds.minX, bounds.minY]
  82. return this
  83. },
  84. transformSingle(shape, bounds, info) {
  85. return this.transform(shape, bounds, info)
  86. },
  87. setParent(shape, parentId) {
  88. shape.parentId = parentId
  89. return this
  90. },
  91. setChildIndex(shape, childIndex) {
  92. shape.childIndex = childIndex
  93. return this
  94. },
  95. canTransform: false,
  96. canChangeAspectRatio: false,
  97. })
  98. export default line