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