gen_keyframe: Generator for keyframe based tweening

View source: R/gen_keyframe.R

gen_keyframeR Documentation

Generator for keyframe based tweening

Description

This is a generator version of tween_state() and its utility functions. It returns a generator that can be used with get_frame() and get_raw_frames() to extract frames for a specific time point scaled between 0 and 1.

Usage

gen_keyframe(keyframe = NULL, pause = 0)

add_pause(.data, pause = 0)

add_keyframe(
  .data,
  keyframe,
  ease,
  length,
  id = NULL,
  enter = NULL,
  exit = NULL
)

Arguments

keyframe

A data frame to use as a keyframe state

pause

The length of the pause at the current keyframe

.data

A data.frame to start from. If .data is the result of a prior tween, only the last frame will be used for the tween. The new tween will then be added to the prior tween

ease

The easing function to use. Either a single string or one for each column in the data set.

length

The length of the transition

id

The column to match observations on. If NULL observations will be matched by position. See the Match, Enter, and Exit section for more information.

enter, exit

functions that calculate a start state for new observations that appear in to or an end state for observations that are not present in to. If NULL the new/old observations will not be part of the tween. The function gets a data.frame with either the start state of the exiting observations, or the end state of the entering observations and must return a modified version of that data.frame. See the Match, Enter, and Exit section for more information.

Value

A keyframe_generator object

See Also

Other Other generators: gen_along(), gen_at(), gen_components(), gen_events()

Examples

df1 <- data.frame(
  country = c('Denmark', 'Sweden', 'Norway'),
  population = c(5e6, 10e6, 3.5e6)
)
df2 <- data.frame(
  country = c('Denmark', 'Sweden', 'Norway', 'Finland'),
  population = c(6e6, 10.5e6, 4e6, 3e6)
)
df3 <- data.frame(
  country = c('Denmark', 'Norway'),
  population = c(10e6, 6e6)
)
to_zero <- function(x) {
  x$population <- 0
  x
}
gen <- gen_keyframe(df1, 10) %>%
  add_keyframe(df2, 'cubic-in-out', 35, id = country, enter = to_zero) %>%
  add_pause(10) %>%
  add_keyframe(df3, 'cubic-in-out', 35, id = country, enter = to_zero,
               exit = to_zero) %>%
  add_pause(10)

get_frame(gen, 0.25)

tweenr documentation built on Sept. 6, 2022, 9:05 a.m.