2020-11-02 20:14:20 +01:00
# @excalidraw/utils
## Install
2020-11-06 22:06:30 +02:00
```bash
npm install @excalidraw/utils
```
2020-11-02 20:14:20 +01:00
If you prefer Yarn over npm, use this command to install the Excalidraw utils package:
2022-03-04 10:58:02 +05:30
```bash
2020-11-06 22:06:30 +02:00
yarn add @excalidraw/utils
```
2020-11-02 20:14:20 +01:00
## API
2021-05-12 00:24:41 +02:00
### `serializeAsJSON`
See [`serializeAsJSON` ](https://github.com/excalidraw/excalidraw/blob/master/src/packages/excalidraw/README.md#serializeAsJSON ) for API and description.
2020-11-02 20:14:20 +01:00
### `exportToBlob` (async)
Export an Excalidraw diagram to a [Blob ](https://developer.mozilla.org/en-US/docs/Web/API/Blob ).
### `exportToSvg`
Export an Excalidraw diagram to a [SVGElement ](https://developer.mozilla.org/en-US/docs/Web/API/SVGElement ).
## Usage
2023-02-01 19:57:54 +05:30
Excalidraw utils is published as a UMD (Universal Module Definition). If you are using a module bundler (for instance, Webpack), you can import it as an ES6 module:
2020-11-02 20:14:20 +01:00
```js
import { exportToSvg, exportToBlob } from "@excalidraw/utils ";
```
To use it in a browser directly:
```html
2020-11-05 01:08:16 +05:30
< script src = "https://unpkg.com/ @excalidraw/utils@0 .1.0/dist/excalidraw-utils.min.js" ></ script >
2020-11-02 20:14:20 +01:00
< script >
// ExcalidrawUtils is a global variable defined by excalidraw.min.js
const { exportToSvg, exportToBlob } = ExcalidrawUtils;
< / script >
```
Here's the `exportToBlob` and `exportToSvg` functions in action:
```js
const excalidrawDiagram = {
type: "excalidraw",
version: 2,
source: "https://excalidraw.com",
elements: [
{
id: "vWrqOAfkind2qcm7LDAGZ",
type: "ellipse",
x: 414,
y: 237,
width: 214,
height: 214,
angle: 0,
strokeColor: "#000000 ",
backgroundColor: "#15aabf ",
fillStyle: "hachure",
strokeWidth: 1,
strokeStyle: "solid",
roughness: 1,
opacity: 100,
groupIds: [],
2022-12-08 23:48:49 +08:00
roundness: null,
2020-11-02 20:14:20 +01:00
seed: 1041657908,
version: 120,
versionNonce: 1188004276,
isDeleted: false,
boundElementIds: null,
},
],
appState: {
viewBackgroundColor: "#ffffff ",
gridSize: null,
},
};
// Export the Excalidraw diagram as SVG string
const svg = exportToSvg(excalidrawDiagram);
console.log(svg.outerHTML);
// Export the Excalidraw diagram as PNG Blob URL
(async () => {
const blob = await exportToBlob({
...excalidrawDiagram,
mimeType: "image/png",
});
const urlCreator = window.URL || window.webkitURL;
console.log(urlCreator.createObjectURL(blob));
})();
```