animate_piece: Animate board game pieces

View source: R/animate_piece.R

animate_pieceR Documentation

Animate board game pieces

Description

animate_piece() animates board game pieces.

Usage

animate_piece(
  dfs,
  file = "animation.gif",
  ...,
  annotate = TRUE,
  .f = piecepackr::grid.piece,
  cfg = getOption("piecepackr.cfg", NULL),
  envir = getOption("piecepackr.envir", game_systems("sans")),
  n_transitions = 0L,
  n_pauses = 1L,
  fps = n_transitions + n_pauses,
  width = NULL,
  height = NULL,
  ppi = NULL,
  new_device = TRUE,
  annotation_scale = NULL
)

Arguments

dfs

A list of data frames of game data to plot.

file

Filename to save animation unless NULL in which case it uses the current graphics device.

...

Arguments to pmap_piece

annotate

If TRUE or "algebraic" annotate the plot with “algrebraic” coordinates, if FALSE or "none" don't annotate, if "cartesian" annotate the plot with “cartesian” coordinates.

.f

Low level graphics function to use e.g. grid.piece(), piece3d(), piece(), or piece_mesh().

cfg

A piecepackr configuration list

envir

Environment (or named list) of piecepackr configuration lists

n_transitions

Integer, if over zero (the default) how many transition frames to add between moves.

n_pauses

Integer, how many paused frames per completed move.

fps

Double, frames per second.

width

Width of animation (in inches). Inferred by default.

height

Height of animation (in inches). Inferred by default.

ppi

Resolution of animation in pixels per inch. By default set so image max 600 pixels wide or tall.

new_device

If file is NULL should we open up a new graphics device?

annotation_scale

Multiplicative factor that scales (stretches) any annotation coordinates. By default uses attr(df, "scale_factor") %||% 1.

Value

Nothing, as a side effect creates an animation.

Examples

  # Basic tic-tac-toe animation
  dfs <- list()
  d.frame <- function(piece_side = "bit_back", ..., rank = 1L) {
                 data.frame(piece_side = piece_side, ..., rank = rank,
                            cfg = "checkers1", stringsAsFactors = FALSE)
  }
  df <- d.frame("board_back", suit = 2L, rank = 3L, x = 2, y = 2, id = "1")
  dfs[[1L]] <- df
  df <- rbind(df, d.frame(suit = 1L, x = 2, y = 2, id = "2"))
  dfs[[2L]] <- df
  df <- rbind(df, d.frame(suit = 2L, x = 1, y = 2, id = "3"))
  dfs[[3L]] <- df
  df <- rbind(df, d.frame(suit = 1L, x = 3, y = 1, id = "4"))
  dfs[[4L]] <- df
  df <- rbind(df, d.frame(suit = 2L, x = 1, y = 3, id = "5"))
  dfs[[5L]] <- df
  df <- rbind(df, d.frame(suit = 1L, x = 1, y = 1, id = "6"))
  dfs[[6L]] <- df
  df <- rbind(df, d.frame(suit = 2L, x = 3, y = 3, id = "7"))
  dfs[[7L]] <- df
  df <- rbind(df, d.frame(suit = 1L, x = 2, y = 1, id = "8"))
  dfs[[8L]] <- df

  ## Press enter to walk through moves in a "game" in new graphics device
  if (interactive()) {
      animate_piece(dfs, file = NULL)
  }

  ## Save GIF of game with animation transitions
  ## Not run: # May take more than 5 seconds on CRAN servers
  if ((requireNamespace("animation", quietly = TRUE) ||
       requireNamespace("gifski", quietly = TRUE)) &&
      requireNamespace("tweenr", quietly = TRUE)) {
      file <- tempfile("tic-tac-toe", fileext = ".gif")
      animate_piece(dfs, file = file,
                    n_transitions = 5L, n_pauses = 2L, fps = 9)
  }
  
## End(Not run)


piecepackr documentation built on Sept. 11, 2024, 9:09 p.m.