feat: prevent flickering before when switching type of background
This commit is contained in:
parent
13ea98e2e5
commit
22fde9d808
@ -95,6 +95,7 @@ type State = {
|
|||||||
embedScene: boolean;
|
embedScene: boolean;
|
||||||
exportScale: number;
|
exportScale: number;
|
||||||
exportBaseScale: number;
|
exportBaseScale: number;
|
||||||
|
isExportWithFancyBackground: boolean;
|
||||||
renderError: Error | null;
|
renderError: Error | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -118,6 +119,10 @@ type Action =
|
|||||||
| { type: "SET_EMBED_SCENE"; embedScene: boolean }
|
| { type: "SET_EMBED_SCENE"; embedScene: boolean }
|
||||||
| { type: "SET_EXPORT_SCALE"; exportScale: number }
|
| { type: "SET_EXPORT_SCALE"; exportScale: number }
|
||||||
| { type: "SET_ALL_SCALES"; exportScale: number }
|
| { type: "SET_ALL_SCALES"; exportScale: number }
|
||||||
|
| {
|
||||||
|
type: "SET_IS_EXPORT_WITH_FANCY_BACKGROUND";
|
||||||
|
isExportWithFancyBackground: boolean;
|
||||||
|
}
|
||||||
| { type: "SET_RENDER_ERROR"; renderError: Error | null };
|
| { type: "SET_RENDER_ERROR"; renderError: Error | null };
|
||||||
|
|
||||||
const reducer = (state: State, action: Action): State => {
|
const reducer = (state: State, action: Action): State => {
|
||||||
@ -155,6 +160,11 @@ const reducer = (state: State, action: Action): State => {
|
|||||||
return { ...state, embedScene: action.embedScene };
|
return { ...state, embedScene: action.embedScene };
|
||||||
case "SET_EXPORT_SCALE":
|
case "SET_EXPORT_SCALE":
|
||||||
return { ...state, exportScale: action.exportScale };
|
return { ...state, exportScale: action.exportScale };
|
||||||
|
case "SET_IS_EXPORT_WITH_FANCY_BACKGROUND":
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
isExportWithFancyBackground: action.isExportWithFancyBackground,
|
||||||
|
};
|
||||||
case "SET_ALL_SCALES":
|
case "SET_ALL_SCALES":
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
@ -184,6 +194,8 @@ const createInitialState = ({
|
|||||||
embedScene: appState.exportEmbedScene,
|
embedScene: appState.exportEmbedScene,
|
||||||
exportScale: appState.exportScale,
|
exportScale: appState.exportScale,
|
||||||
exportBaseScale: appState.exportScale,
|
exportBaseScale: appState.exportScale,
|
||||||
|
isExportWithFancyBackground:
|
||||||
|
appState.exportBackground && appState.fancyBackgroundImageKey !== "solid",
|
||||||
renderError: null,
|
renderError: null,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -282,6 +294,24 @@ const ImageExportModal = ({
|
|||||||
if (!maxWidth) {
|
if (!maxWidth) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// when switching between solid/no background and image background, we clear the canvas to prevent flickering
|
||||||
|
const isExportWithFancyBackground =
|
||||||
|
appState.exportBackground && appState.fancyBackgroundImageKey !== "solid";
|
||||||
|
|
||||||
|
if (state.isExportWithFancyBackground !== isExportWithFancyBackground) {
|
||||||
|
const existingCanvas = previewNode.querySelector("canvas");
|
||||||
|
if (existingCanvas) {
|
||||||
|
const context = existingCanvas.getContext("2d");
|
||||||
|
|
||||||
|
context!.clearRect(0, 0, existingCanvas.width, existingCanvas.height);
|
||||||
|
}
|
||||||
|
dispatch({
|
||||||
|
type: "SET_IS_EXPORT_WITH_FANCY_BACKGROUND",
|
||||||
|
isExportWithFancyBackground,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
exportToCanvas({
|
exportToCanvas({
|
||||||
elements: state.exportedElements,
|
elements: state.exportedElements,
|
||||||
appState,
|
appState,
|
||||||
@ -294,8 +324,20 @@ const ImageExportModal = ({
|
|||||||
// if converting to blob fails, there's some problem that will
|
// if converting to blob fails, there's some problem that will
|
||||||
// likely prevent preview and export (e.g. canvas too big)
|
// likely prevent preview and export (e.g. canvas too big)
|
||||||
return canvasToBlob(canvas).then(() => {
|
return canvasToBlob(canvas).then(() => {
|
||||||
previewNode.replaceChildren(canvas);
|
const existingCanvas = previewNode.querySelector("canvas");
|
||||||
|
if (!existingCanvas) {
|
||||||
|
previewNode.appendChild(canvas);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
existingCanvas.width = canvas.width;
|
||||||
|
existingCanvas.height = canvas.height;
|
||||||
|
|
||||||
|
const context = existingCanvas.getContext("2d");
|
||||||
|
context!.drawImage(canvas, 0, 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get the 2D rendering context of the existing canvas
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
@ -307,6 +349,7 @@ const ImageExportModal = ({
|
|||||||
appState.fancyBackgroundImageKey,
|
appState.fancyBackgroundImageKey,
|
||||||
files,
|
files,
|
||||||
state.exportedElements,
|
state.exportedElements,
|
||||||
|
state.isExportWithFancyBackground,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -317,7 +360,6 @@ const ImageExportModal = ({
|
|||||||
className={clsx("ImageExportModal__preview__canvas", {
|
className={clsx("ImageExportModal__preview__canvas", {
|
||||||
"ImageExportModal__preview__canvas--img-bcg":
|
"ImageExportModal__preview__canvas--img-bcg":
|
||||||
appState.exportBackground &&
|
appState.exportBackground &&
|
||||||
appState.fancyBackgroundImageKey &&
|
|
||||||
appState.fancyBackgroundImageKey !== "solid",
|
appState.fancyBackgroundImageKey !== "solid",
|
||||||
})}
|
})}
|
||||||
ref={previewRef}
|
ref={previewRef}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user