tangrams / tangram

WebGL map rendering engine for creative cartography
https://tangram.city
MIT License
2.22k stars 290 forks source link

Percent-based and ratio-constrained sprite scaling #616

Closed bcamper closed 7 years ago

bcamper commented 7 years ago

This PR adds two new features for setting the size of sprites on points-based features. Both are designed to eliminate significant boilerplate and hand-calculated code for specifying sprite sizes across zooms; this has particularly come to light with the addition of many new highway shields. This work is distinct from but builds on the recently added density parameter for textures.

Percent scaling

Ratio-constrained scaling

Sprite requirement

bcamper commented 7 years ago

TODO: I noticed that negative sprite sizes in Tangram JS currently flip the sprite upside-down and anchor it from the bottom left. We should clamp negative point size values (both for explicit px values and for %) to zero.

bcamper commented 7 years ago

cc @matteblair @nvkelso

nvkelso commented 7 years ago

Nice!

A new ratio-constrained scaling syntax allows the user to specify a size for the width OR height, with the unspecified dimension automatically calculated to preserve the sprite's aspect ratio. The syntax for this is to specify size as a 2D array, with the special ?px value as one of the dimensions: size: [32px, ?px] # scale the sprite to 32px wide, with auto-calc height size: [?px, 16px] # scale the sprite to 16px high, with auto-calc width Syntax note: the ?px value is a proposal. My original plan was to use a single ? character, but this is reserved YAML syntax, as are many similar candidates.

meetar commented 7 years ago

I've seen things like auto used here:

size: [32px, auto]

matteblair commented 7 years ago

These changes make sense to me, I can't think of any issues with implementing this in Tangram ES.

I was also going to suggest auto for the auto-constraint token. * or *px is interpreted as a YAML anchor, so we can't attach our own semantics to it. An empty value as in [32px,] or [,32px] could technically work (the empty value is interpreted as null) but to me that looks a lot like a typo.

bcamper commented 7 years ago

auto makes sense to me, I forgot that we already use that for point angle. I'll update the code and description.

I also considered * (YAML reserved) and a null value (but decided against it for the reasons cited above, I think something explicit is better and less error-prone).