feat: load background image in promise

This commit is contained in:
Arnošt Pleskot 2023-08-02 16:17:23 +02:00
parent b64c3693fe
commit fe17d88b74
No known key found for this signature in database

View File

@ -391,7 +391,8 @@ const addExportBackground = (
normalizedCanvasHeight: number,
svgUrl: string,
rectangleColor: string,
): void => {
): Promise<void> => {
return new Promise((resolve, reject) => {
const MARGIN = 24;
const BORDER_RADIUS = 12;
@ -497,10 +498,17 @@ const addExportBackground = (
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
resolve();
};
img.onerror = (): void => {
reject(new Error(`Failed to load image with URL ${svgUrl}`));
};
// Start loading the image
img.src = svgUrl;
});
};
export const _renderScene = ({
@ -558,13 +566,19 @@ export const _renderScene = ({
}
context.save();
if (isExporting && exportBackgroundImage) {
addExportBackground(
(async () => {
try {
await addExportBackground(
canvas,
normalizedCanvasWidth,
normalizedCanvasHeight,
exportBackgroundImage,
renderConfig.viewBackgroundColor,
renderConfig.viewBackgroundColor!,
);
} catch (error) {
console.error("Failed to add background:", error);
}
})();
} else {
context.fillStyle = renderConfig.viewBackgroundColor;
context.fillRect(0, 0, normalizedCanvasWidth, normalizedCanvasHeight);