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.

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