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.

rectangle.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { uniqueId } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { DashStyle, RectangleShape, ShapeType } from 'types'
  4. import {
  5. getSvgPathFromStroke,
  6. translateBounds,
  7. rng,
  8. shuffleArr,
  9. pointsBetween,
  10. } from 'utils/utils'
  11. import {
  12. defaultStyle,
  13. getShapeStyle,
  14. getStrokeDashArray,
  15. } from 'state/shape-styles'
  16. import getStroke from 'perfect-freehand'
  17. import { registerShapeUtils } from './register'
  18. const pathCache = new WeakMap<number[], string>([])
  19. const rectangle = registerShapeUtils<RectangleShape>({
  20. boundsCache: new WeakMap([]),
  21. create(props) {
  22. return {
  23. id: uniqueId(),
  24. seed: Math.random(),
  25. type: ShapeType.Rectangle,
  26. isGenerated: false,
  27. name: 'Rectangle',
  28. parentId: 'page1',
  29. childIndex: 0,
  30. point: [0, 0],
  31. size: [1, 1],
  32. radius: 2,
  33. rotation: 0,
  34. isAspectRatioLocked: false,
  35. isLocked: false,
  36. isHidden: false,
  37. style: defaultStyle,
  38. ...props,
  39. }
  40. },
  41. render(shape) {
  42. const { id, size, radius, style } = shape
  43. const styles = getShapeStyle(style)
  44. if (style.dash === DashStyle.Solid) {
  45. if (!pathCache.has(shape.size)) {
  46. renderPath(shape)
  47. }
  48. const path = pathCache.get(shape.size)
  49. return (
  50. <g id={id}>
  51. <rect
  52. rx={radius}
  53. ry={radius}
  54. x={+styles.strokeWidth / 2}
  55. y={+styles.strokeWidth / 2}
  56. width={Math.max(0, size[0] + -styles.strokeWidth)}
  57. height={Math.max(0, size[1] + -styles.strokeWidth)}
  58. strokeWidth={0}
  59. fill={styles.fill}
  60. />
  61. <path d={path} fill={styles.stroke} />
  62. </g>
  63. )
  64. }
  65. return (
  66. <rect
  67. id={id}
  68. width={size[0]}
  69. height={size[1]}
  70. fill={styles.fill}
  71. stroke={styles.stroke}
  72. strokeDasharray={getStrokeDashArray(
  73. style.dash,
  74. +styles.strokeWidth
  75. ).join(' ')}
  76. />
  77. )
  78. },
  79. getBounds(shape) {
  80. if (!this.boundsCache.has(shape)) {
  81. const [width, height] = shape.size
  82. const bounds = {
  83. minX: 0,
  84. maxX: width,
  85. minY: 0,
  86. maxY: height,
  87. width,
  88. height,
  89. }
  90. this.boundsCache.set(shape, bounds)
  91. }
  92. return translateBounds(this.boundsCache.get(shape), shape.point)
  93. },
  94. hitTest() {
  95. return true
  96. },
  97. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  98. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  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. })
  127. export default rectangle
  128. function renderPath(shape: RectangleShape) {
  129. const styles = getShapeStyle(shape.style)
  130. const getRandom = rng(shape.id)
  131. const baseOffset = +styles.strokeWidth / 2
  132. const offsets = Array.from(Array(4)).map(() => [
  133. getRandom() * baseOffset,
  134. getRandom() * baseOffset,
  135. ])
  136. const [w, h] = shape.size
  137. const tl = vec.add([0, 0], offsets[0])
  138. const tr = vec.add([w, 0], offsets[1])
  139. const br = vec.add([w, h], offsets[2])
  140. const bl = vec.add([0, h], offsets[3])
  141. const lines = shuffleArr(
  142. [
  143. pointsBetween(tr, br),
  144. pointsBetween(br, bl),
  145. pointsBetween(bl, tl),
  146. pointsBetween(tl, tr),
  147. ],
  148. Math.floor(5 + getRandom() * 4)
  149. )
  150. const stroke = getStroke(
  151. [...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
  152. {
  153. size: 1 + +styles.strokeWidth,
  154. thinning: 0.6,
  155. easing: (t) => t * t * t * t,
  156. end: { taper: +styles.strokeWidth * 20 },
  157. start: { taper: +styles.strokeWidth * 20 },
  158. simulatePressure: false,
  159. }
  160. )
  161. pathCache.set(shape.size, getSvgPathFromStroke(stroke))
  162. }