Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

rectangle.tsx 3.5KB

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