Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rectangle.tsx 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. radius: 2,
  24. rotation: 0,
  25. style: {
  26. fill: "#c6cacb",
  27. stroke: "#000",
  28. },
  29. ...props,
  30. }
  31. },
  32. render({ id, size, radius, childIndex }) {
  33. return (
  34. <g id={id}>
  35. <rect
  36. id={id}
  37. width={size[0]}
  38. height={size[1]}
  39. rx={radius}
  40. ry={radius}
  41. />
  42. <text
  43. y={4}
  44. x={4}
  45. fontSize={18}
  46. fill="black"
  47. stroke="none"
  48. alignmentBaseline="text-before-edge"
  49. pointerEvents="none"
  50. >
  51. {childIndex}
  52. </text>
  53. </g>
  54. )
  55. },
  56. applyStyles(shape, style) {
  57. Object.assign(shape.style, style)
  58. return this
  59. },
  60. getBounds(shape) {
  61. if (!this.boundsCache.has(shape)) {
  62. const [width, height] = shape.size
  63. const bounds = {
  64. minX: 0,
  65. maxX: width,
  66. minY: 0,
  67. maxY: height,
  68. width,
  69. height,
  70. }
  71. this.boundsCache.set(shape, bounds)
  72. }
  73. return translateBounds(this.boundsCache.get(shape), shape.point)
  74. },
  75. getRotatedBounds(shape) {
  76. return getBoundsFromPoints(
  77. getRotatedCorners(this.getBounds(shape), shape.rotation)
  78. )
  79. },
  80. getCenter(shape) {
  81. const bounds = this.getRotatedBounds(shape)
  82. return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
  83. },
  84. hitTest(shape) {
  85. return true
  86. },
  87. hitTestBounds(shape, brushBounds) {
  88. const rotatedCorners = getRotatedCorners(
  89. this.getBounds(shape),
  90. shape.rotation
  91. )
  92. return (
  93. boundsContainPolygon(brushBounds, rotatedCorners) ||
  94. boundsCollidePolygon(brushBounds, rotatedCorners)
  95. )
  96. },
  97. rotateTo(shape, rotation) {
  98. shape.rotation = rotation
  99. return this
  100. },
  101. translateTo(shape, point) {
  102. shape.point = point
  103. return this
  104. },
  105. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  106. if (shape.rotation === 0) {
  107. shape.size = [bounds.width, bounds.height]
  108. shape.point = [bounds.minX, bounds.minY]
  109. } else {
  110. shape.size = vec.mul(
  111. initialShape.size,
  112. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  113. )
  114. shape.point = [
  115. bounds.minX +
  116. (bounds.width - shape.size[0]) *
  117. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  118. bounds.minY +
  119. (bounds.height - shape.size[1]) *
  120. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  121. ]
  122. shape.rotation =
  123. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  124. ? -initialShape.rotation
  125. : initialShape.rotation
  126. }
  127. return this
  128. },
  129. transformSingle(shape, bounds) {
  130. shape.size = [bounds.width, bounds.height]
  131. shape.point = [bounds.minX, bounds.minY]
  132. return this
  133. },
  134. setParent(shape, parentId) {
  135. shape.parentId = parentId
  136. return this
  137. },
  138. setChildIndex(shape, childIndex) {
  139. shape.childIndex = childIndex
  140. return this
  141. },
  142. canTransform: true,
  143. canChangeAspectRatio: true,
  144. })
  145. export default rectangle