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 3.3KB

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