Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. rotateTo(shape, rotation) {
  87. shape.rotation = rotation
  88. return this
  89. },
  90. translateTo(shape, point) {
  91. shape.point = point
  92. return this
  93. },
  94. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  95. if (shape.rotation === 0) {
  96. shape.size = [bounds.width, bounds.height]
  97. shape.point = [bounds.minX, bounds.minY]
  98. } else {
  99. shape.size = vec.mul(
  100. initialShape.size,
  101. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  102. )
  103. shape.point = [
  104. bounds.minX +
  105. (bounds.width - shape.size[0]) *
  106. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  107. bounds.minY +
  108. (bounds.height - shape.size[1]) *
  109. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  110. ]
  111. shape.rotation =
  112. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  113. ? -initialShape.rotation
  114. : initialShape.rotation
  115. }
  116. return this
  117. },
  118. transformSingle(shape, bounds) {
  119. shape.size = [bounds.width, bounds.height]
  120. shape.point = [bounds.minX, bounds.minY]
  121. return this
  122. },
  123. setParent(shape, parentId) {
  124. shape.parentId = parentId
  125. return this
  126. },
  127. setChildIndex(shape, childIndex) {
  128. shape.childIndex = childIndex
  129. return this
  130. },
  131. canTransform: true,
  132. canChangeAspectRatio: true,
  133. })
  134. export default rectangle