{"version":3,"file":"XImage.min.mjs","sources":["../../../../src/shapes/canvasx/XImage.ts"],"sourcesContent":["import type { BaseFilter } from '../../filters/BaseFilter';\nimport { SHARED_ATTRIBUTES } from '../../parser/attributes';\nimport { TSize } from '../../typedefs';\nimport { findScaleToCover, findScaleToFit } from '../../util/misc/findScaleTo';\nimport { parsePreserveAspectRatioAttribute } from '../../util/misc/svgParsing';\nimport { classRegistry } from '../../ClassRegistry';\nimport { TOptions } from '../../typedefs';\nimport type { FabricObjectProps, SerializedObjectProps } from '../Object/types';\nimport type { ObjectEvents } from '../../EventTypeDefs';\n// @todo Would be nice to have filtering code not imported directly.\n\nimport { WidgetImageInterface, EntityKeys } from './type/widget.entity.image';\nimport { FabricImage } from '../Image';\nimport { WidgetType } from './type/widget.type';\nimport { FileObject } from './type/file';\n\n\nexport type ImageSource =\n  | HTMLImageElement\n  | HTMLVideoElement\n  | HTMLCanvasElement;\n\ninterface UniqueImageProps {\n  srcFromAttribute: boolean;\n  minimumScaleTrigger: number;\n  cropX: number;\n  cropY: number;\n  imageSmoothing: boolean;\n  crossOrigin: string | null;\n  filters: BaseFilter[];\n  resizeFilter?: BaseFilter;\n}\n\nexport const imageDefaultValues: Partial<UniqueImageProps> &\n  Partial<FabricObjectProps> = {\n  strokeWidth: 0,\n  srcFromAttribute: false,\n  minimumScaleTrigger: 0.5,\n  cropX: 0,\n  cropY: 0,\n  imageSmoothing: true,\n  crossOrigin: 'anonymous',\n  originX: 'center',\n  originY: 'center',\n};\n\nexport interface SerializedImageProps extends SerializedObjectProps {\n  src: string;\n  crossOrigin: string | null;\n  filters: any[];\n  resizeFilter?: any;\n  cropX: number;\n  cropY: number;\n}\n\nexport interface ImageProps extends FabricObjectProps, UniqueImageProps { }\n\nconst IMAGE_PROPS = ['cropX', 'cropY'] as const;\n\n/**\n * @tutorial {@link http://fabricjs.com/fabric-intro-part-1#images}\n */\nexport class XImage<\n  Props extends TOptions<ImageProps> = Partial<ImageProps>,\n  SProps extends SerializedImageProps = SerializedImageProps,\n  EventSpec extends ObjectEvents = ObjectEvents\n> extends FabricImage implements WidgetImageInterface {\n\n  cropWidth: number;\n  cropHeight: number;\n  previewImage: FileObject;\n  imageSrc: FileObject;\n  version: string;\n  updatedAt: number;\n\n  createdAt: number;\n  createdBy: string;\n  boardId: string;\n  objType: WidgetType;\n  userId: string;\n  zIndex: number;\n  /* boardx cusotm function */\n\n  static type: WidgetType = 'XImage';\n  static objType: WidgetType = 'XImage';\n\n  constructor(image: any, options: any) {\n\n    options.objType = 'XImage';\n    options.cornerColor = 'white';\n    options.cornerSize = 10;\n    options.cornerStyle = 'circle';\n    options.transparentCorners = false;\n    options.cornerStrokeColor = 'gray';\n    super(image, options);\n    Object.assign(this, options);\n\n  }\n  updatedBy: string;\n  updatedByName: string;\n  markdownText: string;\n\n  createdByName: string;\n\n\n  getObject() {\n    const entityKeys: string[] = EntityKeys;\n    const result: Record<string, any> = {};\n\n    entityKeys.forEach((key) => {\n      if (key in this) {\n        result[key] = (this as any)[key];\n      }\n    });\n\n    return result;\n  }\n\n\n  /**\n   * Renders the image on the canvas context, correctly handling scaling and cropping.\n   * @private\n   * @param {CanvasRenderingContext2D} ctx Context to render on\n   */\n  _renderFill(ctx: CanvasRenderingContext2D) {\n    const elementToDraw = this._element;\n    if (!elementToDraw) {\n      return;\n    }\n\n    // Get the object's dimensions\n    const w = this.width,\n      h = this.height,\n      // Crop values cannot be less than 0\n      cropX = Math.max(this.cropX, 0),\n      cropY = Math.max(this.cropY, 0),\n      // Get the natural dimensions of the image element\n      elWidth =\n        (elementToDraw as HTMLImageElement).naturalWidth || elementToDraw.width,\n      elHeight =\n        (elementToDraw as HTMLImageElement).naturalHeight || elementToDraw.height,\n      // Calculate source width and height, ensuring we don't exceed image bounds\n      sX = cropX,\n      sY = cropY,\n      sW = Math.min(w, elWidth - cropX),\n      sH = Math.min(h, elHeight - cropY),\n      // Destination coordinates (centered)\n      x = -w / 2,\n      y = -h / 2,\n      destW = w,\n      destH = h;\n\n    // Draw the image onto the canvas context\n    ctx.drawImage(\n      elementToDraw,\n      sX,\n      sY,\n      sW,\n      sH,\n      x,\n      y,\n      destW,\n      destH\n    );\n  }\n\n\n  /**\n   * Decide if the object should cache or not. Create its own cache level\n   * needsItsOwnCache should be used when the object drawing method requires\n   * a cache step. None of the fabric classes requires it.\n   * Generally you do not cache objects in groups because the group outside is cached.\n   * This is the special image version where we would like to avoid caching where possible.\n   * Essentially images do not benefit from caching. They may require caching, and in that\n   * case we do it. Also caching an image usually ends in a loss of details.\n   * A full performance audit should be done.\n   * @return {Boolean}\n   */\n  shouldCache() {\n    return this.needsItsOwnCache();\n  }\n\n  /**\n   * needed to check if image needs resize\n   * @private\n   */\n  _needsResize() {\n    const scale = this.getTotalObjectScaling();\n    return scale.x !== this._lastScaleX || scale.y !== this._lastScaleY;\n  }\n\n  /**\n   * @private\n   * @deprecated unused\n   */\n  _resetWidthHeight() {\n    this.set(this.getOriginalSize());\n  }\n\n  /**\n   * @private\n   * Set the width and the height of the image object, using the element or the\n   * options.\n   */\n  _setWidthHeight({ width, height }: Partial<TSize> = {}) {\n    const size = this.getOriginalSize();\n    this.width = width || size.width;\n    this.height = height || size.height;\n  }\n\n  /**\n   * Calculate offset for center and scale factor for the image in order to respect\n   * the preserveAspectRatio attribute\n   * @private\n   */\n  parsePreserveAspectRatioAttribute() {\n    const pAR = parsePreserveAspectRatioAttribute(\n      this.preserveAspectRatio || ''\n    ),\n      pWidth = this.width,\n      pHeight = this.height,\n      parsedAttributes = { width: pWidth, height: pHeight };\n    let rWidth = this._element.width,\n      rHeight = this._element.height,\n      scaleX = 1,\n      scaleY = 1,\n      offsetLeft = 0,\n      offsetTop = 0,\n      cropX = 0,\n      cropY = 0,\n      offset;\n\n    if (pAR && (pAR.alignX !== 'none' || pAR.alignY !== 'none')) {\n      if (pAR.meetOrSlice === 'meet') {\n        scaleX = scaleY = findScaleToFit(this._element, parsedAttributes);\n        offset = (pWidth - rWidth * scaleX) / 2;\n        if (pAR.alignX === 'Min') {\n          offsetLeft = -offset;\n        }\n        if (pAR.alignX === 'Max') {\n          offsetLeft = offset;\n        }\n        offset = (pHeight - rHeight * scaleY) / 2;\n        if (pAR.alignY === 'Min') {\n          offsetTop = -offset;\n        }\n        if (pAR.alignY === 'Max') {\n          offsetTop = offset;\n        }\n      }\n      if (pAR.meetOrSlice === 'slice') {\n        scaleX = scaleY = findScaleToCover(this._element, parsedAttributes);\n        offset = rWidth - pWidth / scaleX;\n        if (pAR.alignX === 'Mid') {\n          cropX = offset / 2;\n        }\n        if (pAR.alignX === 'Max') {\n          cropX = offset;\n        }\n        offset = rHeight - pHeight / scaleY;\n        if (pAR.alignY === 'Mid') {\n          cropY = offset / 2;\n        }\n        if (pAR.alignY === 'Max') {\n          cropY = offset;\n        }\n        rWidth = pWidth / scaleX;\n        rHeight = pHeight / scaleY;\n      }\n    } else {\n      scaleX = pWidth / rWidth;\n      scaleY = pHeight / rHeight;\n    }\n    return {\n      width: rWidth,\n      height: rHeight,\n      scaleX,\n      scaleY,\n      offsetLeft,\n      offsetTop,\n      cropX,\n      cropY,\n    };\n  }\n\n  /**\n   * Default CSS class name for canvas\n   * @static\n   * @type String\n   * @default\n   */\n  static CSS_CANVAS = 'canvas-img';\n\n  /**\n   * List of attribute names to account for when parsing SVG element (used by {@link Image.fromElement})\n   * @static\n   * @see {@link http://www.w3.org/TR/SVG/struct.html#ImageElement}\n   */\n  static ATTRIBUTE_NAMES = [\n    ...SHARED_ATTRIBUTES,\n    'x',\n    'y',\n    'width',\n    'height',\n    'preserveAspectRatio',\n    'xlink:href',\n    'crossOrigin',\n    'image-rendering',\n  ];\n\n  _stopEvent(e: any) {\n    if (e.preventDefault) e.preventDefault();\n    if (e.stopPropagation) e.stopPropagation();\n  }\n\n  cloneWidget() {\n    return this.toObject();\n  }\n}\n\nclassRegistry.setClass(XImage);\nclassRegistry.setSVGClass(XImage);\n"],"names":["XImage","FabricImage","constructor","image","options","objType","cornerColor","cornerSize","cornerStyle","transparentCorners","cornerStrokeColor","super","_defineProperty","this","Object","assign","getObject","result","EntityKeys","forEach","key","_renderFill","ctx","elementToDraw","_element","w","width","h","height","cropX","Math","max","cropY","elWidth","naturalWidth","elHeight","naturalHeight","sX","sY","sW","min","sH","x","y","destW","destH","drawImage","shouldCache","needsItsOwnCache","_needsResize","scale","getTotalObjectScaling","_lastScaleX","_lastScaleY","_resetWidthHeight","set","getOriginalSize","_setWidthHeight","arguments","length","undefined","size","parsePreserveAspectRatioAttribute","pAR","preserveAspectRatio","pWidth","pHeight","parsedAttributes","offset","rWidth","rHeight","scaleX","scaleY","offsetLeft","offsetTop","alignX","alignY","meetOrSlice","findScaleToFit","findScaleToCover","_stopEvent","e","preventDefault","stopPropagation","cloneWidget","toObject","SHARED_ATTRIBUTES","classRegistry","setClass","setSVGClass"],"mappings":"sfA8DO,MAAMA,UAIHC,EAoBRC,WAAAA,CAAYC,EAAYC,GAEtBA,EAAQC,QAAU,SAClBD,EAAQE,YAAc,QACtBF,EAAQG,WAAa,GACrBH,EAAQI,YAAc,SACtBJ,EAAQK,oBAAqB,EAC7BL,EAAQM,kBAAoB,OAC5BC,MAAMR,EAAOC,GAASQ,EAAAC,KAAA,iBAAA,GAAAD,EAAAC,KAAA,kBAAA,GAAAD,EAAAC,KAAA,oBAAA,GAAAD,EAAAC,KAAA,gBAAA,GAAAD,EAAAC,KAAA,eAAA,GAAAD,EAAAC,KAAA,iBAAA,GAAAD,EAAAC,KAAA,iBAAA,GAAAD,EAAAC,KAAA,iBAAA,GAAAD,EAAAC,KAAA,eAAA,GAAAD,EAAAC,KAAA,eAAA,GAAAD,EAAAC,KAAA,cAAA,GAAAD,EAAAC,KAAA,cAAA,GAAAD,EAAAC,KAAA,iBAAA,GAAAD,EAAAC,KAAA,qBAAA,GAAAD,EAAAC,KAAA,oBAAA,GAAAD,EAAAC,KAAA,qBAAA,GACtBC,OAAOC,OAAOF,KAAMT,EAEtB,CAQAY,SAAAA,GACE,MACMC,EAA8B,C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