Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Mar 29, 2019

mouse_to_cat

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.