cache the custom image element and improve jittering experience

This commit is contained in:
ad1992 2022-03-23 19:42:39 +05:30
parent 39d0084a5e
commit 61699ff3c2

View File

@ -6,6 +6,7 @@ import {
NonDeletedExcalidrawElement, NonDeletedExcalidrawElement,
ExcalidrawFreeDrawElement, ExcalidrawFreeDrawElement,
ExcalidrawImageElement, ExcalidrawImageElement,
ExcalidrawCustomElement,
} from "../element/types"; } from "../element/types";
import { import {
isTextElement, isTextElement,
@ -189,6 +190,9 @@ const drawImagePlaceholder = (
); );
}; };
const customElementImgCache: {
[key: ExcalidrawCustomElement["name"]]: HTMLImageElement;
} = {};
const drawElementOnCanvas = ( const drawElementOnCanvas = (
element: NonDeletedExcalidrawElement, element: NonDeletedExcalidrawElement,
rc: RoughCanvas, rc: RoughCanvas,
@ -254,10 +258,20 @@ const drawElementOnCanvas = (
case "custom": { case "custom": {
const config = renderConfig.customElementsConfig?.find( const config = renderConfig.customElementsConfig?.find(
(config) => config.name === element.name, (config) => config.name === element.name,
); )!;
if (!customElementImgCache[config.name]) {
const img = document.createElement("img"); const img = document.createElement("img");
img.src = config!.svg; img.id = config.name;
context.drawImage(img, 0, 0, element.width, element.height); img.src = config.svg;
customElementImgCache[img.id] = img;
}
context.drawImage(
customElementImgCache[config.name],
0,
0,
element.width,
element.height,
);
break; break;
} }
default: { default: {