mapbox-gl-js icon indicating copy to clipboard operation
mapbox-gl-js copied to clipboard

map.addLayer() doesn't use? "preserveDrawingBuffer" from canvas context?

Open cschlueter opened this issue 7 years ago • 6 comments

mapbox-gl-js version: 0.53.1

browser: Safari/Chrome OSX

Steps to Trigger Behavior

  1. create canvas/map (set preserveDrawingBuffer: true)
  2. create e.g. custom layer
  3. addLayer(layer)
  4. 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

cschlueter avatar Mar 25 '19 08:03 cschlueter

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).

mourner avatar Mar 25 '19 08:03 mourner

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).

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.

cschlueter avatar Mar 25 '19 08:03 cschlueter

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();
});

ansis avatar Mar 25 '19 15:03 ansis

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.

cschlueter avatar Mar 25 '19 15:03 cschlueter

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 avatar Mar 25 '19 16:03 ansis

@ansis Actually that did the trick! Thanks a lot for helping out so quickly!

cschlueter avatar Mar 26 '19 09:03 cschlueter