{"version":3,"file":"useClips.js","sources":["../../../../../../packages/components/watermark/src/useClips.ts"],"sourcesContent":["import type { WatermarkProps } from './watermark'\n\nexport const FontGap = 3\n\nfunction prepareCanvas(\n width: number,\n height: number,\n ratio = 1\n): [\n ctx: CanvasRenderingContext2D,\n canvas: HTMLCanvasElement,\n realWidth: number,\n realHeight: number\n] {\n const canvas = document.createElement('canvas')\n const ctx = canvas.getContext('2d')!\n const realWidth = width * ratio\n const realHeight = height * ratio\n canvas.setAttribute('width', `${realWidth}px`)\n canvas.setAttribute('height', `${realHeight}px`)\n ctx.save()\n\n return [ctx, canvas, realWidth, realHeight]\n}\n\n/**\n * Get the clips of text content.\n * This is a lazy hook function since SSR no need this\n */\nexport default function useClips() {\n // Get single clips\n function getClips(\n content: NonNullable | HTMLImageElement,\n rotate: number,\n ratio: number,\n width: number,\n height: number,\n font: Required>,\n gapX: number,\n gapY: number\n ): [dataURL: string, finalWidth: number, finalHeight: number] {\n // ================= Text / Image =================\n const [ctx, canvas, contentWidth, contentHeight] = prepareCanvas(\n width,\n height,\n ratio\n )\n\n if (content instanceof HTMLImageElement) {\n // Image\n ctx.drawImage(content, 0, 0, contentWidth, contentHeight)\n } else {\n // Text\n const {\n color,\n fontSize,\n fontStyle,\n fontWeight,\n fontFamily,\n textAlign,\n textBaseline,\n } = font\n const mergedFontSize = Number(fontSize) * ratio\n\n ctx.font = `${fontStyle} normal ${fontWeight} ${mergedFontSize}px/${height}px ${fontFamily}`\n ctx.fillStyle = color\n ctx.textAlign = textAlign\n ctx.textBaseline = textBaseline\n const contents = Array.isArray(content) ? content : [content]\n contents?.forEach((item, index) => {\n ctx.fillText(\n item ?? '',\n contentWidth / 2,\n index * (mergedFontSize + FontGap * ratio)\n )\n })\n }\n\n // ==================== Rotate ====================\n const angle = (Math.PI / 180) * Number(rotate)\n const maxSize = Math.max(width, height)\n const [rCtx, rCanvas, realMaxSize] = prepareCanvas(maxSize, maxSize, ratio)\n\n // Copy from `ctx` and rotate\n rCtx.translate(realMaxSize / 2, realMaxSize / 2)\n rCtx.rotate(angle)\n if (contentWidth > 0 && contentHeight > 0) {\n rCtx.drawImage(canvas, -contentWidth / 2, -contentHeight / 2)\n }\n\n // Get boundary of rotated text\n function getRotatePos(x: number, y: number) {\n const targetX = x * Math.cos(angle) - y * Math.sin(angle)\n const targetY = x * Math.sin(angle) + y * Math.cos(angle)\n return [targetX, targetY]\n }\n\n let left = 0\n let right = 0\n let top = 0\n let bottom = 0\n\n const halfWidth = contentWidth / 2\n const halfHeight = contentHeight / 2\n const points = [\n [0 - halfWidth, 0 - halfHeight],\n [0 + halfWidth, 0 - halfHeight],\n [0 + halfWidth, 0 + halfHeight],\n [0 - halfWidth, 0 + halfHeight],\n ]\n points.forEach(([x, y]) => {\n const [targetX, targetY] = getRotatePos(x, y)\n left = Math.min(left, targetX)\n right = Math.max(right, targetX)\n top = Math.min(top, targetY)\n bottom = Math.max(bottom, targetY)\n })\n\n const cutLeft = left + realMaxSize / 2\n const cutTop = top + realMaxSize / 2\n const cutWidth = right - left\n const cutHeight = bottom - top\n\n // ================ Fill Alternate ================\n const realGapX = gapX * ratio\n const realGapY = gapY * ratio\n const filledWidth = (cutWidth + realGapX) * 2\n const filledHeight = cutHeight + realGapY\n\n const [fCtx, fCanvas] = prepareCanvas(filledWidth, filledHeight)\n\n function drawImg(targetX = 0, targetY = 0) {\n fCtx.drawImage(\n rCanvas,\n cutLeft,\n cutTop,\n cutWidth,\n cutHeight,\n targetX,\n targetY,\n cutWidth,\n cutHeight\n )\n }\n drawImg()\n drawImg(cutWidth + realGapX, -cutHeight / 2 - realGapY / 2)\n drawImg(cutWidth + realGapX, +cutHeight / 2 + realGapY / 2)\n\n return [fCanvas.toDataURL(), filledWidth / ratio, filledHeight / ratio]\n }\n\n return getClips\n}\n"],"names":[],"mappings":";;;;AAAY,MAAC,OAAO,GAAG,EAAE;AACzB,SAAS,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,GAAG,CAAC,EAAE;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