Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

rectangle.tsx 5.4KB

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