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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, style }) {
  33. return (
  34. <g id={id}>
  35. <rect
  36. id={id}
  37. rx={radius}
  38. ry={radius}
  39. width={Math.max(0, size[0] - Number(style.strokeWidth) / 2)}
  40. height={Math.max(0, size[1] - Number(style.strokeWidth) / 2)}
  41. />
  42. </g>
  43. )
  44. },
  45. applyStyles(shape, style) {
  46. Object.assign(shape.style, style)
  47. return this
  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 = vec.toPrecision(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