Skip to content
Snippets Groups Projects
Verified Commit 0563c1a5 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

refactor: performance optimization in the initialization

of the canvas
parent c139047e
No related branches found
No related tags found
1 merge request!9Version 0.4.0 🍾
Pipeline #1365 passed
...@@ -57,7 +57,7 @@ export const App: FC<AppProps> = ({ emulator, backgrounds = ["264653"] }) => { ...@@ -57,7 +57,7 @@ export const App: FC<AppProps> = ({ emulator, backgrounds = ["264653"] }) => {
const [paused, setPaused] = useState(false); const [paused, setPaused] = useState(false);
const [fullscreen, setFullscreen] = useState(false); const [fullscreen, setFullscreen] = useState(false);
const [backgroundIndex, setBackgroundIndex] = useState(0); const [backgroundIndex, setBackgroundIndex] = useState(0);
const interval = useRef<number | undefined>(undefined); const intervalRef = useRef<number | undefined>(undefined);
const getPauseText = () => (paused ? "Resume" : "Pause"); const getPauseText = () => (paused ? "Resume" : "Pause");
const getPauseIcon = () => const getPauseIcon = () =>
paused ? require("../res/play.svg") : require("../res/pause.svg"); paused ? require("../res/play.svg") : require("../res/pause.svg");
...@@ -79,8 +79,8 @@ export const App: FC<AppProps> = ({ emulator, backgrounds = ["264653"] }) => { ...@@ -79,8 +79,8 @@ export const App: FC<AppProps> = ({ emulator, backgrounds = ["264653"] }) => {
setFullscreen(!fullscreen); setFullscreen(!fullscreen);
}; };
const onDrawHandler = (handler: DrawHandler) => { const onDrawHandler = (handler: DrawHandler) => {
if (interval.current) return; if (intervalRef.current) return;
interval.current = setInterval(() => { intervalRef.current = setInterval(() => {
handler(emulator.getImageBuffer(), PixelFormat.RGB); handler(emulator.getImageBuffer(), PixelFormat.RGB);
}, 1000); }, 1000);
}; };
......
...@@ -52,13 +52,13 @@ export const Display: FC<DisplayProps> = ({ ...@@ -52,13 +52,13 @@ export const Display: FC<DisplayProps> = ({
options.scale = window.devicePixelRatio ? window.devicePixelRatio : 1; options.scale = window.devicePixelRatio ? window.devicePixelRatio : 1;
} }
let canvasContents: CanvasContents | null = null;
const classes = () => const classes = () =>
["display", fullscreen ? "fullscreen" : null, size, ...style].join(" "); ["display", fullscreen ? "fullscreen" : null, size, ...style].join(" ");
const [width, setWidth] = useState<number | undefined>(undefined); const [width, setWidth] = useState<number | undefined>(undefined);
const [height, setHeight] = useState<number | undefined>(undefined); const [height, setHeight] = useState<number | undefined>(undefined);
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
const canvasContentsRef = useRef<CanvasContents | undefined>(undefined);
const resizeRef = useRef(() => { const resizeRef = useRef(() => {
const [fullWidth, fullHeight] = crop(options.width / options.height); const [fullWidth, fullHeight] = crop(options.width / options.height);
setWidth(fullWidth); setWidth(fullWidth);
...@@ -66,8 +66,8 @@ export const Display: FC<DisplayProps> = ({ ...@@ -66,8 +66,8 @@ export const Display: FC<DisplayProps> = ({
}); });
useEffect(() => { useEffect(() => {
if (canvasRef.current && !canvasContents) { if (canvasRef.current && !canvasContentsRef.current) {
canvasContents = initCanvas( canvasContentsRef.current = initCanvas(
options.logicWidth, options.logicWidth,
options.logicHeight, options.logicHeight,
canvasRef.current canvasRef.current
...@@ -85,8 +85,8 @@ export const Display: FC<DisplayProps> = ({ ...@@ -85,8 +85,8 @@ export const Display: FC<DisplayProps> = ({
if (onDrawHandler) { if (onDrawHandler) {
onDrawHandler((pixels: Uint8Array, format: PixelFormat) => { onDrawHandler((pixels: Uint8Array, format: PixelFormat) => {
if (!canvasContents) return; if (!canvasContentsRef.current) return;
updateCanvas(canvasContents, pixels, format); updateCanvas(canvasContentsRef.current, pixels, format);
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment