Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

rectangle.tsx 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 { translateBounds } from 'utils/utils'
  6. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  7. const rectangle = registerShapeUtils<RectangleShape>({
  8. boundsCache: new WeakMap([]),
  9. create(props) {
  10. return {
  11. id: uuid(),
  12. type: ShapeType.Rectangle,
  13. isGenerated: false,
  14. name: 'Rectangle',
  15. parentId: 'page0',
  16. childIndex: 0,
  17. point: [0, 0],
  18. size: [1, 1],
  19. radius: 2,
  20. rotation: 0,
  21. isAspectRatioLocked: false,
  22. isLocked: false,
  23. isHidden: false,
  24. style: defaultStyle,
  25. ...props,
  26. }
  27. },
  28. render({ id, size, radius, style }) {
  29. const styles = getShapeStyle(style)
  30. return (
  31. <g id={id}>
  32. <rect
  33. id={id}
  34. rx={radius}
  35. ry={radius}
  36. width={Math.max(0, size[0] - Number(styles.strokeWidth) / 2)}
  37. height={Math.max(0, size[1] - Number(styles.strokeWidth) / 2)}
  38. />
  39. </g>
  40. )
  41. },
  42. getBounds(shape) {
  43. if (!this.boundsCache.has(shape)) {
  44. const [width, height] = shape.size
  45. const bounds = {
  46. minX: 0,
  47. maxX: width,
  48. minY: 0,
  49. maxY: height,
  50. width,
  51. height,
  52. }
  53. this.boundsCache.set(shape, bounds)
  54. }
  55. return translateBounds(this.boundsCache.get(shape), shape.point)
  56. },
  57. hitTest() {
  58. return true
  59. },
  60. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  61. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  62. shape.size = [bounds.width, bounds.height]
  63. shape.point = [bounds.minX, bounds.minY]
  64. } else {
  65. shape.size = vec.mul(
  66. initialShape.size,
  67. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  68. )
  69. shape.point = [
  70. bounds.minX +
  71. (bounds.width - shape.size[0]) *
  72. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  73. bounds.minY +
  74. (bounds.height - shape.size[1]) *
  75. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  76. ]
  77. shape.rotation =
  78. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  79. ? -initialShape.rotation
  80. : initialShape.rotation
  81. }
  82. return this
  83. },
  84. transformSingle(shape, bounds) {
  85. shape.size = [bounds.width, bounds.height]
  86. shape.point = [bounds.minX, bounds.minY]
  87. return this
  88. },
  89. })
  90. export default rectangle