Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import { LineShape, ShapeType } from "types"
  4. import { createShape } from "./index"
  5. import { boundsContained } from "utils/bounds"
  6. import { intersectCircleBounds } from "utils/intersections"
  7. const line = createShape<LineShape>({
  8. boundsCache: new WeakMap([]),
  9. create(props) {
  10. return {
  11. id: uuid(),
  12. type: ShapeType.Line,
  13. isGenerated: false,
  14. name: "Line",
  15. parentId: "page0",
  16. childIndex: 0,
  17. point: [0, 0],
  18. direction: [0, 0],
  19. rotation: 0,
  20. style: {
  21. fill: "#777",
  22. stroke: "#000",
  23. },
  24. ...props,
  25. }
  26. },
  27. render({ id, direction }) {
  28. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 100000))
  29. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 100000))
  30. return (
  31. <g id={id}>
  32. <line x1={x1} y1={y1} x2={x2} y2={y2} />
  33. <circle cx={0} cy={0} r={4} />
  34. </g>
  35. )
  36. },
  37. getBounds(shape) {
  38. if (this.boundsCache.has(shape)) {
  39. return this.boundsCache.get(shape)
  40. }
  41. const {
  42. point: [x, y],
  43. } = shape
  44. const bounds = {
  45. minX: x,
  46. maxX: x + 1,
  47. minY: y,
  48. maxY: y + 1,
  49. width: 1,
  50. height: 1,
  51. }
  52. this.boundsCache.set(shape, bounds)
  53. return bounds
  54. },
  55. hitTest(shape, test) {
  56. return true
  57. },
  58. hitTestBounds(this, shape, brushBounds) {
  59. const shapeBounds = this.getBounds(shape)
  60. return (
  61. boundsContained(shapeBounds, brushBounds) ||
  62. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  63. )
  64. },
  65. rotate(shape) {
  66. return shape
  67. },
  68. translate(shape, delta) {
  69. shape.point = vec.add(shape.point, delta)
  70. return shape
  71. },
  72. scale(shape, scale: number) {
  73. return shape
  74. },
  75. transform(shape, bounds) {
  76. shape.point = [bounds.minX, bounds.minY]
  77. return shape
  78. },
  79. canTransform: false,
  80. })
  81. export default line