Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tween between two lissajous curves using transformr
library(tidyverse)
library(transformr)
library(tweenr)
library(animation)
# function to generate lissajous curves in a format
# suitable for later tweening
lissajous <- function(a=1, b=1, w=1, d=pi/2, t0=-pi, t1=pi, n=100) {
tibble(
t = seq(t0, t1, length.out = n),
x = a * sin(w * t + d),
y = b * sin(t)
)
}
# two curves to tween
curve1 <- lissajous(w = .3, d = 1, t0 = 0, t1 = 20, n = 500)
curve2 <- lissajous(w = .9, d = 1, t0 = 0, t1 = 20, n = 500)
# how many frames?
nf <- 100
# tweening forward...
forward <- tween_path(
curve1, curve2,
ease = 'linear',
nframes = nf/2,
id = t
)
# tweening back...
backward <- tween_path(
curve2, curve1,
ease = 'elastic-in-out',
nframes = nf/2,
id = t) %>%
mutate(.frame = .frame + nf/2)
# put the parts together
anim <- bind_rows(forward, backward)
# plotting function for a frame
drawframe <- function(data, f) {
cat(f," ")
pic <- data %>%
filter(.frame == f) %>%
ggplot(aes(x=x, y=y)) +
geom_path(
size = 2,
show.legend = FALSE) +
coord_equal() +
xlim(-1.5,1.5) +
ylim(-1.5,1.5) +
theme_void()
plot(pic)
}
# save the gif using animation::saveGIF
saveGIF(
expr = {for(f in 1:nframes) drawframe(anim, f)},
movie.name = "~/../Desktop/anim.gif",
nmax = nframes,
interval = 3/nframes
)
@djnavarro

This comment has been minimized.

Copy link
Owner Author

commented Feb 4, 2019

anim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.