Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

line.tsx 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ...props,
  22. }
  23. },
  24. render({ id, direction }) {
  25. const [x1, y1] = vec.add([0, 0], vec.mul(direction, 100000))
  26. const [x2, y2] = vec.sub([0, 0], vec.mul(direction, 100000))
  27. return (
  28. <g id={id}>
  29. <line x1={x1} y1={y1} x2={x2} y2={y2} />
  30. <circle cx={0} cy={0} r={4} />
  31. </g>
  32. )
  33. },
  34. getBounds(shape) {
  35. if (this.boundsCache.has(shape)) {
  36. return this.boundsCache.get(shape)
  37. }
  38. const {
  39. point: [x, y],
  40. } = shape
  41. const bounds = {
  42. minX: x,
  43. maxX: x + 1,
  44. minY: y,
  45. maxY: y + 1,
  46. width: 1,
  47. height: 1,
  48. }
  49. this.boundsCache.set(shape, bounds)
  50. return bounds
  51. },
  52. hitTest(shape, test) {
  53. return true
  54. },
  55. hitTestBounds(this, shape, brushBounds) {
  56. const shapeBounds = this.getBounds(shape)
  57. return (
  58. boundsContained(shapeBounds, brushBounds) ||
  59. intersectCircleBounds(shape.point, 4, brushBounds).length > 0
  60. )
  61. },
  62. rotate(shape) {
  63. return shape
  64. },
  65. translate(shape, delta) {
  66. shape.point = vec.add(shape.point, delta)
  67. return shape
  68. },
  69. scale(shape, scale: number) {
  70. return shape
  71. },
  72. stretch(shape, scaleX: number, scaleY: 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