{"version":3,"file":"Rect.min.mjs","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';\n\nexport const rectDefaultValues: Partial<TClassProperties<Rect>> = {\n  uniformRoundness: false,\n  rx: 0,\n  ry: 0,\n};\n\ninterface UniqueRectProps {\n  uniformRoundness: boolean;\n  rx: number;\n  ry: number;\n}\n\nexport interface SerializedRectProps\n  extends SerializedObjectProps,\n    UniqueRectProps {\n}\n\nexport interface RectProps extends FabricObjectProps, UniqueRectProps {\n}\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  declare uniformRoundness: boolean;\n  /**\n   * Horizontal border radius\n   * @type Number\n   * @default\n   */\n  declare rx: number;\n\n  /**\n   * Vertical border radius\n   * @type Number\n   * @default\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  /**\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    let rx = this.rx ? this.rx : 0;\n    let ry = this.ry ? this.ry : 0;\n    const isRounded = rx !== 0 || ry !== 0;\n\n    if (this.uniformRoundness) {\n      const scaling = this.getObjectScaling();\n      rx = rx / scaling.x;\n      ry = ry / scaling.y;\n    }\n\n    rx = Math.min(rx, w / 2);\n    ry = Math.min(ry, h / 2);\n\n    ctx.beginPath();\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=\"${rx}\" ry=\"${ry}\" width=\"${width}\" height=\"${height}\" />\\n`,\n    ];\n  }\n\n  /**\n   * List of attribute names to account for when parsing SVG element (used by `Rect.fromElement`)\n   * @static\n   * @memberOf Rect\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   * @static\n   * @memberOf Rect\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"],"names":["rectDefaultValues","uniformRoundness","rx","ry","RECT_PROPS","Rect","FabricObject","getDefaults","super","ownDefaults","constructor","options","Object","assign","this","setOptions","_initRxRy","_render","ctx","width","w","height","h","x","y","isRounded","scaling","getObjectScaling","Math","min","beginPath","moveTo","lineTo","bezierCurveTo","kRect","closePath","_renderPaintInOrder","toObject","propertiesToInclude","arguments","length","undefined","_toSVG","fromElement","element","cssRules","left","top","visible","restOfparsedAttributes","parseA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