Fork me on GitHub

Animations

Note

Besides the animation package there are also several other R packages that can be used to help make an animation like the gifski and tweenr packages.

Here we'll give a simple example using the animation package and piecepackr's API to build an animation showing a preview of each of the piecepack configurations stored on this website.

library("animation")
library("grid")
library("piecepackr")
cfgs <- new.env()
load("../share/demo_cfgs.RData", envir=cfgs)
draw_demos <- function(cfgs) {
    for (nn in sort(names(cfgs))) {
        grid.newpage()
        text <- sprintf("grid.piece('preview_layout', cfg=%s)", nn)
        grid.text(text, y=0.94, gp=gpar(fontsize=16, fontface="bold"))
        pushViewport(viewport(y=6/14, height=inch(6), width=inch(6)))
        grid.piece("preview_layout", cfg=cfgs[[nn]])
        popViewport()
    }
}
saveGIF({draw_demos(cfgs)}, movie.name="pnp-previews.gif",
    ani.height=540, ani.width=480, ani.res=80, ani.dev="png", ani.type="png")
Animation of previews of various piecepacks

And here is a simple animation of a tic-tac-toe game:

draw_tictactoe_game_step <- function(n, cfg=list()) {
    grid.newpage()
    pushViewport(viewport(width=inch(4), height=inch(4)))
    if (n > 0) grid.piece("tile_back", cfg=cfg)
    if (n > 1) grid.piece("coin_back", suit=1, cfg=cfg, x=inch(2),y=inch(2))
    if (n > 2) grid.piece("coin_back", suit=2, cfg=cfg, x=inch(1),y=inch(2), angle=180)
    if (n > 3) grid.piece("coin_back", suit=1, cfg=cfg, x=inch(3),y=inch(1))
    if (n > 4) grid.piece("coin_back", suit=2, cfg=cfg, x=inch(1),y=inch(3), angle=180)
    if (n > 5) grid.piece("coin_back", suit=1, cfg=cfg, x=inch(1),y=inch(1))
    if (n > 6) grid.piece("coin_back", suit=2, cfg=cfg, x=inch(3),y=inch(3), angle=180)
    if (n > 7) grid.piece("coin_back", suit=1, cfg=cfg, x=inch(2),y=inch(1))
    if (n > 8) grid.segments(x0=inch(0.5), y0=inch(1), x1=inch(3.5), y1=inch(1),
                             gp=gpar(col="blue", lwd=4))
    popViewport()
}
draw_tictactoe_game <- function(cfg=list()) {
    for (ii in 0:9) { draw_tictactoe_game_step(ii, cfg) }
}
saveGIF({draw_tictactoe_game(cfgs$dual1)}, movie.name="tictactoe.gif",
    ani.height=480, ani.width=480, ani.res=120, ani.dev="png", ani.type="png")
Animation of a tic-tac-toe game

Comments


There are no comments yet.

Add a Comment

You can use Markdown or restructuredText to format your comment.

links

social