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

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