#' Create an animated gif from a list of stimuli
#'
#' @param stimuli list of class stimlist
#' @param fps frames per second
#' @param loop how many times to loop the animation (0 = infinite)
#' @param rev whether to loop back and forth (TRUE) or in one direction (FALSE)
#' @param filename an optional filename for saving
#'
#' @return magick image
#' @export
#'
#' @examples
#' demo_stim() %>% animate()
animate <- function(stimuli, fps = 1, loop = 0, rev = FALSE, filename = NULL) {
if (length(stimuli) < 2) {
stop("You need at least two images in the list to make an animated gif")
}
# if looping back and forth
if (isTRUE(rev)) stimuli <- c(stimuli, rev(stimuli))
img <- stimuli[[1]]$img
for (i in 2:length(stimuli)) {
img <- c(img, stimuli[[i]]$img)
}
x <- magick::image_animate(
img,
fps = fps,
dispose = "previous",
loop = loop,
optimize = TRUE)
if (!is.null(filename)) {
magick::image_write(x, path = filename)
}
x
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.