Giter VIP home page Giter VIP logo

excalidraw-embed's Introduction

Excalidraw

npm npm

Excalidraw exported as a component to directly embed in your projects

Installation

You can use npm

npm install react react-dom excalidraw

or via yarn

yarn add react react-dom excalidraw

After installation you will see the below font files in dist directory which you will have to copy to the path from where assets are served. In the demo its served from public folder.

Cascadia.woff2
FG_Virgil.woff2

Demo

Try here

Usage

import React, { useState } from "react";
import Excalidraw from "excalidraw";
import InitialData from "./initialData";

import "excalidraw/dist/excalidraw.min.css";
import "./styles.css";

export default function App() {
  const onChange = (elements, state) => {
    console.log("Elements :", elements, "State : ", state);
  };

  const onUsernameChange = (username) => {
    console.log("current username", username);
  };
  const [dimensions, setDimensions] = useState({
    width: window.innerWidth,
    height: window.innerHeight,
  });

  const onResize = () => {
    setDimensions({
      width: window.innerWidth,
      height: window.innerHeight,
    });
  };

  useEffect(() => {
    window.addEventListener("resize", onResize);

    return () => window.removeEventListener("resize", onResize);
  }, []);

  const { width, height } = dimensions;
  const options = { zenModeEnabled: true, viewBackgroundColor: "#AFEEEE" };
  return (
    <div className="App">
      <Excalidraw
        width={width}
        height={height}
        initialData={InitialData}
        onChange={onChange}
        options={options}
        user={{ name: "Excalidraw User" }}
        onUsernameChange={onUsernameChange}
      />
    </div>
  );
}

Edit excalidraw-embed

Props

Name Type Default Description
width Number window.innerWidth The width of Excalidraw component
height Number window.innerHeight The height of Excalidraw component
initialData ExcalidrawElement[] [] The initial data with which app loads.
onChange Function This callback is triggered whenever the component updates due to any change. This callback will receive the excalidraw elements and the current app state.
options Object Each option has a default value. See options section for more details Options to be passed to Excalidraw
user { name?: string } User details. The name refers to the name of the user to be shown
onUsernameChange Function This callback is triggered whenever the username change. This callback receives the username.

width

This props defines the width of the Excalidraw component. Defaults to window.innerWidth if not passed.

height

This props defines the height of the Excalidraw component. Defaults to window.innerHeight if not passed.

initialData

This helps to load Excalidraw with initialData. Defaults to []. This should be array of ExcalidrawElement[] as shown below.

[
  {
    type: "rectangle",
    version: 141,
    versionNonce: 361174001,
    isDeleted: false,
    id: "oDVXy8D6rom3H1-LLH2-f",
    fillStyle: "hachure",
    strokeWidth: 1,
    strokeStyle: "solid",
    roughness: 1,
    opacity: 100,
    angle: 0,
    x: 100.50390625,
    y: 93.67578125,
    strokeColor: "#000000",
    backgroundColor: "transparent",
    width: 186.47265625,
    height: 141.9765625,
    seed: 1968410350,
    groupIds: [],
  },
];

You might want to use this if you are using some backend to store the excalidraw elements and want to preload excalidraw with those elements.

onChange

Every time component updates, this callback if passed will get triggered and has the below signature.

onChange(ExcalidrawElement[], AppState)

Here you can try saving the data to your backend or local storage for example.

options

These contains the options object with which you can control the initial rendering of Excalidraw. Currently it only contains the below keys, more to be added in future.

Name type default Description
zenModeEnabled boolean false Decides whether to enable zen mode
viewBackgroundColor string #fff The background color of Excalidraw

Here is how you use it

{ zenModeEnabled: true, viewBackgroundColor: "AFEEEE" }

user

This is the user name which shows during collaboration. Defaults to {name: ''}. This is Object as later more attributes like user cursor colors can also be added.

onUsernameChange

This callback if passed gets triggered whenever username changes and has the below signature

onUserNameChange(username);

Local Installation

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Clone the repo

git clone https://github.com/excalidraw/excalidraw-embed.git

Commands

Command Description
npm install Install the dependencies
npm start Run the project
npm run fix Reformat all files with Prettier
npm test Run tests
npm run test:update Update test snapshots
npm run test:code Test for formatting with Prettier

Contributing

Currently this is a fork of excalidraw and all the changes for making it an embedable component are done on top of it. So for any changes, please open an issue first to discuss what you would like to change.

excalidraw-embed's People

Contributors

ad1992 avatar amrew avatar bradymadden97 avatar dai-shi avatar dependabot-preview[bot] avatar dwelle avatar fausto95 avatar gasimgasimzada avatar giovannigiordano avatar gmertk avatar idlewinn avatar j-f1 avatar jquintozamora avatar jsjoeio avatar kbariotis avatar kentbeck avatar lipis avatar lissitz avatar lusingander avatar muzakparov avatar nanot1m avatar nminhnguyen avatar paulomenezes avatar petehunt avatar pshihn avatar tomayac avatar vjeux avatar voluntadpear avatar xixixao avatar yongdamsh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.