Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

rectangle.tsx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { uniqueId } from 'utils/utils'
  2. import 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: uniqueId(),
  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. className="hi"
  48. rx={radius}
  49. ry={radius}
  50. x={+styles.strokeWidth / 2}
  51. y={+styles.strokeWidth / 2}
  52. width={Math.max(0, size[0] + -styles.strokeWidth)}
  53. height={Math.max(0, size[1] + -styles.strokeWidth)}
  54. strokeWidth={0}
  55. fill={styles.fill}
  56. />
  57. <path d={path} fill={styles.stroke} />
  58. </g>
  59. )
  60. },
  61. getBounds(shape) {
  62. if (!this.boundsCache.has(shape)) {
  63. const [width, height] = shape.size
  64. const bounds = {
  65. minX: 0,
  66. maxX: width,
  67. minY: 0,
  68. maxY: height,
  69. width,
  70. height,
  71. }
  72. this.boundsCache.set(shape, bounds)
  73. }
  74. return translateBounds(this.boundsCache.get(shape), shape.point)
  75. },
  76. hitTest() {
  77. return true
  78. },
  79. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  80. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  81. shape.size = [bounds.width, bounds.height]
  82. shape.point = [bounds.minX, bounds.minY]
  83. } else {
  84. shape.size = vec.mul(
  85. initialShape.size,
  86. Math.min(Math.abs(scaleX), Math.abs(scaleY))
  87. )
  88. shape.point = [
  89. bounds.minX +
  90. (bounds.width - shape.size[0]) *
  91. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  92. bounds.minY +
  93. (bounds.height - shape.size[1]) *
  94. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  95. ]
  96. shape.rotation =
  97. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  98. ? -initialShape.rotation
  99. : initialShape.rotation
  100. }
  101. return this
  102. },
  103. transformSingle(shape, bounds) {
  104. shape.size = [bounds.width, bounds.height]
  105. shape.point = [bounds.minX, bounds.minY]
  106. return this
  107. },
  108. })
  109. export default rectangle
  110. function renderPath(shape: RectangleShape) {
  111. const styles = getShapeStyle(shape.style)
  112. const getRandom = rng(shape.id)
  113. const baseOffset = +styles.strokeWidth / 2
  114. const offsets = Array.from(Array(4)).map((_, i) => [
  115. getRandom() * baseOffset,
  116. getRandom() * baseOffset,
  117. ])
  118. const [w, h] = shape.size
  119. const tl = vec.add([0, 0], offsets[0])
  120. const tr = vec.add([w, 0], offsets[1])
  121. const br = vec.add([w, h], offsets[2])
  122. const bl = vec.add([0, h], offsets[3])
  123. const lines = shuffleArr(
  124. [
  125. pointsBetween(tr, br),
  126. pointsBetween(br, bl),
  127. pointsBetween(bl, tl),
  128. pointsBetween(tl, tr),
  129. ],
  130. Math.floor(5 + getRandom() * 4)
  131. )
  132. const stroke = getStroke(
  133. [...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
  134. {
  135. size: 1 + +styles.strokeWidth,
  136. thinning: 0.6,
  137. easing: (t) => t * t * t * t,
  138. end: { taper: +styles.strokeWidth * 20 },
  139. start: { taper: +styles.strokeWidth * 20 },
  140. simulatePressure: false,
  141. }
  142. )
  143. pathCache.set(shape.size, getSvgPathFromStroke(stroke))
  144. }