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

rectangle.tsx 1.7KB

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