Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created March 29, 2019 06:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djnavarro/81c59b27987031b76b2b84f9bfdec841 to your computer and use it in GitHub Desktop.
Save djnavarro/81c59b27987031b76b2b84f9bfdec841 to your computer and use it in GitHub Desktop.
Transform mouse emoji into cat emoji
library(tidyverse)
library(fontr)
library(emojifont)
library(ggpolypath)
library(transformr)
library(animation)
library(Cairo)
# create the base images from cat emoji and mouse emoji!
cat <- glyph_polygon(ch = emoji("cat"), family = "EmojiOne")
mouse <- glyph_polygon(ch = emoji("mouse"), family = "EmojiOne")
# function to stitch two tweens together
bind_tweens <- function(forward, backward) {
nframes <- max(forward$.frame)
backward <- backward %>% mutate(.frame = .frame + nframes)
anim <- bind_rows(forward, backward)
return(anim)
}
# function to tween from one image to another then back
loop <- function(from, to, by, ...) {
forward <- by(from, to, ...)
backward <- by(to, from, ...)
return(bind_tweens(forward, backward))
}
# function to draw a frame
drawframe <- function(data, f) {
df <- data %>%
filter(.frame == f)
df$branch <- is.na(df$x)
df$branch <- cumsum(df$branch) + 1
pic <- df %>%
filter(!is.na(x)) %>%
ggplot(aes(x=x, y=y, group=branch)) +
geom_polypath(show.legend = FALSE,
fill="black", alpha = .5) +
xlim(-.5,1) +
ylim(-.5,1) +
coord_equal() +
theme_void()
plot(pic)
}
# construct the anumation
anim <- loop(mouse, cat, tween_path,
ease = "cubic-in-out", nframes = 50)
# save it
saveGIF(
expr = {
for(f in 1:100) drawframe(anim, f)
},
movie.name = "~/../Desktop/mouse_to_cat.gif",
interval = .05,
ani.dev = "CairoPNG"
)
@djnavarro
Copy link
Author

mouse_to_cat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment