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