Some checks failed
Deploy analytics worker / Deploy analytics worker to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
i18n upload strings / Upload latest strings (push) Failing after 1s
Deploy bemo / Deploy bemo to ${{ (github.ref == 'refs/heads/bemo-production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Failing after 1s
Deploy grafana resources / ${{ github.event_name == 'push' && 'Deploy grafana resources' || 'Validate grafana resources (dry run)' }} (push) Failing after 0s
End to end tests (dotcom) / Check for relevant changes (push) Failing after 0s
End to end tests (examples) / Check for relevant changes (push) Failing after 0s
End to end tests (mermaid readability) / Check for relevant changes (push) Failing after 1s
End to end tests (perf) / Check for relevant changes (push) Failing after 0s
Publish editor extensions / Publish editor extensions (push) Failing after 1s
Publish SDK packages / Remove the publish-packages label (push) Has been skipped
Checks / Tests & checks (push) Has been cancelled
Checks / Build all projects (push) Has been cancelled
Deploy templates / Deploy templates (push) Has been cancelled
End to end tests (dotcom) / End to end tests (dotcom) (push) Has been cancelled
End to end tests (mermaid readability) / End to end tests (mermaid readability) (push) Has been cancelled
End to end tests (perf) / End to end tests (perf) (push) Has been cancelled
Deploy .com / Deploy dotcom to ${{ (github.ref == 'refs/heads/production' && 'production') || (github.ref == 'refs/heads/main' && 'staging') || 'preview' }} (push) Has been cancelled
End to end tests (examples) / End to end tests (examples) (push) Has been cancelled
Publish SDK packages / Publish SDK packages (push) Has been cancelled
Publish SDK packages / Tell user released version (push) Has been cancelled
Publish SDK packages / Publishes code templates to separate repositories (push) Has been cancelled
Publish SDK packages / Deploys the template demos with the new SDK version (push) Has been cancelled
160 lines
5.1 KiB
Plaintext
160 lines
5.1 KiB
Plaintext
---
|
|
title: Quick start
|
|
status: published
|
|
author: steveruizok
|
|
date: 3/22/2023
|
|
order: 0
|
|
keywords:
|
|
- hello world
|
|
- intro
|
|
- start
|
|
- installation
|
|
---
|
|
|
|
Have five minutes? Run this command in your terminal to explore tldraw [starter kits](/starter-kits):
|
|
|
|
```bash
|
|
npm create tldraw@latest
|
|
```
|
|
|
|
Have a little more time? Let's try out the tldraw SDK in a React project.
|
|
|
|
If you're new to React, we recommend using a [Vite template](https://vitejs.dev/guide/#scaffolding-your-first-vite-project) as a starter. We'll assume your project is already running locally.
|
|
|
|
## Getting started
|
|
|
|
First, install the `tldraw` package from npm:
|
|
|
|
```bash
|
|
npm install tldraw
|
|
```
|
|
|
|
Next, in your React project, import the `Tldraw` component and tldraw's CSS styles. Then render the `Tldraw` component inside a full screen container:
|
|
|
|
```tsx
|
|
import { Tldraw } from 'tldraw'
|
|
import 'tldraw/tldraw.css'
|
|
|
|
export default function App() {
|
|
return (
|
|
<div style={{ position: 'fixed', inset: 0 }}>
|
|
<Tldraw />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
That's pretty much it! At this point, you should have a complete working single-user canvas. You can draw, write, add images and video, zoom, pan, copy and paste, undo and redo—everything you'd expect from a canvas.
|
|
|
|
You'll be starting from our default [shapes](/docs/shapes), [tools](/docs/tools), and [user interface](/docs/user-interface), but you can customize all of these things for your project if you wish. For now, let's show off a few more features.
|
|
|
|
## Local persistence
|
|
|
|
Let's add local persistence by passing a `persistenceKey` prop to the `Tldraw` component:
|
|
|
|
<FocusLines lines={[4]}>
|
|
|
|
```tsx
|
|
export default function App() {
|
|
return (
|
|
<div style={{ position: 'fixed', inset: 0 }}>
|
|
<Tldraw persistenceKey="example" />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
</FocusLines>
|
|
|
|
The `persistenceKey` prop saves your project to the browser's storage so that it survives a refresh. It also synchronizes the project with other instances that share the same key—even in other browser tabs! Give it a try by opening your app in a second window.
|
|
|
|
## Real-time collaboration
|
|
|
|
To add support for multiple users collaborating in real time, you can use the [**tldraw sync**](/docs/sync) library. Its demo hook connects your app to tldraw's demo server, which hosts temporary rooms.
|
|
|
|
First, install the `@tldraw/sync` package:
|
|
|
|
```bash
|
|
npm install @tldraw/sync
|
|
```
|
|
|
|
Next, import the `useSyncDemo` hook from the `@tldraw/sync` package. Call it in your component with a unique ID and pass the store that it returns to the `Tldraw` component:
|
|
|
|
<FocusLines lines={[2,6,10]}>
|
|
|
|
```tsx
|
|
import { Tldraw } from 'tldraw'
|
|
import { useSyncDemo } from '@tldraw/sync'
|
|
import 'tldraw/tldraw.css'
|
|
|
|
export default function App() {
|
|
const store = useSyncDemo({ roomId: 'insert-any-string-here' })
|
|
|
|
return (
|
|
<div style={{ position: 'fixed', inset: 0 }}>
|
|
<Tldraw store={store} />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
</FocusLines>
|
|
|
|
Try it out by opening your project in a second incognito window, or else access it from another device. You should see all of tldraw's multiplayer features: live cursors, user names, viewport following, cursor chat, and more.
|
|
|
|
If you want to go further with real-time collaboration, be sure to check out our guide to the [**tldraw sync**](/docs/sync) library.
|
|
|
|
## Controlling the canvas
|
|
|
|
The tldraw [editor](/docs/editor) has a runtime JavaScript API. Everything that can happen in tldraw can be done programmatically through the [Editor](?) instance.
|
|
|
|
For simplicity's sake, let's roll back our persistence and sync code. We can then use the `Tldraw` component's `onMount` callback to get access to the Editor instance. We'll use the editor to create a new shape on the canvas, select it, then slowly zoom to it.
|
|
|
|
```tsx
|
|
import { Editor, Tldraw, toRichText } from 'tldraw'
|
|
import 'tldraw/tldraw.css'
|
|
|
|
export default function App() {
|
|
const handleMount = (editor: Editor) => {
|
|
editor.createShape({
|
|
type: 'text',
|
|
x: 200,
|
|
y: 200,
|
|
props: {
|
|
richText: toRichText('Hello world!'),
|
|
},
|
|
})
|
|
|
|
editor.selectAll()
|
|
|
|
editor.zoomToSelection({
|
|
animation: { duration: 5000 },
|
|
})
|
|
}
|
|
|
|
return (
|
|
<div style={{ position: 'fixed', inset: 0 }}>
|
|
<Tldraw onMount={handleMount} />
|
|
</div>
|
|
)
|
|
}
|
|
```
|
|
|
|
The Editor is tldraw's main interface for controlling the canvas. Be sure to check out the [Editor](?) API documentation for more information on what you can do with it, as well as our [guide on using the editor](/docs/editor).
|
|
|
|
## Next steps
|
|
|
|
Now that you've seen how the tldraw canvas works, you can:
|
|
|
|
- Create your own [shapes](/docs/shapes) and [tools](/docs/tools)
|
|
- Customize the [user interface](/docs/user-interface)
|
|
- Learn more about the [editor](/docs/editor)
|
|
- Explore our [examples](/examples)
|
|
- Build with our [starter kits](/starter-kits)
|
|
|
|
The SDK lets you customize shapes, tools, UI, and more. In addition to our long-form docs, we have dozens of examples in our [examples section](/examples) that cover more of its functionality. You can run these locally with the tldraw [GitHub repository](https://github.com/tldraw/tldraw).
|
|
|
|
## Join our community
|
|
|
|
If you build something great, please share it with us in our [#show-and-tell](https://discord.tldraw.com/?utm_source=docs&utm_medium=organic&utm_campaign=sociallink) channel on Discord. Good luck!
|