Skip to content

Instantly share code, notes, and snippets.

@thomasp85
Created September 1, 2017 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thomasp85/fb64d3ec416a44ecdc717ca23b3bdd7a to your computer and use it in GitHub Desktop.
Save thomasp85/fb64d3ec416a44ecdc717ca23b3bdd7a to your computer and use it in GitHub Desktop.
Animate through several closed b-splines
library(ggforce) # need github version
library(tweenr)
library(animation)
# Define the different states
controls <- list(
data.frame(
x = runif(6),
y = runif(6),
col = 'steelblue',
stringsAsFactors = FALSE
),
data.frame(
x = runif(6),
y = runif(6),
col = 'firebrick',
stringsAsFactors = FALSE
),
data.frame(
x = runif(6),
y = runif(6),
col = 'forestgreen',
stringsAsFactors = FALSE
),
data.frame(
x = runif(6),
y = runif(6),
col = 'goldenrod',
stringsAsFactors = FALSE
)
)
# Repeat first state so the animation loops
controls[[5]] <- controls[[1]]
# Create intermediary states
controls <- tween_states(controls, tweenlength = 2, statelength = 0, ease = 'cubic-in-out', nframes = 300)
# Create animation
saveGIF({
for (i in seq_len(max(controls$.frame))) {
p <- ggplot(controls[controls$.frame == i,], aes(x, y)) +
geom_polygon(fill = NA, colour = 'grey', linetype = 'dashed') +
geom_point(colour = 'black') +
geom_bspline_closed(aes(fill = col)) +
scale_fill_identity() +
scale_x_continuous(limits = c(0, 1)) +
scale_y_continuous(limits = c(0, 1))
plot(p)
}
}, movie.name = 'closed_spline.gif', interval = 1/24)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment