Gathering detailed insights and metrics for webgl-sdf-generator
Gathering detailed insights and metrics for webgl-sdf-generator
Gathering detailed insights and metrics for webgl-sdf-generator
Gathering detailed insights and metrics for webgl-sdf-generator
A signed distance field (SDF) image generator for 2D paths such as font glyphs, accelerated using WebGL.
npm install webgl-sdf-generator
99.8
Supply Chain
99.6
Quality
75.3
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
34 Stars
15 Commits
1 Forks
4 Watching
2 Branches
1 Contributors
Updated on 30 Jul 2024
JavaScript (92.85%)
GLSL (7.15%)
Cumulative downloads
Total Downloads
Last day
-5.1%
45,521
Compared to previous day
Last week
2.2%
254,856
Compared to previous week
Last month
6.3%
1,064,521
Compared to previous month
Last year
92.1%
9,455,425
Compared to previous year
This is a signed distance field (SDF) image generator for 2D paths such as font glyphs, for use in Web environments. It utilizes WebGL when possible for GPU-accelerated SDF generation.
1npm install webgl-sdf-generator
1import initSDFGenerator from 'webgl-sdf-generator' 2// or: const initSDFGenerator = require('webgl-sdf-generator') 3 4const generator = initSDFGenerator()
The webgl-sdf-generator
package's only export is a factory function which you must invoke to return an object which holds various methods for performing the generation.
Why a factory function? The main reason is to ensure the entire module's code is wrapped within a single self-contained function with no closure dependencies. This enables that function to be stringified and passed into a web worker, for example.
Note that each factory call will result in its own internal WebGL context, which may be useful in some rare cases, but usually you'll just want to call it once and share that single generator object.
1const sdfImageData = generator.generate( 2 64, // width 3 64, // height 4 'M0,0L50,25L25,50Z', // path 5 [-5, -5, 55, 55], // viewBox 6 25, // maxDistance 7 1 // exponent 8)
Let's break down those arguments...
width/height
- The dimensions of the resulting image.
path
- An SVG-like path string. Only the following path commands are currently supported: M
, L
, Q
, C
, and Z
.
viewBox
- The rectangle in the path
's coordinate system that will be covered by the output image. Specified as an array of [left, top, right, bottom]
. You'll want to account for padding around the path shape in this rectangle.
maxDistance
- The maximum distance that will be encoded in the distance field; this is the distance from the path's edge at which the SDF value will be 0
or 255
.
exponent
- An optional exponent to apply to the SDF distance values as they get farther from the path's edge. This can be useful when maxDistance
is large, to allow more precision near the path edge where it's more important and decreasing precision far away (visualized here). Whatever uses the SDF later on will need to invert the transformation to get useful distance values. Defaults to 1
for no curve.
The return value is a Uint8Array
of SDF image pixel values (single channel), where 127.5 is the "zero distance" aligning with path edges. Values below that are outside the path and values above it are inside the path.
When you call generator.generate(...)
, it will first attempt to build the SDF using WebGL; this is super fast because it is GPU-acclerated. This should work in most browsers, but if for whatever reason the proper WebGL support is not available or fails due to context loss then it will fall back to a slower JavaScript-based implementation.
If you want more control over this fallback behavior, you can access the individual implementations directly:
1// Same arguments as the main generate(): 2const resultFromGL = generator.webgl.generate(...args) 3const resultFromJS = generator.javascript.generate(...args)
The WebGL implementation also provides method to detect support if you want to test it beforehand:
1const webglSupported = generator.webgl.isSupported()
Making a single SDF has its uses, but it's likely that you actually want that SDF to be added to a larger "atlas" image of many SDFs. While you could do that by calling generator.generate()
as above and manually inserting the returned Uint8Array
into a larger texture, that isn't optimal because it involves reading pixels back from the GPU for every SDF, which has a performance impact.
Instead, you can generate an SDF directly into a region+channel of a WebGL-enabled canvas
, populating your "atlas" directly on the GPU. Only the region/channel for the new SDF will be overwritten, the rest will be preserved. That canvas
can then be used as the source for a WebGL texture for rendering. This ends up being much faster since the data all stays on the GPU.
1generator.generateIntoCanvas( 2 64, // width 3 64, // height 4 'M0,0L50,25L25,50Z', // path 5 [-5, -5, 55, 55], // viewBox 6 25, // maxDistance 7 1, // exponent 8 yourCanvasElement, // output canvas 9 128, // output x coordinate 10 64, // output y coordinate 11 0 // output color channel 12)
And similarly for the individual implementations:
1// Same arguments as the main generate():
2generator.webgl.generateIntoCanvas(...args)
3generator.javascript.generateIntoCanvas(...args)
Note that the canvas you pass:
getContext('webgl')
will succeed for it; you can't give it a canvas with a 2d
context, for example.generateIntoCanvas
only sets the exact state it needs and makes no attempt to restore previous state, so sharing it could lead to unpredictable results.No vulnerabilities found.
No security vulnerabilities found.