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.3KB

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