templates/shader/README.md
</picture>
This template demonstrates how to integrate WebGL shaders with tldraw, creating dynamic backgrounds that respond to canvas interactions. It includes four complete examples and a reusable WebGLManager base class for building custom shader effects.
Install dependencies with yarn or npm install.
Run the development server with yarn dev or npm run dev.
Open http://localhost:5173/ in your browser to see the app.
src/WebGLManager.ts) - Handles WebGL lifecycle, viewport synchronization, and animation loop integration with tldraw's reactive systemsrc/config-panel/) - Pre-built UI controls for adjusting shader parameters with localStorage persistenceSwitch between examples using the toggle buttons in the tldraw style panel.
A real-time fluid simulation that creates dynamic flows from shape interactions. Based on Pavel Dobryakov's WebGL fluid implementation.
See: src/fluid/ | Documentation
An animated gradient shader demonstrating time-based effects and uniform management.
See: src/rainbow/
Dynamic shadow casting from tldraw shapes using raymarching and signed distance fields.
See: src/shadow/
A bare-bones template for starting new shader projects. Renders a solid color that adapts to dark mode.
See: src/minimal/ | Documentation
The WebGLManager class (src/WebGLManager.ts) provides the foundation for all shader examples. It handles:
onInitialize(), onUpdate(), onRender(), onDispose()To create a custom shader, extend WebGLManager and implement the lifecycle hooks. See the minimal example for a starting point.
The config panel system (src/config-panel/) provides reusable UI components for creating interactive shader controls:
ConfigPanel - Collapsible container with reset functionalityConfigPanelSlider - Numeric slider controlConfigPanelBooleanControl - Checkbox controlConfig values are stored in reactive atoms and automatically persisted to localStorage. See config-panel.md for usage details.
Each example follows this pattern:
WebGLManagerConfig in config.tsWebGLManageruseLayoutEffect to initialize the managerBackground componentSee any of the example directories for complete implementations.
The fastest way to create a custom shader is to copy the minimal example:
cp -r src/minimal src/my-shader
Then customize the following files:
config.ts - Define your configuration parametersfragment.glsl / vertex.glsl - Write your shader codeMyShaderManager.ts - Extend WebGLManager and implement rendering logicMyRenderer.tsx - Create the React componentMyConfigPanel.tsx - Build UI controlsFinally, register your shader in src/App.tsx.
For a detailed walkthrough, see src/minimal/minimal-example.md.
The WebGLManager has direct access to the tldraw editor instance, allowing you to:
this.editor.getCurrentPageShapes()this.editor.store.listen()this.editor.getCamera()this.editor.pageToViewport()this.editor.inputs.currentPagePointThe fluid simulation example (src/fluid/FluidManager.ts) demonstrates shape integration in depth.
This project is provided under the MIT license found here. The tldraw SDK is provided under the tldraw license.
Copyright (c) 2024-present tldraw Inc. The tldraw name and logo are trademarks of tldraw. Please see our trademark guidelines for info on acceptable usage.
You can find tldraw on npm here.
Please see our contributing guide. Found a bug? Please submit an issue.
Have questions, comments or feedback? Join our discord. For the latest news and release notes, visit tldraw.dev.
Find us on Twitter/X at @tldraw.