From 27fd150a2049e4634833daf620d7c7a90e7b8ec2 Mon Sep 17 00:00:00 2001 From: David Luzar Date: Tue, 5 Sep 2023 12:06:48 +0200 Subject: [PATCH] fix: canvas flickering due to resetting canvas on skipped frames (#6960) --- src/components/canvases/StaticCanvas.tsx | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/components/canvases/StaticCanvas.tsx b/src/components/canvases/StaticCanvas.tsx index f32133f3..dfdf8b51 100644 --- a/src/components/canvases/StaticCanvas.tsx +++ b/src/components/canvases/StaticCanvas.tsx @@ -37,10 +37,25 @@ const StaticCanvas = (props: StaticCanvasProps) => { canvas.classList.add("excalidraw__canvas", "static"); } - canvas.style.width = `${props.appState.width}px`; - canvas.style.height = `${props.appState.height}px`; - canvas.width = props.appState.width * props.scale; - canvas.height = props.appState.height * props.scale; + const widthString = `${props.appState.width}px`; + const heightString = `${props.appState.height}px`; + if (canvas.style.width !== widthString) { + canvas.style.width = widthString; + } + if (canvas.style.height !== heightString) { + canvas.style.height = heightString; + } + + const scaledWidth = props.appState.width * props.scale; + const scaledHeight = props.appState.height * props.scale; + // setting width/height resets the canvas even if dimensions not changed, + // which would cause flicker when we skip frame (due to throttling) + if (canvas.width !== scaledWidth) { + canvas.width = scaledWidth; + } + if (canvas.height !== scaledHeight) { + canvas.height = scaledHeight; + } renderStaticScene( {