consbio / mbgl-renderer

Static renderer for Mapbox GL
MIT License
216 stars 59 forks source link

Run-time styling options before calling render #26

Open charandas opened 5 years ago

charandas commented 5 years ago

I would love to be able to do map.setPaintProperty before this library calls map.render to give me the png output.

The motivation would be that runtime styling in GL JS actually returns proper errors like:

Could not parse color from value 'FFFFFF' TypeError: [2]: Could not parse color from value 'FFFFFF'
    at /Users/charandas/serve/asurity/RiskExec/static-maps/node_modules/mbgl-renderer/dist/render.js:486:13

Whereas if my style.json has an incorrect color like that, it just fails silently to apply the expression to say, fill-color.

Also, run-time styling would be nice for the calling-user as they won't be interfacing with style.json but instead have access to all map APIs supported by gl-native. Also, for context, runtime styling options for node seem to be mature and have been in support since at least 2017.

render(style, width, height, {
  zoom,
  center,
  token: process.env.MAPBOX_TOKEN,
  tilePath: path.resolve(__dirname, 'styles'),
  preRenderFn: (map) => map.setPaintProperty('my-layer', 'fill-color', myExpr) 
})
brendan-ward commented 5 years ago

@charandas this is an interesting idea. Definitely makes me wish we got better errors for invalid styles than we do now.

I'm open to your idea of using a preRender function - but I don't have the time at the moment to implement that. Open to PRs, if you want to rough out something along these lines.