2.0.6 • Published 2 years ago

@mapbox/tiny-sdf v2.0.6

Weekly downloads
365,577
License
BSD-2-Clause
Repository
github
Last release
2 years 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-gltest-map-azsftwrx-mapbox-glstarry-glswsk-mapboxglthinknetmapsvnmapvizzuality-mapbox-glvicapow-mapbox-glvietmap-glvietmaps-gl@curio/mapbox-glkarn-mapkarn-mapbox-glkt-maplibre-gl@openmapvn/maplibre-gltiny-atlastrackasia-gl@antv/g-adapter@americana/maplibre-gl@antv/g-plugin-webgl-renderer@antv/l7-core@ark-org/map@autoxing/mapbox-gl@alantgeo/mapbox-gl@bcherny/mapbox-gl@bewithjonam/mapbox-gl@carto/mapbox-gl@cczbby/mapbox-gl@cczbby/maplibre-gl@cgcs2000/mapbox-gl@cgcs2000/deck.gl.layers@3drobotics/mapbox-gl@0xfa11/maplibre-gl@deck.gl/layers@dfw810/mapbox-gl@datlas/mdt-lib@datav-gi/datavgi-gl-jsxyd-mapbox-glxtal-gl@felix5/mapbox-webgl2@gvol-org/mapbox-gl@hansdo/maplibre-gl@goongmaps/goong-js@foxglove/three-text@foxglove/regl-worldview@geops/maplibre-gl@geomatico/mapbox-gl@globalfishingwatch/mapbox-gl@globalfishingwatch/maplibre-gl@infinitebrahmanuniverse/nolb-_map@krisdages/mapbox-gl@koordinates/maplibre-gl@lichtblick/three-text@luqh_mapbox/mapbox-gl@maps-gl/maps-gl@maptalks/vector-packer@mapcat/mapbox-gl@mapgis/mapbox-gl@emuanalytics/maplibre-gl@jingsam/mapbox-gl@jhgrrewq/l7-core@jcolot/deck.gl-layers@inelo/maplibre-gl@kantas/vector-js@ogix/mapbox-gl@rczobor/maplibre-gl@plotly/mapbox-gl@sakitam-gis/mapbox-gl@openmapvn/openmapvn-gl@onthegomap/mapbox-gl@porsager/mapbox-gl@pirxpilot/mapbox-gl@skywise-app/maplibre-gl@shitao1988/swsk-mapbox-gl@shitao1988/swsk-mapbox-gl-jsp-mapbox-glpbf-basic-renderrpd-mapbox-glregl-worldviewpetr-maplibre-glrif-mapbox-gllg-mapbox-glmap-core-funcmapbox-glmapbox-gl--custom-stretchmapbox-gl-ahtmapbox-gl-custom-layermapbox-gl-tdtsdmapbox-gl-testmapbox-gl-tultmapbox-gl_wstdmapbox-gl-encryptedmapbox-gl-epsg4490
2.0.6

2 years ago

2.0.5

3 years ago

2.0.3

4 years ago

2.0.4

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.1

6 years ago

1.1.0

8 years ago

1.0.2

8 years ago