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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import { uniqueId, getPerfectDashProps, getFromCache } from 'utils/utils'
  2. import vec from 'utils/vec'
  3. import { DashStyle, RectangleShape, ShapeType } from 'types'
  4. import { getSvgPathFromStroke, translateBounds, rng, shuffleArr } from 'utils'
  5. import { defaultStyle, getShapeStyle } from 'state/shape-styles'
  6. import getStroke from 'perfect-freehand'
  7. import { registerShapeUtils } from './register'
  8. const pathCache = new WeakMap<number[], string>([])
  9. const rectangle = registerShapeUtils<RectangleShape>({
  10. boundsCache: new WeakMap([]),
  11. defaultProps: {
  12. id: uniqueId(),
  13. type: ShapeType.Rectangle,
  14. name: 'Rectangle',
  15. parentId: 'page1',
  16. childIndex: 0,
  17. point: [0, 0],
  18. size: [1, 1],
  19. radius: 2,
  20. rotation: 0,
  21. style: defaultStyle,
  22. },
  23. shouldRender(shape, prev) {
  24. return shape.size !== prev.size || shape.style !== prev.style
  25. },
  26. render(shape, { isHovered, isDarkMode }) {
  27. const { id, size, radius, style } = shape
  28. const styles = getShapeStyle(style, isDarkMode)
  29. const strokeWidth = +styles.strokeWidth
  30. if (style.dash === DashStyle.Draw) {
  31. const pathData = getFromCache(pathCache, shape.size, (cache) => {
  32. cache.set(shape.size, renderPath(shape))
  33. })
  34. return (
  35. <>
  36. {style.isFilled && (
  37. <rect
  38. rx={radius}
  39. ry={radius}
  40. x={+styles.strokeWidth / 2}
  41. y={+styles.strokeWidth / 2}
  42. width={Math.max(0, size[0] - strokeWidth)}
  43. height={Math.max(0, size[1] - strokeWidth)}
  44. strokeWidth={0}
  45. fill={styles.fill}
  46. stroke={styles.stroke}
  47. />
  48. )}
  49. <path
  50. d={pathData}
  51. fill={styles.stroke}
  52. stroke={styles.stroke}
  53. strokeWidth={styles.strokeWidth}
  54. filter={isHovered ? 'url(#expand)' : 'none'}
  55. pointerEvents={style.isFilled ? 'all' : 'stroke'}
  56. />
  57. </>
  58. )
  59. }
  60. const sw = strokeWidth * 1.618
  61. const w = Math.max(0, size[0] - sw / 2)
  62. const h = Math.max(0, size[1] - sw / 2)
  63. const strokes: [number[], number[], number][] = [
  64. [[sw / 2, sw / 2], [w, sw / 2], w - sw / 2],
  65. [[w, sw / 2], [w, h], h - sw / 2],
  66. [[w, h], [sw / 2, h], w - sw / 2],
  67. [[sw / 2, h], [sw / 2, sw / 2], h - sw / 2],
  68. ]
  69. const paths = strokes.map(([start, end, length], i) => {
  70. const { strokeDasharray, strokeDashoffset } = getPerfectDashProps(
  71. length,
  72. sw,
  73. shape.style.dash
  74. )
  75. return (
  76. <line
  77. key={id + '_' + i}
  78. x1={start[0]}
  79. y1={start[1]}
  80. x2={end[0]}
  81. y2={end[1]}
  82. stroke={styles.stroke}
  83. strokeWidth={sw}
  84. strokeLinecap="round"
  85. strokeDasharray={strokeDasharray}
  86. strokeDashoffset={strokeDashoffset}
  87. />
  88. )
  89. })
  90. return (
  91. <>
  92. <rect
  93. x={sw / 2}
  94. y={sw / 2}
  95. width={w}
  96. height={h}
  97. fill={styles.fill}
  98. stroke="transparent"
  99. strokeWidth={sw}
  100. pointerEvents={style.isFilled ? 'all' : 'stroke'}
  101. />
  102. <g filter={isHovered ? 'url(#expand)' : 'none'} pointerEvents="stroke">
  103. {paths}
  104. </g>
  105. </>
  106. )
  107. },
  108. getBounds(shape) {
  109. const bounds = getFromCache(this.boundsCache, shape, (cache) => {
  110. const [width, height] = shape.size
  111. cache.set(shape, {
  112. minX: 0,
  113. maxX: width,
  114. minY: 0,
  115. maxY: height,
  116. width,
  117. height,
  118. })
  119. })
  120. return translateBounds(bounds, shape.point)
  121. },
  122. hitTest() {
  123. return true
  124. },
  125. transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
  126. if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
  127. shape.size = vec.round([bounds.width, bounds.height])
  128. shape.point = vec.round([bounds.minX, bounds.minY])
  129. } else {
  130. shape.size = vec.round(
  131. vec.mul(initialShape.size, Math.min(Math.abs(scaleX), Math.abs(scaleY)))
  132. )
  133. shape.point = vec.round([
  134. bounds.minX +
  135. (bounds.width - shape.size[0]) *
  136. (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
  137. bounds.minY +
  138. (bounds.height - shape.size[1]) *
  139. (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
  140. ])
  141. shape.rotation =
  142. (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
  143. ? -initialShape.rotation
  144. : initialShape.rotation
  145. }
  146. return this
  147. },
  148. transformSingle(shape, bounds) {
  149. shape.size = vec.round([bounds.width, bounds.height])
  150. shape.point = vec.round([bounds.minX, bounds.minY])
  151. return this
  152. },
  153. })
  154. export default rectangle
  155. function renderPath(shape: RectangleShape) {
  156. const styles = getShapeStyle(shape.style)
  157. const getRandom = rng(shape.id)
  158. const strokeWidth = +styles.strokeWidth
  159. const baseOffset = strokeWidth / 2
  160. const offsets = Array.from(Array(4)).map(() => [
  161. getRandom() * baseOffset,
  162. getRandom() * baseOffset,
  163. ])
  164. const sw = strokeWidth
  165. const w = Math.max(0, shape.size[0] - sw / 2)
  166. const h = Math.max(0, shape.size[1] - sw / 2)
  167. const tl = vec.add([sw / 2, sw / 2], offsets[0])
  168. const tr = vec.add([w, sw / 2], offsets[1])
  169. const br = vec.add([w, h], offsets[2])
  170. const bl = vec.add([sw / 2, h], offsets[3])
  171. const lines = shuffleArr(
  172. [
  173. vec.pointsBetween(tr, br),
  174. vec.pointsBetween(br, bl),
  175. vec.pointsBetween(bl, tl),
  176. vec.pointsBetween(tl, tr),
  177. ],
  178. Math.floor(5 + getRandom() * 4)
  179. )
  180. const stroke = getStroke(
  181. [...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
  182. {
  183. size: 1 + +styles.strokeWidth,
  184. thinning: 0.6,
  185. easing: (t) => t * t * t * t,
  186. end: { taper: +styles.strokeWidth * 20 },
  187. start: { taper: +styles.strokeWidth * 20 },
  188. simulatePressure: false,
  189. }
  190. )
  191. return getSvgPathFromStroke(stroke)
  192. }