{ "version": 3, "sources": ["../../../src/lib/utils/assets.ts"], "sourcesContent": ["import { getBrowserCanvasMaxSize } from '../shapes/shared/getBrowserCanvasMaxSize'\nimport { isAnimated } from './is-gif-animated'\n\ntype BoxWidthHeight = {\n\tw: number\n\th: number\n}\n\n/**\n * Contains the size within the given box size\n *\n * @param originalSize - The size of the asset\n * @param containBoxSize - The container size\n * @returns Adjusted size\n * @public\n */\nexport function containBoxSize(\n\toriginalSize: BoxWidthHeight,\n\tcontainBoxSize: BoxWidthHeight\n): BoxWidthHeight {\n\tconst overByXScale = originalSize.w / containBoxSize.w\n\tconst overByYScale = originalSize.h / containBoxSize.h\n\n\tif (overByXScale <= 1 && overByYScale <= 1) {\n\t\treturn originalSize\n\t} else if (overByXScale > overByYScale) {\n\t\treturn {\n\t\t\tw: originalSize.w / overByXScale,\n\t\t\th: originalSize.h / overByXScale,\n\t\t}\n\t} else {\n\t\treturn {\n\t\t\tw: originalSize.w / overByYScale,\n\t\t\th: originalSize.h / overByYScale,\n\t\t}\n\t}\n}\n\n/**\n * Get the size of an image from its source.\n *\n * @param dataURLForImage - The image file as a string.\n * @param width - The desired width.\n * @param height - The desired height.\n * @public\n */\nexport async function getResizedImageDataUrl(\n\tdataURLForImage: string,\n\twidth: number,\n\theight: number\n): Promise {\n\treturn await new Promise((resolve) => {\n\t\tconst img = new Image()\n\t\timg.onload = async () => {\n\t\t\t// Initialize the canvas and it's size\n\t\t\tconst canvas = document.createElement('canvas')\n\t\t\tconst ctx = canvas.getContext('2d')\n\n\t\t\tif (!ctx) return\n\n\t\t\tconst canvasSizes = await getBrowserCanvasMaxSize()\n\n\t\t\tlet desiredWidth = width * 2\n\t\t\tlet desiredHeight = height * 2\n\t\t\tconst aspectRatio = img.width / img.height\n\n\t\t\tif (desiredWidth > canvasSizes.maxWidth) {\n\t\t\t\tdesiredWidth = canvasSizes.maxWidth\n\t\t\t\tdesiredHeight = desiredWidth / aspectRatio\n\t\t\t}\n\n\t\t\tif (desiredHeight > canvasSizes.maxHeight) {\n\t\t\t\tdesiredHeight = canvasSizes.maxHeight\n\t\t\t\tdesiredWidth = desiredHeight * aspectRatio\n\t\t\t}\n\n\t\t\tif (desiredWidth * desiredHeight > canvasSizes.maxArea) {\n\t\t\t\tconst ratio = Math.sqrt(canvasSizes.maxArea / (desiredWidth * desiredHeight))\n\t\t\t\tdesiredWidth *= ratio\n\t\t\t\tdesiredHeight *= ratio\n\t\t\t}\n\n\t\t\tcanvas.width = desiredWidth\n\t\t\tcanvas.height = desiredHeight\n\n\t\t\t// Draw image and export to a data-uri\n\t\t\tctx.drawImage(img, 0, 0, canvas.width, canvas.height)\n\t\t\tconst newDataURL = canvas.toDataURL()\n\n\t\t\t// Do something with the result, like overwrite original\n\t\t\tresolve(newDataURL)\n\t\t}\n\t\timg.crossOrigin = 'anonymous'\n\t\timg.src = dataURLForImage\n\t})\n}\n\n/** @public */\nexport const DEFAULT_ACCEPTED_IMG_TYPE = ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml']\n/** @public */\nexport const DEFAULT_ACCEPTED_VID_TYPE = ['video/mp4', 'video/quicktime']\n\n/** @public */\nexport async function isGifAnimated(file: File): Promise {\n\treturn await new Promise((resolve, reject) => {\n\t\tconst reader = new FileReader()\n\t\treader.onerror = () => reject(reader.error)\n\t\treader.onload = () => {\n\t\t\tresolve(reader.result ? isAnimated(reader.result as ArrayBuffer) : false)\n\t\t}\n\t\treader.readAsArrayBuffer(file)\n\t})\n}\n"], "mappings": "AAAA,SAAS,+BAA+B;AACxC,SAAS,kBAAkB;AAepB,SAAS,eACf,cACAA,iBACiB;AACjB,QAAM,eAAe,aAAa,IAAIA,gBAAe;AACrD,QAAM,eAAe,aAAa,IAAIA,gBAAe;AAErD,MAAI,gBAAgB,KAAK,gBAAgB,GAAG;AAC3C,WAAO;AAAA,EACR,WAAW,eAAe,cAAc;AACvC,WAAO;AAAA,MACN,GAAG,aAAa,IAAI;AAAA,MACpB,GAAG,aAAa,IAAI;AAAA,IACrB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