| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 | 
							- import { uniqueId } from 'utils/utils'
 - import vec from 'utils/vec'
 - import { DashStyle, RectangleShape, ShapeType } from 'types'
 - import {
 -   getSvgPathFromStroke,
 -   translateBounds,
 -   rng,
 -   shuffleArr,
 -   pointsBetween,
 - } from 'utils/utils'
 - import {
 -   defaultStyle,
 -   getShapeStyle,
 -   getStrokeDashArray,
 - } from 'state/shape-styles'
 - import getStroke from 'perfect-freehand'
 - import { registerShapeUtils } from './register'
 - 
 - const pathCache = new WeakMap<number[], string>([])
 - 
 - const rectangle = registerShapeUtils<RectangleShape>({
 -   boundsCache: new WeakMap([]),
 - 
 -   create(props) {
 -     return {
 -       id: uniqueId(),
 -       seed: Math.random(),
 -       type: ShapeType.Rectangle,
 -       isGenerated: false,
 -       name: 'Rectangle',
 -       parentId: 'page1',
 -       childIndex: 0,
 -       point: [0, 0],
 -       size: [1, 1],
 -       radius: 2,
 -       rotation: 0,
 -       isAspectRatioLocked: false,
 -       isLocked: false,
 -       isHidden: false,
 -       style: defaultStyle,
 -       ...props,
 -     }
 -   },
 - 
 -   render(shape) {
 -     const { id, size, radius, style } = shape
 - 
 -     const styles = getShapeStyle(style)
 - 
 -     if (style.dash === DashStyle.Solid) {
 -       if (!pathCache.has(shape.size)) {
 -         renderPath(shape)
 -       }
 - 
 -       const path = pathCache.get(shape.size)
 - 
 -       return (
 -         <g id={id}>
 -           <rect
 -             rx={radius}
 -             ry={radius}
 -             x={+styles.strokeWidth / 2}
 -             y={+styles.strokeWidth / 2}
 -             width={Math.max(0, size[0] + -styles.strokeWidth)}
 -             height={Math.max(0, size[1] + -styles.strokeWidth)}
 -             strokeWidth={0}
 -             fill={styles.fill}
 -           />
 -           <path d={path} fill={styles.stroke} />
 -         </g>
 -       )
 -     }
 - 
 -     return (
 -       <rect
 -         id={id}
 -         width={size[0]}
 -         height={size[1]}
 -         fill={styles.fill}
 -         stroke={styles.stroke}
 -         strokeDasharray={getStrokeDashArray(
 -           style.dash,
 -           +styles.strokeWidth
 -         ).join(' ')}
 -         strokeDashoffset={-(size[0] + size[1])}
 -       />
 -     )
 -   },
 - 
 -   getBounds(shape) {
 -     if (!this.boundsCache.has(shape)) {
 -       const [width, height] = shape.size
 -       const bounds = {
 -         minX: 0,
 -         maxX: width,
 -         minY: 0,
 -         maxY: height,
 -         width,
 -         height,
 -       }
 - 
 -       this.boundsCache.set(shape, bounds)
 -     }
 - 
 -     return translateBounds(this.boundsCache.get(shape), shape.point)
 -   },
 - 
 -   hitTest() {
 -     return true
 -   },
 - 
 -   transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
 -     if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
 -       shape.size = [bounds.width, bounds.height]
 -       shape.point = [bounds.minX, bounds.minY]
 -     } else {
 -       shape.size = vec.mul(
 -         initialShape.size,
 -         Math.min(Math.abs(scaleX), Math.abs(scaleY))
 -       )
 - 
 -       shape.point = [
 -         bounds.minX +
 -           (bounds.width - shape.size[0]) *
 -             (scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
 -         bounds.minY +
 -           (bounds.height - shape.size[1]) *
 -             (scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
 -       ]
 - 
 -       shape.rotation =
 -         (scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
 -           ? -initialShape.rotation
 -           : initialShape.rotation
 -     }
 - 
 -     return this
 -   },
 - 
 -   transformSingle(shape, bounds) {
 -     shape.size = [bounds.width, bounds.height]
 -     shape.point = [bounds.minX, bounds.minY]
 -     return this
 -   },
 - })
 - 
 - export default rectangle
 - 
 - function renderPath(shape: RectangleShape) {
 -   const styles = getShapeStyle(shape.style)
 - 
 -   const getRandom = rng(shape.id)
 - 
 -   const baseOffset = +styles.strokeWidth / 2
 - 
 -   const offsets = Array.from(Array(4)).map(() => [
 -     getRandom() * baseOffset,
 -     getRandom() * baseOffset,
 -   ])
 - 
 -   const [w, h] = shape.size
 -   const tl = vec.add([0, 0], offsets[0])
 -   const tr = vec.add([w, 0], offsets[1])
 -   const br = vec.add([w, h], offsets[2])
 -   const bl = vec.add([0, h], offsets[3])
 - 
 -   const lines = shuffleArr(
 -     [
 -       pointsBetween(tr, br),
 -       pointsBetween(br, bl),
 -       pointsBetween(bl, tl),
 -       pointsBetween(tl, tr),
 -     ],
 -     Math.floor(5 + getRandom() * 4)
 -   )
 - 
 -   const stroke = getStroke(
 -     [...lines.flat().slice(2), ...lines[0], ...lines[0].slice(4)],
 -     {
 -       size: 1 + +styles.strokeWidth,
 -       thinning: 0.6,
 -       easing: (t) => t * t * t * t,
 -       end: { taper: +styles.strokeWidth * 20 },
 -       start: { taper: +styles.strokeWidth * 20 },
 -       simulatePressure: false,
 -     }
 -   )
 - 
 -   pathCache.set(shape.size, getSvgPathFromStroke(stroke))
 - }
 
 
  |