Created
September 1, 2017 13:19
-
-
Save thomasp85/fb64d3ec416a44ecdc717ca23b3bdd7a to your computer and use it in GitHub Desktop.
Animate through several closed b-splines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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