2.0.6 • Published 1 year ago

@mapbox/tiny-sdf v2.0.6

Weekly downloads
365,577
License
BSD-2-Clause
Repository
github
Last release
1 year ago

TinySDF Build Status

TinySDF is a tiny and fast JavaScript library for generating SDF (signed distance field) from system fonts on the browser using Canvas 2D and Felzenszwalb/Huttenlocher distance transform. This is very useful for rendering text with WebGL.

Demo

Usage

Create a TinySDF for drawing glyph SDFs based on font parameters:

const tinySdf = new TinySDF({
    fontSize: 24,             // Font size in pixels
    fontFamily: 'sans-serif', // CSS font-family
    fontWeight: 'normal',     // CSS font-weight
    fontStyle: 'normal',      // CSS font-style
    buffer: 3,                // Whitespace buffer around a glyph in pixels
    radius: 8,                // How many pixels around the glyph shape to use for encoding distance
    cutoff: 0.25              // How much of the radius (relative) is used for the inside part of the glyph
});

const glyph = tinySdf.draw('泽'); // draw a single character

Returns an object with the following properties:

  • data is a Uint8ClampedArray array of alpha values (0–255) for a width x height grid.
  • width: Width of the returned bitmap.
  • height: Height of the returned bitmap.
  • glyphTop: Maximum ascent of the glyph from alphabetic baseline.
  • glyphLeft: Currently hardwired to 0 (actual glyph differences are encoded in the rasterization).
  • glyphWidth: Width of the rasterized portion of the glyph.
  • glyphHeight Height of the rasterized portion of the glyph.
  • glyphAdvance: Layout advance.

TinySDF is provided as a ES module, so it's only supported on modern browsers, excluding IE.

<script type="module">
import TinySDF from 'https://cdn.skypack.dev/@mapbox/tiny-sdf';
...
</script>

In Node, you can't use require — only import in ESM-capable versions (v12.15+):

import TinySDF from '@mapbox/tiny-sdf';

Development

npm test  # run tests
npm start # start server for the demo page

License

This implementation is licensed under the BSD 2-Clause license. It's based directly on the algorithm published in the Felzenszwalb/Huttenlocher paper, and is not a port of the existing C++ implementation provided by the paper's authors.

@simosol/mapbox-gl-js-offlinebst-map-gl@mapcreator/mapbox-glmdt-lib@datav-gi/datav-gi-js@everything-registry/sub-chunk-579@langge-data/mapbox-gl@webdeb/mapbox-gl@xbcx/mapbox-gl@tommy2gis/swsk-mapbox-gl@windycom/maplibre-gl@yongju/maplibre-glanthonyhull-mapbox-glark-mapanother-mapmagic-gl@xt-plugins/mapbox-gl@tssu/mapbox-glf-mapbox-gl@xll/mapbox-gl@3drobotics/mapbox-glcheaper-mapbox-gl-with-referrer-policy@cczbby/mapbox-gl@cczbby/maplibre-gl@bewithjonam/mapbox-gl@carto/mapbox-gl@cgcs2000/deck.gl.layers@cgcs2000/mapbox-gl@ark-org/maptrackasia-glvicapow-mapbox-glvietmap-glvietmaps-gl@plotly/mapbox-gl@porsager/mapbox-gl@pirxpilot/mapbox-gl@shitao1988/swsk-mapbox-gl@shitao1988/swsk-mapbox-gl-jsvizzuality-mapbox-gl@sakitam-gis/mapbox-glx-mapbox-glxtal-glvnmapxyd-mapbox-gl@curio/mapbox-gl@dfw810/mapbox-gl@deck.gl/layerscomments-map@datlas/mdt-lib@datav-gi/datavgi-gl-js@antv/l7-core@antv/g-adapter@antv/g-plugin-webgl-renderer@bcherny/mapbox-gl@autoxing/mapbox-glkarn-mapkarn-mapbox-gl@foxglove/three-text@foxglove/regl-worldview@emuanalytics/maplibre-gldingi-gldmap-gl@felix5/mapbox-webgl2@jingsam/mapbox-glpbf-basic-render@jhgrrewq/l7-core@infinitebrahmanuniverse/nolb-_map@jcolot/deck.gl-layersp-mapbox-gl@inelo/maplibre-glrpd-mapbox-glkt-maplibre-glmapbox-gl-encryptedmapbox-gl-epsg4490mapbox-gl-mapmagicmapbox-gl-mbtilesmapbox-gl-custom-layermapbox-gl-htmapbox-gl-js-with-shapesmapbox-gl-kyemapbox-gl-testmapbox-gl-tdtsdmapbox-gl-tultmapbox-with-shapesmap-core-funcmapmagic-glmaplibre-glmaplibre-gl-ashmaplibre-gl-reduce-functionalitiesmaplibre-gl-tdtsdmapsi-glmapbox-glmapbox-gl--custom-stretchmapbox-gl-ahtdeck.gl-district@geops/maplibre-gl@geomatico/mapbox-gl@globalfishingwatch/mapbox-gl@globalfishingwatch/maplibre-glrif-mapbox-gl@hansdo/maplibre-gl
2.0.6

1 year ago

2.0.5

2 years ago

2.0.3

3 years ago

2.0.4

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

5 years ago

1.1.0

7 years ago

1.0.2

7 years ago