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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 {
  6. getSvgPathFromStroke,
  7. translateBounds,
  8. rng,
  9. shuffleArr,
  10. pointsBetween,
  11. } from 'utils/utils'
  12. import { defaultStyle, getShapeStyle } from 'lib/shape-styles'
  13. import getStroke from 'perfect-freehand'
  14. const pathCache = new WeakMap<number[], string>([])
  15. const rectangle = registerShapeUtils<RectangleShape>({
  16. boundsCache: new WeakMap([]),
  17. create(props) {
  18. return {
  19. id: uuid(),
  20. seed: Math.random(),
  21. type: ShapeType.Rectangle,
  22. isGenerated: false,
  23. name: 'Rectangle',
  24. parentId: 'page1',
  25. childIndex: 0,
  26. point: [0, 0],
  27. size: [1, 1],
  28. radius: 2,
  29. rotation: 0,
  30. isAspectRatioLocked: false,
  31. isLocked: false,
  32. isHidden: false,
  33. style: defaultStyle,
  34. ...props,
  35. }
  36. },
  37. render(shape) {
  38. const { id, size, radius, style } = shape
  39. const styles = getShapeStyle(style)
  40. if (!pathCache.has(shape.size)) {
  41. renderPath(shape)
  42. }
  43. const path = pathCache.get(shape.size)
  44. return (
  45. <g id={id}>
  46. <rect
  47. rx={radius}
  48. ry={radius}
  49. x={+styles.strokeWidth / 2}
  50. y={+styles.strokeWidth / 2}
  51. width={Math.max(0, size[0] + -styles.strokeWidth)}
  52. height={Math.max(0, size[1] + -styles.strokeWidth)}
  53. strokeWidth={0}
  54. />
  55. <path d={path} fill={styles.stroke} />
  56. </g>
  57. )
  58. },
  59. getBounds(shape) {
  60. if (!this.boundsCache.has(shape)) {
  61. const [width, height] = shape.size
  62. const bounds = {
  63. minX: 0,
  64. maxX: width,
  65. minY: 0,
  66. maxY: height,
  67. width,
  68. height,
  69. }
  70. this.boundsCache.set(shape, bounds)
  71. }
  72. return translateBounds(this.boundsCache.get(shape), shape.point)
  73. },
  74. hitTest() {
  75. return true
  76. },
  77. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  78. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  79. shape.size = [bounds.width, bounds.height]
  80. shape.point = [bounds.minX, bounds.minY]
  81. } else {
  82. shape.size = vec.mul(
  83. initialShape.size,
  84. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  85. )
  86. shape.point = [
  87. bounds.minX +
  88. (bounds.width - shape.size[0]) *
  89. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  90. bounds.minY +
  91. (bounds.height - shape.size[1]) *
  92. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  93. ]
  94. shape.rotation =
  95. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  96. ? -initialShape.rotation
  97. : initialShape.rotation
  98. }
  99. return this
  100. },
  101. transformSingle(shape, bounds) {
  102. shape.size = [bounds.width, bounds.height]
  103. shape.point = [bounds.minX, bounds.minY]
  104. return this
  105. },
  106. })
  107. export default rectangle
  108. function renderPath(shape: RectangleShape) {
  109. const styles = getShapeStyle(shape.style)
  110. const getRandom = rng(shape.id)
  111. const baseOffset = +styles.strokeWidth / 2
  112. const offsets = Array.from(Array(4)).map((_, i) => [
  113. getRandom() * baseOffset,
  114. getRandom() * baseOffset,
  115. ])
  116. const [w, h] = shape.size
  117. const tl = vec.add([0, 0], offsets[0])
  118. const tr = vec.add([w, 0], offsets[1])
  119. const br = vec.add([w, h], offsets[2])
  120. const bl = vec.add([0, h], offsets[3])
  121. const lines = shuffleArr(
  122. [
  123. pointsBetween(tr, br),
  124. pointsBetween(br, bl),
  125. pointsBetween(bl, tl),
  126. pointsBetween(tl, tr),
  127. ],
  128. Math.floor(5 + getRandom() * 4)
  129. )
  130. const stroke = getStroke(
  131. [...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
  132. {
  133. size: 1 + +styles.strokeWidth,
  134. thinning: 0.6,
  135. easing: (t) => t * t * t * t,
  136. end: { taper: +styles.strokeWidth * 20 },
  137. start: { taper: +styles.strokeWidth * 20 },
  138. simulatePressure: false,
  139. }
  140. )
  141. pathCache.set(shape.size, getSvgPathFromStroke(stroke))
  142. }