You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rectangle.tsx 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import { v4 as uuid } from "uuid"
  2. import * as vec from "utils/vec"
  3. import {
  4. RectangleShape,
  5. ShapeType,
  6. TransformCorner,
  7. TransformEdge,
  8. } from "types"
  9. import { registerShapeUtils } from "./index"
  10. import { boundsCollidePolygon, boundsContainPolygon } from "utils/bounds"
  11. import {
  12. getBoundsFromPoints,
  13. getRotatedCorners,
  14. rotateBounds,
  15. translateBounds,
  16. } from "utils/utils"
  17. const rectangle = registerShapeUtils<RectangleShape>({
  18. boundsCache: new WeakMap([]),
  19. create(props) {
  20. return {
  21. id: uuid(),
  22. type: ShapeType.Rectangle,
  23. isGenerated: false,
  24. name: "Rectangle",
  25. parentId: "page0",
  26. childIndex: 0,
  27. point: [0, 0],
  28. size: [1, 1],
  29. rotation: 0,
  30. style: {
  31. fill: "#c6cacb",
  32. stroke: "#000",
  33. },
  34. ...props,
  35. }
  36. },
  37. render({ id, size }) {
  38. return <rect id={id} width={size[0]} height={size[1]} />
  39. },
  40. getBounds(shape) {
  41. if (!this.boundsCache.has(shape)) {
  42. const [width, height] = shape.size
  43. const bounds = {
  44. minX: 0,
  45. maxX: width,
  46. minY: 0,
  47. maxY: height,
  48. width,
  49. height,
  50. }
  51. this.boundsCache.set(shape, bounds)
  52. }
  53. return translateBounds(this.boundsCache.get(shape), shape.point)
  54. },
  55. getRotatedBounds(shape) {
  56. return getBoundsFromPoints(
  57. getRotatedCorners(this.getBounds(shape), shape.rotation)
  58. )
  59. },
  60. getCenter(shape) {
  61. const bounds = this.getRotatedBounds(shape)
  62. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  63. },
  64. hitTest(shape) {
  65. return true
  66. },
  67. hitTestBounds(shape, brushBounds) {
  68. const rotatedCorners = getRotatedCorners(
  69. this.getBounds(shape),
  70. shape.rotation
  71. )
  72. return (
  73. boundsContainPolygon(brushBounds, rotatedCorners) ||
  74. boundsCollidePolygon(brushBounds, rotatedCorners)
  75. )
  76. },
  77. rotate(shape) {
  78. return shape
  79. },
  80. translate(shape, delta) {
  81. shape.point = vec.add(shape.point, delta)
  82. return shape
  83. },
  84. scale(shape, scale) {
  85. return shape
  86. },
  87. transform(shape, bounds, { initialShape, scaleX, scaleY }) {
  88. if (shape.rotation === 0) {
  89. shape.size = [bounds.width, bounds.height]
  90. shape.point = [bounds.minX, bounds.minY]
  91. } else {
  92. // Center shape in resized bounds
  93. shape.size = vec.mul(
  94. initialShape.size,
  95. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  96. )
  97. shape.point = vec.sub(
  98. vec.med([bounds.minX, bounds.minY], [bounds.maxX, bounds.maxY]),
  99. vec.div(shape.size, 2)
  100. )
  101. }
  102. // Set rotation for flipped shapes
  103. shape.rotation = initialShape.rotation
  104. if (scaleX < 0) shape.rotation *= -1
  105. if (scaleY < 0) shape.rotation *= -1
  106. return shape
  107. },
  108. transformSingle(shape, bounds) {
  109. shape.size = [bounds.width, bounds.height]
  110. shape.point = [bounds.minX, bounds.minY]
  111. return shape
  112. },
  113. canTransform: true,
  114. })
  115. export default rectangle