A graphics library to enable the real-time rendering of 2D signed distance fields on the web. https://sdf-2d.schmelczer.dev
  • TypeScript 86.9%
  • GLSL 10.9%
  • JavaScript 1.6%
  • Shell 0.6%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Andras Schmelczer a32b0306a2
All checks were successful
Check / check (push) Successful in 32s
Publish / build (push) Successful in 23s
Publish / publish-npm (push) Successful in 38s
Bump version to 0.8.1
2026-10-28 22:43:48 +00:00
.forgejo/workflows Add CI 2026-06-06 15:42:40 +01:00
docs Update doc gen 2026-10-27 21:58:52 +00:00
media Replace thesis 2020-11-28 13:40:34 +01:00
scripts Add version bump script 2026-06-06 15:50:38 +01:00
src Add more docs 2026-10-27 21:58:16 +00:00
.gitignore Improve gitignore 2022-01-29 21:11:22 +01:00
.npmignore Add more options 2020-09-17 17:51:45 +02:00
.prettierrc Add files 2020-09-15 10:08:16 +02:00
custom.d.ts Bump dependencies 2026-06-06 14:58:32 +01:00
documentation-readme.md Update doc gen 2026-10-27 21:58:52 +00:00
eslint.config.mjs Bump dependencies 2026-06-06 14:58:32 +01:00
package.json Bump version to 0.8.1 2026-10-28 22:43:48 +00:00
README.md Update doc gen 2026-10-27 21:58:52 +00:00
renovate.json Add renovate.json 2026-09-19 10:42:04 +00:00
tsconfig.json Bump dependencies 2026-06-06 14:58:32 +01:00
typedoc.json Update doc gen 2026-10-27 21:58:52 +00:00
webpack.config.js Bump dependencies 2026-06-06 14:58:32 +01:00

SDF-2D logo SDF-2D library

A graphics library for real-time rendering of 2D signed distance fields on the web.

screenshot of a bumpy tunnel

screenshot of merging circles

Features

  • Works with both WebGL and WebGL2

    The former is mainly needed to support older iPhones.

  • Performant even on low-end mobile devices

    Try it out yourself!

  • Comes with a number of built-in shapes and lights
  • Easily extensible with new shapes
  • Antialiasing is built in
  • Built-in quality autoscaling
  • Requires no boilerplate code
    • WebGL and its extensions are detected automatically
    • Shaders are compiled in parallel, without blocking
    • Context loss is handled with automatic restoration
    • Can be used without thinking about the GPU (although for stunning results, it should be kept in mind)

four screenshots from mobiles

Four separate screenshots taken on a mobile device

Install

npm install sdf-2d --save-dev

Use

import { compile, CircleFactory, hsl, CircleLight } from 'sdf-2d';

const main = async () => {
  const Circle = CircleFactory(hsl(30, 66, 50));
  const canvas = document.querySelector('canvas');

  const renderer = await compile(canvas, [Circle.descriptor, CircleLight.descriptor]);

  renderer.addDrawable(new Circle([200, 200], 50));
  renderer.addDrawable(new CircleLight([500, 300], [1, 0.5, 0], 0.5));
  renderer.renderDrawables();
};

main();

A commented version of the above code can be found in this repo.

Examples

For further examples, please visit the following repositories:

Building the documentation

Run npm run docs to regenerate docs/, or npm run build to build both the library and documentation. Documentation warnings fail the build, including unresolved {@link Symbol} references. Use these references for API links in source comments and documentation-readme.md so TypeDoc can validate them.

typedoc.json selects the public modules exported by src/main.ts, along with the settings, defaults, and renderer information used by their signatures. Keep these entry points aligned when adding public APIs. The module layout preserves existing API URLs while leaving internal implementation modules out of navigation.

The factory return types listed in intentionallyNotExported are private implementation types. Their constructor signatures, arguments, and examples are documented on each factory page instead of exporting the base classes. Keep that guidance up to date when changing a factory's constructor.

Documentation

For more technical details, please consult the documentation available in the repository and at sdf-2d-docs.schmelczer.dev/.

Plans

  • Automatic tile multiplier scaling
  • Non-uniform tile sizes based on scene density