feat: throttle scene rendering to animation framerate (#5422)

This commit is contained in:
David Luzar 2022-07-07 11:47:37 +02:00 committed by GitHub
parent c725f84334
commit b6bb74d08d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 89 additions and 51 deletions

View file

@ -1 +0,0 @@
export { renderScene } from "./renderScene";

View file

@ -47,7 +47,11 @@ import {
TransformHandles,
TransformHandleType,
} from "../element/transformHandles";
import { viewportCoordsToSceneCoords, supportsEmoji } from "../utils";
import {
viewportCoordsToSceneCoords,
supportsEmoji,
throttleRAF,
} from "../utils";
import { UserIdleState } from "../types";
import { THEME_FILTER } from "../constants";
import {
@ -568,6 +572,32 @@ export const renderScene = (
return { atLeastOneVisibleElement: visibleElements.length > 0, scrollBars };
};
/** renderScene throttled to animation framerate */
export const renderSceneThrottled = throttleRAF(
(
elements: readonly NonDeletedExcalidrawElement[],
appState: AppState,
selectionElement: NonDeletedExcalidrawElement | null,
scale: number,
rc: RoughCanvas,
canvas: HTMLCanvasElement,
renderConfig: RenderConfig,
callback?: (data: ReturnType<typeof renderScene>) => void,
) => {
const ret = renderScene(
elements,
appState,
selectionElement,
scale,
rc,
canvas,
renderConfig,
);
callback?.(ret);
},
{ trailing: true },
);
const renderTransformHandles = (
context: CanvasRenderingContext2D,
renderConfig: RenderConfig,