{"version":3,"file":"Rect.min.mjs","names":[],"sources":["../../../src/shapes/Rect.ts"],"sourcesContent":["import { kRect } from '../constants';\nimport { SHARED_ATTRIBUTES } from '../parser/attributes';\nimport { parseAttributes } from '../parser/parseAttributes';\nimport type { Abortable, TClassProperties, TOptions } from '../typedefs';\nimport { classRegistry } from '../ClassRegistry';\nimport { FabricObject, cacheProperties } from './Object/FabricObject';\nimport type { FabricObjectProps, SerializedObjectProps } from './Object/types';\nimport type { ObjectEvents } from '../EventTypeDefs';\nimport type { CSSRules } from '../parser/typedefs';\nimport { escapeXml } from '../util/lang_string';\n\nexport const rectDefaultValues: Partial<TClassProperties<Rect>> = {\n  rx: 0,\n  ry: 0,\n};\n\ninterface UniqueRectProps {\n  rx: number;\n  ry: number;\n}\n\nexport interface SerializedRectProps\n  extends SerializedObjectProps, UniqueRectProps {}\n\nexport interface RectProps extends FabricObjectProps, UniqueRectProps {}\n\nconst RECT_PROPS = ['rx', 'ry'] as const;\n\nexport class Rect<\n  Props extends TOptions<RectProps> = Partial<RectProps>,\n  SProps extends SerializedRectProps = SerializedRectProps,\n  EventSpec extends ObjectEvents = ObjectEvents,\n>\n  extends FabricObject<Props, SProps, EventSpec>\n  implements RectProps\n{\n  /**\n   * Horizontal border radius\n   * @type Number\n   */\n  declare rx: number;\n\n  /**\n   * Vertical border radius\n   * @type Number\n   */\n  declare ry: number;\n\n  static type = 'Rect';\n\n  static cacheProperties = [...cacheProperties, ...RECT_PROPS];\n\n  static ownDefaults = rectDefaultValues;\n\n  static getDefaults(): Record<string, any> {\n    return {\n      ...super.getDefaults(),\n      ...Rect.ownDefaults,\n    };\n  }\n\n  /**\n   * Constructor\n   * @param {Object} [options] Options object\n   */\n  constructor(options?: Props) {\n    super();\n    Object.assign(this, Rect.ownDefaults);\n    this.setOptions(options);\n    this._initRxRy();\n  }\n  /**\n   * Initializes rx/ry attributes\n   * @private\n   */\n  _initRxRy() {\n    const { rx, ry } = this;\n    if (rx && !ry) {\n      this.ry = rx;\n    } else if (ry && !rx) {\n      this.rx = ry;\n    }\n  }\n\n  /**\n   * @private\n   * @param {CanvasRenderingContext2D} ctx Context to render on\n   */\n  _render(ctx: CanvasRenderingContext2D) {\n    const { width: w, height: h } = this;\n    const x = -w / 2;\n    const y = -h / 2;\n    const rx = this.rx ? Math.min(this.rx, w / 2) : 0;\n    const ry = this.ry ? Math.min(this.ry, h / 2) : 0;\n    const isRounded = rx !== 0 || ry !== 0;\n\n    ctx.beginPath();\n\n    ctx.moveTo(x + rx, y);\n\n    ctx.lineTo(x + w - rx, y);\n    isRounded &&\n      ctx.bezierCurveTo(\n        x + w - kRect * rx,\n        y,\n        x + w,\n        y + kRect * ry,\n        x + w,\n        y + ry,\n      );\n\n    ctx.lineTo(x + w, y + h - ry);\n    isRounded &&\n      ctx.bezierCurveTo(\n        x + w,\n        y + h - kRect * ry,\n        x + w - kRect * rx,\n        y + h,\n        x + w - rx,\n        y + h,\n      );\n\n    ctx.lineTo(x + rx, y + h);\n    isRounded &&\n      ctx.bezierCurveTo(\n        x + kRect * rx,\n        y + h,\n        x,\n        y + h - kRect * ry,\n        x,\n        y + h - ry,\n      );\n\n    ctx.lineTo(x, y + ry);\n    isRounded &&\n      ctx.bezierCurveTo(x, y + kRect * ry, x + kRect * rx, y, x + rx, y);\n\n    ctx.closePath();\n\n    this._renderPaintInOrder(ctx);\n  }\n\n  /**\n   * Returns object representation of an instance\n   * @param {Array} [propertiesToInclude] Any properties that you might want to additionally include in the output\n   * @return {Object} object representation of an instance\n   */\n  toObject<\n    T extends Omit<Props & TClassProperties<this>, keyof SProps>,\n    K extends keyof T = never,\n  >(propertiesToInclude: K[] = []): Pick<T, K> & SProps {\n    return super.toObject([...RECT_PROPS, ...propertiesToInclude]);\n  }\n\n  /**\n   * Returns svg representation of an instance\n   * @return {Array} an array of strings with the specific svg representation\n   * of the instance\n   */\n  _toSVG() {\n    const { width, height, rx, ry } = this;\n    return [\n      '<rect ',\n      'COMMON_PARTS',\n      `x=\"${-width / 2}\" y=\"${\n        -height / 2\n      }\" rx=\"${escapeXml(rx)}\" ry=\"${escapeXml(ry)}\" width=\"${escapeXml(width)}\" height=\"${escapeXml(height)}\" />\\n`,\n    ];\n  }\n\n  /**\n   * List of attribute names to account for when parsing SVG element (used by `Rect.fromElement`)\n   * @see: http://www.w3.org/TR/SVG/shapes.html#RectElement\n   */\n  static ATTRIBUTE_NAMES = [\n    ...SHARED_ATTRIBUTES,\n    'x',\n    'y',\n    'rx',\n    'ry',\n    'width',\n    'height',\n  ];\n\n  /* _FROM_SVG_START_ */\n\n  /**\n   * Returns {@link Rect} instance from an SVG element\n   * @param {HTMLElement} element Element to parse\n   * @param {Object} [options] Options object\n   */\n  static async fromElement(\n    element: HTMLElement | SVGElement,\n    options?: Abortable,\n    cssRules?: CSSRules,\n  ) {\n    const {\n      left = 0,\n      top = 0,\n      width = 0,\n      height = 0,\n      visible = true,\n      ...restOfparsedAttributes\n    } = parseAttributes(element, this.ATTRIBUTE_NAMES, cssRules);\n\n    return new this({\n      ...options,\n      ...restOfparsedAttributes,\n      left,\n      top,\n      width,\n      height,\n      visible: Boolean(visible && width && height),\n    });\n  }\n\n  /* _FROM_SVG_END_ */\n}\n\nclassRegistry.setClass(Rect);\nclassRegistry.setSVGClass(Rect);\n"],"mappings":"+fAWA,MAeM,EAAa,CAAC,KAAM,KAAA,CAE1B,IAAa,EAAb,MAAa,UAKH,CAAA,CAqBR,OAAA,aAAO,CACL,MAAO,CAAA,GACF,MAAM,aAAA,CAAA,GACN,EAAK,YAAA,CAQZ,YAAY,EAAA,CACV,OAAA,CACA,OAAO,OAAO,KAAM,EAAK,YAAA,CACzB,KAAK,WAAW,EAAA,CAChB,KAAK,WAAA,CAMP,WAAA,CACE,GAAA,CAAM,GAAE,EAAA,GAAI,GAAO,KACf,GAAA,CAAO,EACT,KAAK,GAAK,EACD,GAAA,CAAO,IAChB,KAAK,GAAK,GAQd,QAAQ,EAAA,CACN,GAAA,CAAQ,MAAO,EAAG,OAA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