r-spatial / leafgl

R package for fast web gl rendering for leaflet
Other
263 stars 31 forks source link

screenshooting a map with leafgl layer doesnt work #59

Open trafficonese opened 3 years ago

trafficonese commented 3 years ago

I dont think this is really a leafgl error and more a bug of the underlying JS-libraries (webshot, html2canvas, FileSaver?) but taking a screenshot of a map with leafgl data doesnt display the layer in the resulting image.

I tried several approaches to take a screenshot; mapview::mapshot, shinyscreenshot::screenshot and leaflet.extras2::addEasyprint.

Here's a shiny app which tries all those methods:

library(shiny)  
library(sf)
library(leaflet)
library(leafgl)
library(leaflet.extras2)
library(shinyscreenshot)
library(mapview)

storms = st_as_sf(atlStorms2005)
cols = heat.colors(nrow(storms))

ui <- fluidPage(
  actionButton("go", "Take a screenshot with `shinyscreenshot`"),
  actionButton("mapshot", "Take a screenshot with `mapview`"),
  leafletOutput("map"))

m <- leaflet()  %>% 
  addTiles() %>%
  addEasyprint(options = easyprintOptions(exportOnly = TRUE)) %>%
  addGlPolylines(data = storms, color = cols, popup = TRUE, opacity = 1)

server <- function(input, output, session) {
  output$map <- renderLeaflet({
    m
  })
  observeEvent(input$go, {
    screenshot()
  })
  observeEvent(input$mapshot, {
    mapshot(m, file = paste0(getwd(), "/map.png"))
  })
}
shinyApp(ui, server)
tim-salabim commented 3 years ago

Did you also try https://github.com/rstudio/webshot2?

trafficonese commented 3 years ago

If we expose the option preserveDrawingBuffer and set it to TRUE the screenshot works with addEasyprint and screenshot, but somehow mapshot doesnt work.

No, I didnt try webshot2 yet.

trafficonese commented 3 years ago

Indeed, webshot2 also works, even with preserveDrawingBuffer = FALSE.

tim-salabim commented 3 years ago

That's good news I guess. I've been waiting for RStudio to push webshot2 to CRAN and rewrite mapshot accordingly which currently uses webshot. Until then, a fix using preseveDrawingBuffer seems like a good move. I'll have a look at changes needed for mapshot over the Christmas break. Thanks for bringing this up