Skip to content

Instantly share code, notes, and snippets.

@djnavarro
Created February 23, 2019 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djnavarro/d2507e9847a399458f5affde48d0c872 to your computer and use it in GitHub Desktop.
Save djnavarro/d2507e9847a399458f5affde48d0c872 to your computer and use it in GitHub Desktop.
library(showtext)
library(fontr)
library(tidyverse)
library(transformr)
library(animation)
library(ggpolypath)
# use showfont to load the font
font_add_google("Allura")
# use fontr to extract letter forms
get_letter <- function(ch, xshift, yshift, id) {
char_poly <- glyph_polygon(
ch, family = "Allura",
face = "regular", nseg = 20)
char_poly$x <- (char_poly$x * 1.9) + xshift
char_poly$y <- (char_poly$y * 1.9) + yshift
char_poly$id <- id
return(char_poly)
}
# use transformr to add stars
get_shape <- function(xshift, yshift, id) {
shape_poly <- poly_star()
shape_poly$y <- ((shape_poly$y + 1) / 2.5) + yshift
shape_poly$x <- (shape_poly$x / 2.5) + xshift
shape_poly$id <- id
return(shape_poly)
}
# spacer
na_row <- tibble(x = NA, y = NA, id = NA)
# specify stars
sep <- .75
shapes <- bind_rows(
get_shape(1.5,sep,1), na_row,
get_shape(2.4,sep,2), na_row,
get_shape(3.3,sep,3), na_row,
get_shape(4.2,sep,4), na_row,
get_shape(5.1,sep,5), na_row,
get_shape(2.4,-sep,6), na_row,
get_shape(3.3,-sep,7), na_row,
get_shape(4.2,-sep,8)
)
# specify text
msg <- bind_rows(
get_letter("T", 1.2, sep, 1), na_row,
get_letter("h", 2.2, sep, 2), na_row,
get_letter("a", 3.1, sep, 3), na_row,
get_letter("n", 4.0, sep, 4), na_row,
get_letter("k", 4.8, sep, 5), na_row,
get_letter("Y", 2.2, -sep, 6), na_row,
get_letter("o", 3.2, -sep, 7), na_row,
get_letter("u", 3.9, -sep, 8)
)
nf <- 50
tweened <- tween_polygon(shapes, msg,
ease = "cubic-in-out", nframes = nf)
tweened <- bind_rows(
tweened, tweened %>% mutate(.frame = (2*nf+1)-.frame)) %>%
arrange(.frame) %>%
as_tibble()
drawframe <- function(data, f, alpha = .5) {
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, y, group=branch)) +
geom_polypath(show.legend = FALSE,
fill="black", alpha = alpha) +
coord_equal() +
ylim(-2, 3) +
theme_void()
plot(pic)
}
# save the gif using animation::saveGIF
saveGIF(
expr = {for(f in 1:(nf*2)) drawframe(tweened, f)},
movie.name = "~/../Desktop/thank_you_solid_stars.gif",
nmax = nf*2,
interval = 3/nf
)
@djnavarro
Copy link
Author

thank_you_solid_stars

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