map.addLayer() doesn't use? "preserveDrawingBuffer" from canvas context?
mapbox-gl-js version: 0.53.1
browser: Safari/Chrome OSX
Steps to Trigger Behavior
- create canvas/map (set preserveDrawingBuffer: true)
- create e.g. custom layer
- addLayer(layer)
- map.getCanvas().toDataURL()
It does include the map! as expected, but it doesn't include the custom layer (I took the triangle example from the documentation)
Link to Demonstration
https://jsbin.com/lulequmiwo
Expected Behavior
- toDataURL should include all Layers
- weirdly it seems that a "data" layer I did put into a custom style is also not showing up in the toDataURL, on Mapbox Studio it is shown, why are all "regular" layers in the image but not the once added by myself?
Actual Behavior
- toDataURL should include all customer Layer renderings
Can you set up a minimal reproducible test case that demonstrates this? Maybe it has to do with the timing of toDataURL call (e.g. you're calling it before the layer finishes drawing).
Can you set up a minimal reproducible test case that demonstrates this? Maybe it has to do with the timing of
toDataURLcall (e.g. you're calling it before the layer finishes drawing).
I tried all sort of timing things, don't think it's a timing issue, but to be honest I'm out of ideas. See jsbin example: Once you click on download it renders the PNG without the custom layer.
When changes are made to the map (new center, adding a layer, etc) they aren't rendered immediately. All the changes are batched together and rendered when the browser decides to render a new frame.
In the example the image is saved immediately after adding a layer, before the map has had a chance to render. If you wait for the map to render the image includes the custom layer:
map.once('render', () => {
document.getElementById("download").href = map.getCanvas().toDataURL();
});
When changes are made to the map (new center, adding a layer, etc) they aren't rendered immediately. All the changes are batched together and rendered when the browser decides to render a new frame.
In the example the image is saved immediately after adding a layer, before the map has had a chance to render. If you wait for the map to render the image includes the custom layer:
map.once('render', () => { document.getElementById("download").href = map.getCanvas().toDataURL(); });
Can you provide a jsbin example for the solution? I can't seem to get it to work.
https://jsbin.com/heribumapu/1/edit?html,output
It looks like in this case the labels are still fading in. Switching from the render event to the idle event should fix this.
@ansis Actually that did the trick! Thanks a lot for helping out so quickly!