promiseがコールバックを待たないのを修正

This commit is contained in:
kakkokari-gtyih 2024-12-17 18:50:36 +09:00
parent 1a3e96bf7f
commit 88e0b522ea

View file

@ -113,7 +113,8 @@ export function canPreview(config: Partial<WatermarkConfig | WatermarkUserConfig
* @param el * @param el
* @param config * @param config
*/ */
export async function applyWatermark(img: string | Blob, el: HTMLCanvasElement, config: WatermarkConfig | null) { export function applyWatermark(img: string | Blob, el: HTMLCanvasElement | OffscreenCanvas, config: WatermarkConfig | null) {
return new Promise<void>(async (resolve) => {
const canvas = el; const canvas = el;
const ctx = canvas.getContext('2d')!; const ctx = canvas.getContext('2d')!;
const imgEl = new Image(); const imgEl = new Image();
@ -232,6 +233,8 @@ export async function applyWatermark(img: string | Blob, el: HTMLCanvasElement,
} }
ctx.drawImage(watermark, x, y, width, height); ctx.drawImage(watermark, x, y, width, height);
} }
resolve();
}; };
let watermarkUrl: string; let watermarkUrl: string;
@ -248,11 +251,13 @@ export async function applyWatermark(img: string | Blob, el: HTMLCanvasElement,
} }
} }
}; };
if (typeof img === 'string') { if (typeof img === 'string') {
imgEl.src = img; imgEl.src = img;
} else { } else {
imgEl.src = URL.createObjectURL(img); imgEl.src = URL.createObjectURL(img);
} }
});
} }
/** /**
@ -262,12 +267,8 @@ export async function applyWatermark(img: string | Blob, el: HTMLCanvasElement,
* @param config * @param config
* @returns Blob * @returns Blob
*/ */
export function getWatermarkAppliedImage(img: Blob, config: WatermarkConfig): Promise<Blob> { export async function getWatermarkAppliedImage(img: Blob, config: WatermarkConfig): Promise<Blob> {
const canvas = document.createElement('canvas'); const canvas = document.createElement('canvas');
applyWatermark(img, canvas, config); await applyWatermark(img, canvas, config);
return new Promise<Blob>(resolve => { return new Promise(resolve => canvas.toBlob(blob => resolve(blob!)));
canvas.toBlob(blob => {
resolve(blob!);
});
});
} }