schmelczer/sdf-2d

A graphics library to enable the real-time rendering of 2D signed distance fields on the web. Mirror of https://git.schmelczer.dev/andras/sdf-2d

TypeScript

117

124 commits

updated Sep 19, 2026

See the code

README

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:

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
2d
circle-tracing
glsl
graphics-library
raytracing
sdf-2d
typescript
webgl
webgl2
webpack

Contributors

schmelczer

124 commits

schmelczer/sdf-2d

A graphics library to enable the real-time rendering of 2D signed distance fields on the web. Mirror of https://git.schmelczer.dev/andras/sdf-2d

TypeScript

117

124 commits

updated Sep 19, 2026

See the code

README

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:

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
2d
circle-tracing
glsl
graphics-library
raytracing
sdf-2d
typescript
webgl
webgl2
webpack

Contributors

schmelczer

124 commits

Languages

TypeScript

86.2%

GLSL

11.5%

JavaScript

1.7%