Skip to content

newkub/tldraw-graph-layout

 
 

Repository files navigation

tldraw graph layout

This repo demonstrates an interactive force-directed graph layout integration with tldraw. It uses WebCola, JS port of libcola. This repo is aimed to be a starting point for further exploration and there's lots to be explored! I would also love PRs if you have core improvements which everyone can benefit from.

You can mess around with it online here

tldraw.graph.montage.mov

Usage

  1. Select shapes you wish to include in the physics simulation
  2. Click the "Graph" button (or hit "G")
  3. Move shapes around and watch it go brrrrrrrrrr
  4. Click "Graph" again to stop the simulation

Behaviour

  • Any shapes connected with arrows are included in the graph layout (this extends to videos, frames, and all other shapes)
  • When you select a shape, it will be "fixed" so you can move it around. Deselect to unfreeze.

Constraints

  • Making a shape red will fix it in place
  • Light blue arrows create an alignment constraint between two shapes
  • Much more interesting constraints are possible, PRs welcome!

Setup

yarn install
yarn dev

Then go to http://localhost:5173 in your browser.

Multiplayer is supported* using yjs and partykit. To deploy:

yarn deploy

*Note that multiplayer is essentially the same as a single client manually moving many shapes each frame, but it sure is fun! Due to a connection bug I've disabled multiplayer (err, commented out line 25 of App.tsx). PRs for multiplayer fixes/improvements are very welcome!

Contributing

Please open an issue or PR if you have any suggestions or improvements! Especially looking for:

  • Architecture / performance improvements / bug fixes
  • More interesting constraint demonstrations
  • Approaches to proper multiplayer support

Current Limitations

  • Rotation is not considered in the layout (PRs welcome!)
  • Performance is much poorer than it needs to be, we're currently restarting the layout sim every frame (I know, I know...) and PRs to fix this or otherwise speed things up are very welcome!
  • Due to the current edge length calculation, it's possible for the graph to never reach a stable / zero energy state

About

constraint-based graph layout in tldraw

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 87.0%
  • CSS 9.1%
  • JavaScript 2.7%
  • HTML 1.2%