1
0
mirror of https://github.com/excalidraw/excalidraw.git synced 2024-11-10 11:35:52 +01:00
excalidraw/packages/utils
Ryan Di bbdcd30a73
refactor: update collision from ga to vector geometry (#7636)
* new collision api

* isPointOnShape

* removed redundant code

* new collision methods in app

* curve shape takes starting point

* clean up geometry

* curve rotation

* freedraw

* inside curve

* improve ellipse inside check

* ellipse distance func

* curve inside

* include frame name bounds

* replace previous private methods for getting elements at x,y

* arrow bound text hit detection

* keep iframes on top

* remove dependence on old collision methods from app

* remove old collision functions

* move some hit functions outside of app

* code refactor

* type

* text collision from inside

* fix context menu test

* highest z-index collision

* fix 1px away binding test

* strictly less

* remove unused imports

* lint

* 'ignore' resize flipping test

* more lint fix

* skip 'flips while resizing' test

* more test

* fix merge errors

* fix selection in resize test

* added a bit more comment

---------

Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com>
2024-04-04 16:31:23 +08:00
..
__snapshots__ feat: follow mode (#6848) 2023-12-15 00:07:11 +01:00
geometry refactor: update collision from ga to vector geometry (#7636) 2024-04-04 16:31:23 +08:00
bbox.ts feat: move utils to utils package and make @excalidraw/utils a workspace (#7432) 2023-12-13 21:51:27 +05:30
CHANGELOG.md build: decouple package deps and introduce yarn workspaces (#7415) 2023-12-12 11:32:51 +05:30
collision.ts refactor: update collision from ga to vector geometry (#7636) 2024-04-04 16:31:23 +08:00
export.test.ts feat: move utils to utils package and make @excalidraw/utils a workspace (#7432) 2023-12-13 21:51:27 +05:30
export.ts fix: export utils from excalidraw package in excalidraw library (#7731) 2024-02-28 11:14:57 +05:30
global.d.ts build: export types for @excalidraw/utils (#7736) 2024-02-29 15:43:04 +05:30
index.ts fix: export utils from excalidraw package in excalidraw library (#7731) 2024-02-28 11:14:57 +05:30
package.json build: export types for @excalidraw/utils (#7736) 2024-02-29 15:43:04 +05:30
README.md build: decouple package deps and introduce yarn workspaces (#7415) 2023-12-12 11:32:51 +05:30
tsconfig.json build: export types for @excalidraw/utils (#7736) 2024-02-29 15:43:04 +05:30
utils.unmocked.test.ts build: decouple package deps and introduce yarn workspaces (#7415) 2023-12-12 11:32:51 +05:30
webpack.prod.config.js build: decouple package deps and introduce yarn workspaces (#7415) 2023-12-12 11:32:51 +05:30
withinBounds.test.ts feat: move utils to utils package and make @excalidraw/utils a workspace (#7432) 2023-12-13 21:51:27 +05:30
withinBounds.ts fix: make bounds independent of scene (#7679) 2024-02-19 19:39:14 +05:30

@excalidraw/utils

Install

npm install @excalidraw/utils

If you prefer Yarn over npm, use this command to install the Excalidraw utils package:

yarn add @excalidraw/utils

API

serializeAsJSON

See serializeAsJSON for API and description.

exportToBlob (async)

Export an Excalidraw diagram to a Blob.

exportToSvg

Export an Excalidraw diagram to a SVGElement.

Usage

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:

import { exportToSvg, exportToBlob } from "@excalidraw/utils";

To use it in a browser directly:

<script src="https://unpkg.com/@excalidraw/utils@0.1.0/dist/excalidraw-utils.min.js"></script>
<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:

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: [],
      roundness: null,
      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));
})();