-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordGif.R
146 lines (116 loc) · 4.05 KB
/
recordGif.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' Record a gif from an illustration
#'
#' @param rdog rdog object
#' @param file output file path for the gif file
#' @param duration seconds to record
#'
#' @export
record_gif = function(rdog,file = NULL, duration = 3){
tags = htmltools::as.tags(rdog, standalone = TRUE)
www_dir <- tempfile("viewhtml")
dir.create(www_dir)
index_html <- file.path(www_dir, "index.html")
htmltools::save_html(tags, file = index_html,
background = paste0(rdog$x$background,';margin: 0'),
libdir = "lib")
imageDir = tempfile()
dir.create(imageDir)
# frames = seq(from = 0 , to = duration, by = 1/fps)
#
# for(i in seq_along(frames)){
# a = FALSE
# while(!a){
# a = tryCatch({
# suppressMessages(
# webshot(paste0(index_html),
# file = glue::glue('{imageDir}/{i}.png'),
# selector = glue::glue('.{attributes(rdog)$id}'),
# delay = frames[i])
# )
# TRUE
# }, error = function(e){
# FALSE
# })
# }
# }
#
# images = paste0(imageDir,'/',seq_along(frames),'.png')
#
# animation = images %>% lapply(magick::image_read) %>% do.call(c,.) %>%
# magick::image_animate(fps = fps)
#
# if(!is.null(file)){
# magick::image_write(animation,file)
# }
#
# return(animation)
b <- chromote::ChromoteSession$new(width = rdog$x$width + 4, height = rdog$x$height + 4)
frames <- list()
currentFrame = environment()
cancel_save_screencast_frames <- b$Page$screencastFrame(callback_ = function(value) {
framesLocal = frames
framesLocal[[length(framesLocal) + 1]] <- value
assign('frames',framesLocal,envir = currentFrame)
})
# You'll need to provide the correct file path
b$Page$navigate(paste0('file://',index_html))
b$Page$startScreencast(format = "png", everyNthFrame = 1)
Sys.sleep(duration)
b$Page$stopScreencast()
cancel_save_screencast_frames()
lapply(seq_along(frames), function(i) {
writeBin(jsonlite::base64_dec(frames[[i]]$data), glue::glue('{imageDir}/{i}.png'))
})
# Record for 5 seconds, then stop.
# later::later(
# function() {
# b$Page$stopScreencast()
# cancel_save_screencast_frames()
#
# lapply(seq_along(frames), function(i) {
# writeBin(jsonlite::base64_dec(frames[[i]]$data), glue::glue('{imageDir}/{i}.png'))
# })
# b$close()
# message("done")
# },
# duration
# )
images = paste0(imageDir,'/',seq_along(frames)[-(1:3)],'.png')
animation = images %>%
lapply(function(x){gc();magick::image_read(x)}) %>%
do.call(c,.) %>%
magick::image_crop(glue::glue('{rdog$x$width}x{rdog$x$height}')) %>%
magick::image_animate(fps = 10)
if(!is.null(file)){
magick::image_write(animation,file)
}
gc()
return(animation)
}
#' Save an image from an illustration
#'
#' @param rdog rdog object
#' @param file output file path for the image file
#'
#' @export
save_image = function(rdog, file = NULL){
tags = htmltools::as.tags(rdog, standalone = TRUE)
www_dir <- tempfile("viewhtml")
dir.create(www_dir)
index_html <- file.path(www_dir, "index.html")
htmltools::save_html(tags, file = index_html,
background = paste0(rdog$x$background,';margin: 0'),
libdir = "lib")
# b <- chromote::ChromoteSession$new(width = rdog$x$width + 4, height = rdog$x$height + 4)
# b$Page$navigate(paste0('file://',index_html))
if(is.null(file)){
file = tempfile(fileext = '.png')
}
webshot2::webshot(index_html,file = file)
img = magick::image_read(file) %>% magick::image_crop(glue::glue('{rdog$x$width}x{rdog$x$height}'))
if(!is.null(file)){
magick::image_write(img,file)
}
gc()
return(img)
}