Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

rectangle.tsx 5.5KB

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