npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tile-labeler

v0.8.1

Published

Convert map label features into WebGL buffers for rendering

Downloads

118

Readme

tile-labeler

Convert map label features into WebGL buffers for rendering

Feature layout is guided by a MapLibre style document, as parsed by tile-stencil. The returned buffers are consistent with the format returned by tile-mixer.

See the simple test plotting computed character positions over a pre-rendered tile.

tests

Installation

tile-labeler is provided as an ESM import

import * as tileLabeler from 'tile-labeler';

tileLabeler exposes two methods:

  • initAtlasGetter
  • initShaping

initAtlasGetter

Returns a function that collects font names and character codes for each label, and constructs an atlas of glyph SDFs (via sdf-manager).

Syntax

const getAtlas = tileLabeler.initAtlasGetter(params);

Parameters

The supplied parameters object has the following properties:

  • parsedStyles (Array): An array of MapLibre style layers, with style functions already parsed by tile-stencil
  • glyphEndpoint (String): The URL template from the glyphs property of a MapLibre style document. Any Mapbox-specific domains or API keys must have been already expanded by tile-stencil

API

The returned atlas getter function inputs tile data, and returns a Promise that resolves to an atlas of glyph SDFs. For example,

getAtlas(layers, zoom).then(atlas => {
  // Use atlas to shape label features and construct WebGL buffers
});

The parameters for the getAtlas function are:

  • layers (Object): A dictionary of tile layers, keyed on the .id property of the style for that layer. (NOTE: this is probably NOT the natural order of the tile data—it should be pre-filtered. See filter-source.js in tile-mixer.) Each layer should have a .features property pointing to an array of GeoJSON features.
  • zoom (Number): The zoom level at which style layout functions will be evaluated

The data returned from the labeler function includes:

  • { width, height, data } = atlas: The signed distance field (SDF) data to be loaded as an Alpha texture. data is a Uint16Array

NOTE also that any label features in layer.features will have the following properties added (MODIFIED IN PLACE):

  • feature.font (String): The name of the font used to render this label
  • feature.charCodes (Array): The character codes of the characters in the label text

initShaping

Returns utilities to construct WebGL buffers for valid label features.

The syntax is as follows:

const { serialize, getLength, styleKeys } = tileLabeler
  .initShaping(style, spriteData);

where the arguments are:

There are three named returns: { serialize, getLength, styleKeys}

serialize

The serialize method can serialize a feature, using the following signature:

const buffers = serialize(feature, zoom, atlas, tree);

where the arguments are as follows:

  • feature: A GeoJSON feature, to which the properties .font and .charCodes have been added by a previous call to a getAtlas function (see above under initAtlasGetter).
  • zoom: The map zoom level at which this label will be displayed
  • atlas: An atlas of glyph SDFs, as generated by the getAtlas call
  • tree: An rbush object used for checking collisions with previous labels. The user is responsible for initializing this object, and supplying layers in the correct order.

getLength

The getLength method inputs the buffers returned by serialize, and returns the number of instances that must be rendered to display the serialized feature.

const numInstances = getLength(buffers);

styleKeys

The styleKeys property is an Array of Strings, each representing the key of a symbol style layer property. The values of these properties, if they are specified as feature property functions, will need to be loaded into buffers for rendering. tile-labeler does not construct these buffers, but .getLength and .styleKeys provide the information needed to construct them. See tile-gl code for an example of how to do this.