|
|
|
|
|
by dietrichepp
2100 days ago
|
|
There’s a simple trick you can use if you want to generate the distance field for text or simple shapes in the browser. The way you do it is by repeatedly drawing the text with strokeText, varying the stroke width and the stroke color. For example, you fill the background with 100% white, and then draw a 10px stroke at 90% gray, 9px at 80%, 8px at 70%, etc. This is, most importantly, an extremely fast way to generate a distance field for text from JavaScript in the browser. I mention this because the mystery of the getDistance() function is often one of the tricky parts of demos like these. |
|
> I mention this because the mystery of the getDistance() function is often one of the tricky parts of demos like these.
I agree! In the demos I use a library I wrote to generate 2D distance fields: https://github.com/ryankaplan/gpu-distance-field
It's also pretty fast :)