wch / webshot

Take screenshots of web pages from R
http://wch.github.io/webshot/
228 stars 40 forks source link

Animated canvas is not rendered in the image #86

Open oganm opened 5 years ago

oganm commented 5 years ago

I have a small html file that creates a simple animation on canvas using a javascript library. However all I see with webshot is the empty canvas with the background color. I am unsure if webshot is supposed to capture animated frames but couldn't find a "known issues" section to see if this is within scope.

To replicate, download this file and do

webshot::webshot('hede.html')

After changing the file extension

Had to change the extension to paste the file to github. If you open it with a browser as an html file, you'll see the animation working

oganm commented 5 years ago

To make it simpler this file is an inanimate version, which also does not work.

oganm commented 5 years ago

The root of the issue seems to be phantomjs itself. Trying to see if any configuration of phantomjs can make this work

wch commented 5 years ago

This can be done with Chromote, using the screencastFrame command:

library(chromote)
b <- ChromoteSession$new(width = 240, height = 240)
frames <- list()
cancel_save_screencast_frames <- b$Page$screencastFrame(callback_ = function(value) {
  frames[[length(frames) + 1]] <<- value
  cat(".")
})

# You'll need to provide the correct file path
b$Page$navigate("file:///path/to/hede.html")

b$Page$startScreencast(format = "png", everyNthFrame = 1)

# Record for 5 seconds, then stop.
later(
  function() {
    b$Page$stopScreencast()
    cancel_save_screencast_frames()

    lapply(seq_along(frames), function(i) {
      writeBin(jsonlite::base64_dec(frames[[i]]$data), sprintf("frame%02d.png", i))  
    })
    b$close()
    message("done")
  },
  5
)
oganm commented 5 years ago

Oh this is the elsewhere you were talking about. Thanks again.