12 Oct 2011

Yet Another One.. Animation with saveHTML / saveVideo from Package ANIMATION



...some more playing with saveHTML, as.raster() and rasterImage(), producing a "flickering screen":


# load packages:
require(ReadImages)
require(animation)

img <- as.raster(matrix(sample(0:1, 1e+06, replace = T), ncol=1000, nrow=1000))

# view image:
op <- par(); dev.off()
par(bg = "gray", mar = c(rep(2, 4)), xaxs = "i", yaxs = "i")
plot(c(0, 1), c(0, 1), type = "n", xlab="", ylab="")
rasterImage(img, 0, 0, 1, 1, interpolate = T)

# make the animation:
saveHTML({
    par(bg = "gray", mar = c(rep(2, 4)),
        xaxs = "i", yaxs = "i")
    ani.options(interval = 0.2)
    for (i in 1:20) {
            img <- as.raster(matrix(sample(0:1, 1e+06, replace = T),
                             ncol=1000, nrow=1000))
            plot(c(0, 1), c(0, 1), type = "n", xlab="", ylab="")
            rasterImage(img, 0, 0, 1, 1, interpolate = T)
            text(.5, .5, "R", col = "white", cex = 25)
            ani.pause()
            }
         },
          img.name = "flicker",
          autoplay = T,
          outdir = getwd(),
          htmlfile = "flicker.html",
          ani.height = 300, ani.width = 300,
          title = "Flickering Screen",
          description = c("none"))

par(op); dev.off()


# something similar with saveVideo():

library(animation)

setwd("C:/Users/Kay/Documents/R.Examples")
dir.create("~/Animation")
setwd("C:/Users/Kay/Documents/R.Examples/Animation")

saveMovie({
    par(bg = "gray", mar = c(rep(0, 4)),
        xaxs = "i", yaxs = "i", bty = "n")
    ani.options(interval = 0.2)
    for (i in 1:10) {
            img <- as.raster(matrix(sample(0:1, 1e+06, replace = T),
                             ncol=1000, nrow=1000))
            plot(c(0, 1), c(0, 1), type = "n", xlab="", ylab="")
            rasterImage(img, 0, 0, 1, 1, interpolate = T)
            text(.5, .5, "R", col = "white", cex = 25)
            ani.pause()
            }
         },
          outdir = getwd(),
          movie.name = "flicker.gif",
          ani.height = 300, ani.width = 300,
          title = "Flickering Screen")
To cite package ‘ReadImages’ in publications use:

  Markus Loecher (2009). ReadImages: Image Reading Module for R. R
  package version 0.1.3.1. http://CRAN.R-project.org/package=ReadImages

To cite package ‘animation’ in publications use:

  Yihui Xie (2011). animation: A Gallery of Animations in Statistics
  and Utilities to Create Animations. R package version 2.0-5.
  http://CRAN.R-project.org/package=animation

No comments :

Post a Comment