Both React components can put a whiteboard on a page and let someone draw a rectangle. My image-editing prototype needed more. When someone selected a car in a photograph, I had to give the pixels inside the box to a vision model and draw an edit suggestion back in place. Choosing an SDK now meant finding out who handled selection events, coordinate transformations, and image export.
I preferred tldraw's module organization at the time. I ultimately used Excalidraw in the prototype because I also wanted to commercialize the tool.
Who handles the mouse: the application or the editor?
On April 4 I went from Excalidraw's root package.json on GitHub into excalidraw-app, then opened App.tsx. After cloning it locally I read another file with the same name. The two entry points answer different questions. The outer excalidraw-app/App.tsx connects initial data, change callbacks, and the collaboration interface to <Excalidraw>. To trace what happens after pressing the mouse on a photograph, start instead at packages/excalidraw/components/App.tsx in the component package. In version 0.18.0, which I installed, it was roughly 11,000 lines and contained the pointer move and down handlers. Geometry and rendering had separate modules.12
Would adding my tools beside the canvas also require modifying that large mouse-handling component? On the afternoon of April 5 I explored Excalidraw's Sidebar, welcome screen, and main menu. To let a user pick a reference image on the right, for example, the application can add a named <Sidebar> child to <Excalidraw> and put the image picker there. Next I read the export and Restore documentation. Restore covers reopening a saved scene. If older data lacks a setting, restoreAppState fills it from an existing local value or a default.34
I had looked at tldraw in late 2024. During this comparison, when I found a relevant implementation in Excalidraw, I'd look up the equivalent logic in tldraw. If a photo already had a selection box, how did each work out its position in the image and export only its contents? I may have pressed . on a GitHub repository page to open VS Code for the Web and jump between files there. I no longer remember exactly which files I clicked. Below I use April 2025 source to match up the implementations.56
Take selection. tldraw's SelectTool breaks an interaction into states. PointingShape handles pressing the pointer down on a shape, Brushing handles dragging a selection from empty space, and Translating moves selected objects. Each state has its own file. When I wanted to change a particular gesture, the action's name suggested where to start reading. That's what mattered most to me when I described the modules as clearer.7
What state does a whiteboard need to remember?
Another question I had was what a program needs to remember beyond drawing shapes if I build a whiteboard myself. That was why I opened tldraw's packages/state-react/src/lib on the evening of April 4. After browsing it I returned to packages/state/src/lib, followed by Atom.ts in the same tab.89
Take a street photograph. I draw a rectangle around a taxi, click it, and selection handles appear. I then click empty space. The handles disappear, but my rectangle remains. Two pieces of data can already change independently: there is a rectangle in the document, and a particular shape is currently selected. The data the program has to remember and may change through interaction is what I mean by state.
| What I do | What the editor remembers |
|---|---|
| Move the rectangle a little to the right, save, and reopen | Document content. The photo and rectangle have separate shape records. Moving changes the rectangle's position, which saving should retain.10 |
| Click the rectangle, then empty space | Current selection. Each page has a list of my selected shape IDs, selectedShapeIds. Deselecting clears the list and leaves the rectangle in the document.11 |
| Zoom the car to 200% and pan to its rear | View. tldraw calls the vantage point and scale for a page the camera. x and y record panning and z records zoom. The rectangle stays in the same place on the photo.12 |
| Hold down the mouse, move, and release | The tool's current action. Dragging a selection from empty space and dragging selected shapes are different selection-tool states. One changes what is selected, the other changes shape positions. Releasing the pointer ends the gesture.7 |
| Look at the image with another person | Collaborator presence. Displaying the other person's pointer requires associating cursor coordinates with identity. tldraw also records their page and selection.13 |
What should be restored when reopening? tldraw's getSnapshot returns savable data in two parts, document and session. The former stores drawings. The latter can retain a page's camera position, zoom, and selected shapes. I can share the rectangle with another person yet reopen my own view focused on the rear of the car.12
With concrete examples of state, state-react is easier to interpret. The toolbar's zoom button reads and subscribes to the zoom ratio through useValue('zoom', () => editor.getZoomLevel(), [editor]). Subscribing lets the button reread the value and update the percentage when it changes. In Atom.ts, an atom is an independently tracked value. A read can record a dependency. Writing back the same value returns immediately, while an actual change notifies dependent code.89
Why can the box surround the car but the crop miss it?
On the morning of April 7 I also looked up window.getSelection(). Drag over a sentence on a webpage and it tells the program which text is selected. A lone text caret also has a position. Excalidraw's selectNode uses it to select the contents of a web node, such as a passage of text. But when I box a taxi on a photo, the browser's text selection can be empty even if the rectangle is perfect. Finding the car's pixels starts with locating the rectangle on the canvas.14
Images and selection boxes on the board use scene coordinates. Mouse events provide positions relative to the browser viewport. The underlying Canvas stores bitmap pixels. After panning, the car stays in the same place in the scene but appears somewhere else on screen. On a high-density display, one display pixel can correspond to several bitmap pixels.
Suppose the rectangle around the car starts at scene coordinates (40, 35), with width 48 and height 24. The board is zoomed to 2× and panned 60 scene units right and 15 down. Apply only zoom when cropping and you'll start at (80, 70), capturing the storefront above and to the left. The car and selection moved together. The crop origin must move 60 × 2 = 120 display pixels right and 15 × 2 = 30 down.15
Drawn using Excalidraw 0.18.0 coordinate conversion. Coordinates start at the top left of the canvas, increasing rightward and downward. The two insets are cropped from the equal-size boxes on the left.
The correct origin is ((40 + 60) × 2, (35 + 15) × 2) = (200, 100). Multiply the width and height by zoom alone, giving 96 × 48 display pixels. If cropping directly from a rendered Canvas bitmap whose width and height are both double its displayed dimensions, multiply the entire source rectangle for drawImage by two: take 192 × 96 bitmap pixels from (400, 200). If the origin came from the mouse's clientX/clientY, it already includes the board's pan and zoom. First subtract the canvas's top and left offsets within the viewport, then convert to bitmap pixels.15
I kept tracing this offset. Excalidraw's frameClip shifts the drawing context origin to the frame, accounts for scroll offset, then establishes a clipping area. bootstrapCanvas resets transforms and applies pixel scaling. Extracting the clipping function without looking at canvas initialization makes it easy to count an offset or a scale factor twice. I also copied getElementBounds, getCommonBounds, and a section handling rotated bounds locally for repeated reading. These details match my coordinate questions to Cursor at the time.1617
| Task | Excalidraw | tldraw |
|---|---|---|
| Which element did the pointer hit? | I opened collision.ts. It separates hitting an element from being inside its bounding rectangle.18 | editor.getShapeAtPoint accepts a page point, interior-hit option, and hit tolerance. It can find the photograph shape. Identifying the car within that photograph takes separate recognition.18 |
| Find an object's or selection's bounds | getElementBounds computes one element's bounds and getCommonBounds combines several. Separate functions convert between scene and viewport coordinates.1715 | editor.getShapePageBounds(id) gives a shape's bounds on the page. pageToScreen and screenToPage explicitly name the conversion direction.19 |
| Export selected content | exportToCanvas or exportToBlob accepts scene elements and associated files. The export dialog, public utilities, and underlying drawing have separate entry points.20 | editor.toImage(shapes, options) accepts shapes or IDs and optional export bounds. This version creates SVG first, then renders a bitmap in the chosen format.21 |
| Track refreshes of the selection box and image | staticScene.ts handles the static scene, interactiveScene.ts draws feedback such as selection boxes, and ShapeCache caches generated shapes.22 | Each Shape remembers its last transform, dimensions, and clipping styles and only updates the DOM when they change. Cullable shapes outside the viewport are hidden.23 |
One difference is easy to miss. Exporting the selected element generally gives you the whole photograph. Exporting the boxed region of the photograph also requires treating the rectangle as the crop bounds. Passing the whole picture and a transparent rectangle to an exporter only says that those two objects are in the scene. For a close-up of the car, specify the export bounds and decide whether the selection outline belongs in the result. With tldraw's API at the time, the photo ID goes to toImage, the box's page bounds go to bounds, and padding: 0 removes the margins. Now the image contains only the photograph, cropped using the selection.24
Following imports: where do they lead back?
Which files determine a polyline's bounds?
In the Excalidraw code I copied into boundary_draw.js, two functions shared the name getElementAbsoluteCoords. Below are excerpts by file, omitting branches for other elements, type declarations, and unrelated imports. Follow the two from clauses to see why I doubled back.25
// bounds.ts:通用入口遇到线条或箭头
import { LinearElementEditor } from "./linearElementEditor";
// getElementAbsoluteCoords 的线条分支
return LinearElementEditor.getElementAbsoluteCoords(
element, elementsMap, includeBoundText,
);
// linearElementEditor.ts:同名静态方法里,取缓存路径算边界
import { getMinMaxXYFromCurvePathOps } from "./bounds";
// 至少两个点且已有图形缓存时
const shape = ShapeCache.generateElementShape(element, null);
const ops = getCurvePathOps(shape[0]);
const [minX, minY, maxX, maxY] = getMinMaxXYFromCurvePathOps(ops);
The first jump leads from the bounds module to the line editor, and the second returns to a path extrema function in the bounds module. The same linearElementEditor.ts also directly imports the editor package's Scene. Above the line, a maintainer wrote: “TODO: remove direct dependency on the scene, should be passed in or injected instead”. I started out following one set of coordinates and ended up needing to understand the line editor's relationship with the whole scene.26
Now compare tldraw's representation of an ordinary polyline. These excerpts from three files follow the call order and retain the branch for a polyline made of straight segments.27
// LineShapeUtil.tsx:getGeometryForLineShape 的折线分支
const points = linePointsToArray(shape).map(Vec.From)
return new Polyline2d({ points })
// Polyline2d.ts:折线本身提供顶点
getVertices() {
return this.points
}
// Geometry2d.ts:父类从顶点求局部外接矩形
getBounds() {
return Box.FromPoints(this.vertices)
}
At Box.FromPoints I only have to consider the farthest left, right, top, and bottom points. If I need to locate this box on the whole board, I can turn to editor.getShapePageBounds. Local geometry and page coordinates each have a natural stopping point.19
What sits beneath the image export function?
When Excalidraw previews an exported image, two packages each contain an exportToCanvas. Comparing the imports side by side is more informative than counting directory levels. These excerpts keep only the Canvas-related imports.28
// packages/excalidraw/components/ImageExportDialog.tsx
import { exportToCanvas } from "@excalidraw/utils/export";
// packages/utils/src/export.ts
import { exportToCanvas as _exportToCanvas } from "@excalidraw/excalidraw/scene/export";
The dialog calls the first function. After restoring scene data, the utility package calls the second to draw the preview. Seeing the first import, I'd assumed canvas export was implemented in the utilities package. Following the second showed me the drawing itself was back in the editor package.
This also affects when users download the export code. The editor's LayerUI imports the export dialog statically. If you emit one JavaScript file per package, the two files can depend on each other, causing the browser to load utilities with the editor. Splitting by package alone won't defer that download until someone clicks Export. The official application in this version specified chunking rules for only certain language files. Inspect the production build to see where the export code actually lands.29
Once loaded, cross-package calls are ordinary function calls. One more interesting operation in the preview is editing the filename. In browsers that show its input, every filename change reruns the preview useEffect. Even changing one character without touching the picture restores the elements, draws a Canvas again, and converts it to a Blob to check exportability. With large scenes this repeated work may slow the preview, but the actual delay needs measurement.30
For tldraw's division of export responsibilities, look at the end of exportAs.ts. The filename and extension are already assembled. Only three lines follow.31
const { blob } = await editor.toImage(ids, opts)
const file = new File([blob], name, { type: blob.type })
downloadFile(file)
When the first line returns, the image content has been generated. To change the download filename I can stay in this file. To understand PNG pixel generation I can open editor.toImage and inspect SVG-to-bitmap conversion. That's what I liked: the return value tells me exactly what the next layer has already done for me.
Why did I end up using Excalidraw?
At the time I preferred tldraw's features and UI and suspected Excalidraw might perform less well. I don't know which performed better. I never benchmarked both with the same scene.
I wanted to retain the option of commercializing the product. When reading about tldraw that day I opened Steve Ruiz's 2023 dual-licensing announcement. It stated the choice at that time directly: “If you would like to use the tldraw SDK in a commercial product […] you will need to purchase a commercial-use license”. By April 2025 the SDK license in the repository already allowed commercial use under conditions including retaining the “Made with tldraw” watermark. A separate commercial license allowed its removal. Looking back, it's easy to use an old article to misstate the terms of a later version.32
Excalidraw's MIT license permits commercial use, modification, and distribution and requires retaining the copyright and license notices in copies or substantial portions. I felt that suited my product better, so I integrated @excalidraw/excalidraw into the prototype. The suggestion in the screen recording appears beside the selection box and can be accepted with Tab. It was implemented on this board; see the image-editing prototype retrospective.
The rights I wanted to grant others in the application code I wrote outside the component were a separate decision. See Writing a LICENSE for a Closed-Source Project.33
Footnotes
-
Excalidraw. (2025, April). Excalidraw app. GitHub. Application entry point and component assembly. ↩
-
Excalidraw. (2025). App.tsx (v0.18.0). GitHub. handleCanvasPointerMove, handleCanvasPointerDown. ↩
-
Excalidraw. (n.d.). Restore Utilities. Excalidraw Docs. restoreAppState fallback rules, contemporary documentation. ↩
-
GitHub. (n.d.). Using VS Code for the Web from GitHub. GitHub Docs. Opening the editor by pressing the period key on a repository page. ↩
-
tldraw. (2025, April 3). tldraw [source snapshot, 3.11.0]. GitHub. Commit 629125a2. The local Excalidraw checkout was c79e892e; the prototype lockfile pinned v0.18.0. References distinguish the source tree inspected from the installed version. ↩
-
tldraw. (2025, April). SelectTool. GitHub. Registered child states. ↩ ↩2
-
tldraw. (2025, April). state-react and the zoom button. GitHub. React state directory inspected, contemporary useValue subscription explanation, zoom button reading the level. ↩ ↩2
-
tldraw. (2025, April). Atom.ts. GitHub. File URL when browsing, contemporary get, set, and change notifications. ↩ ↩2
-
tldraw. (2025, April). TLBaseShape and TLShape. GitHub. Shape position, rotation, and parent, shape records in the document. ↩
-
tldraw. (2025, April). TLPageState. GitHub. Current page selection, hover, and editing targets. ↩
-
tldraw. (2025, April). TLEditorSnapshot and TLSessionStateSnapshot. GitHub. Separate document and session snapshot, per-page camera and selection. ↩ ↩2
-
tldraw. (2025, April). TLInstancePresence. GitHub. User, page, selection, and pointer location. ↩
-
MDN contributors. (n.d.). Window: getSelection() method. MDN Web Docs. Return value and usage. Excalidraw. (2025, April). utils.ts. GitHub. selectNode and removeSelection. ↩
-
Excalidraw. (2025). Coordinate utilities (v0.18.0). GitHub. Viewport and scene coordinate conversion. ↩ ↩2 ↩3
-
Excalidraw. (2025, April). Static scene renderer and helpers. GitHub. frameClip, bootstrapCanvas. ↩
-
Excalidraw. (2025, April). Element bounds. GitHub. getElementBounds and getCommonBounds. ↩ ↩2
-
Excalidraw. (2025, April). Collision. GitHub. Element and bounding-box hit testing. tldraw. (2025, April). Editor.getShapeAtPoint. GitHub. Position, interior hits, and tolerance. ↩ ↩2
-
tldraw. (2025, April). Editor. GitHub. Getting a shape's page bounds, screen/page coordinate conversion. ↩ ↩2
-
Excalidraw. (2025, April). Export utilities. GitHub. Exporting scene elements to Canvas and Blob. ↩
-
tldraw. (2025, April). Editor.toImage. GitHub. Image export. ↩
-
Excalidraw. (2025, April). Rendering. GitHub. Shape cache, drawing selection boxes, viewport filtering. ↩
-
tldraw. (2025, April). Shape. GitHub. Per-shape style and visibility updates. ↩
-
tldraw. (2025, April). getSvgJsx. GitHub. Selecting the shapes to draw, bounds, and padding separately. ↩
-
Excalidraw. (2025, April). Element bounds and linear element editor. GitHub. Generic entry delegates to the line editor, editor imports functions from bounds, cached branch uses path extrema. ↩
-
Excalidraw. (2025, April). LinearElementEditor. GitHub. TODO before importing Scene, original line 23. ↩
-
tldraw. (2025, April). Line shape geometry. GitHub. Choosing geometry by line type, polyline vertices, local bounds from the vertices. ↩
-
Excalidraw. (2025, April). Image export preview. GitHub. Dialog imports utilities, utilities import scene implementation back, restoring and calling. ↩
-
Excalidraw. (2025, April). LayerUI and Vite configuration. GitHub. UI statically imports export dialog, official manual chunking rules. ↩
-
Excalidraw. (2025, April). ImageExportDialog. GitHub. Preview generation, Blob conversion, and effect dependencies, filename input updates projectName. ↩
-
tldraw. (2025, April). Image export. GitHub. useExportAs imports export function, exportAs invokes editor, names and downloads file, editor converts SVG to bitmap. ↩
-
Ruiz, S. (2023, December 20). License updates for the tldraw SDK. Substack. The new license, paragraph 2. tldraw. (2025, April). tldraw license. GitHub. Permissions, Conditions, and Alternative License. ↩
-
Excalidraw. (2025). MIT License (v0.18.0). GitHub. Full license. ↩