How does export utilities work and how do I save my drawing ? #3778
-
Hello, I recently discovered the beautiful Excalidraw, and very delighted by its simplicity. I'm quickly trying to ramp myself up to understand its capabilities in order to map them to a use case I have in mind for my react app. At a high level, the user starts a new drawing and along the way saves snapshots of the drawing. The user can later go back to any of the snapshot, and use it as a starting point to make further changes. For each of the drawing the user also generates svg that can be shared with other users. Looking at the demos and the documentation, I suppose I can use But how do I save the actual drawing itself. I read that the drawing is saved as JSON. I am not able to understand the difference between Thanks, Vineet |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
@vineetdigit looks like your queries are related to the npm package so I will summarize the usage of the 3 export utilities
const ctx = canvas.getContext("2d");
ctx.font = "30px Virgil";
ctx.strokeText("My custom text", 50, 60); I have updated the example as well in the demo for the same. As per your question of saving the drawing, I think you are talking about saving it so users can retrieve the same when they come back to the app the way it works in However, right now the |
Beta Was this translation helpful? Give feedback.
-
RESOLVED: I can use @ad1992 I took a look at the export const saveToLocalStorage = (
elements: readonly ExcalidrawElement[],
appState: AppState,
) => {
try {
localStorage.setItem(
STORAGE_KEYS.LOCAL_STORAGE_ELEMENTS,
JSON.stringify(clearElementsForLocalStorage(elements)),
);
localStorage.setItem(
STORAGE_KEYS.LOCAL_STORAGE_APP_STATE,
JSON.stringify(clearAppStateForLocalStorage(appState)),
);
} catch (error) {
// Unable to access window.localStorage
console.error(error);
}
}; It looks like on save there are a couple of helper methods Similarly on load the application seems to provide helper methods which prepare the state to be serialized from localStorage. appState = {
...getDefaultAppState(),
...clearAppStateForLocalStorage(
JSON.parse(savedState) as Partial<AppState>,
),
}; These helper methods are not exposed in the package and as a result there are runtime errors since the state saved and loaded from local storage doesn't have the correct shape. An example of application state not working is the
|
Beta Was this translation helpful? Give feedback.
@vineetdigit looks like your queries are related to the npm package so I will summarize the usage of the 3 export utilities
exportToSvg
- As you mentioned, this returns thesvg
of the exported elements.exportToBlob
This will return the blob representing the image in the canvas, you can specify themime type
here (ex "image/png", "image/jpeg"), so will be useful when you want to convert the drawings toimage
.exportToCanvas
- This returns thecanvas
with the exported elements, app state and dimensions. You can use this when you want to customize the canvas let's say add some custom drawing of yours, for example 👇🏻