A graphics library to enable the real-time rendering of 2D signed distance fields on the web. http://schmelczer.dev/sdf-2d/
Find a file
schmelczerandras d19e7b3e9b Generate docs
2020-10-31 19:25:00 +01:00
docs Generate docs 2020-10-31 19:25:00 +01:00
media Update readme 2020-10-13 23:12:32 +02:00
src Simplify options and improve destroy 2020-10-31 19:24:22 +01:00
.eslintignore Add files 2020-09-15 10:08:16 +02:00
.eslintrc.json Add version string 2020-10-31 16:02:45 +01:00
.gitignore Remove clutter 2020-09-16 14:21:28 +02: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 Start adding documentation 2020-09-28 12:13:07 +02:00
documentation-readme.md Minor fixes 2020-10-19 12:16:20 +02:00
package.json Bump version 2020-10-31 19:24:32 +01:00
README.md Add plans 2020-10-19 11:42:02 +02:00
tsconfig.json Generate docs 2020-10-19 12:17:54 +02:00
typedoc.json Improve uniform loading 2020-09-28 17:39:07 +02:00
webpack.config.js Add version string 2020-10-31 16:02:45 +01:00

SDF-2D logo SDF-2D library

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

screenshot of a bumpy tunnel taken on version (v0.5.0)

screenshot of a merging circles taken on version (v0.5.0)

Features

  • Works with both WebGL and WebGL2

    The former is mostly required for supporting iPhones.

  • Performant even on low-end mobile devices

    Try it out yourself!

  • Has a number of built-in shapes and lights
  • Easily extensible with new shapes
  • Antialiasing is implemented
  • Has built-in quality autoscaling
  • Requires no boilerplate code
    • Automatic detection of WebGL and its extensions is provided
    • Parallel, non-blocking shader compiling
    • Context lost is handled with automatic restoration
    • Can be used without thinking of the GPU (although for stunning results it, should be kept in mind)

three screenshots from mobiles (v0.3.0)

Three 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 schmelczerandras.github.io/sdf-2d/.

Plans

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