Skip to content

Instantly share code, notes, and snippets.

@georgemsavva
Last active April 4, 2022 22:23
Show Gist options
  • Save georgemsavva/f2460545cc4ed0c1965e6eb62aa17050 to your computer and use it in GitHub Desktop.
Save georgemsavva/f2460545cc4ed0c1965e6eb62aa17050 to your computer and use it in GitHub Desktop.
code as art work in progress
# For the point.in.poly function
library(sp)
# For 'createPolygons'
library(gglogo)
# To tween polygons
library(transformr)
# To make a gif!
library(gifski)
# I'm running the code like this because I want to captue the whole call during the execution.
(function(m){
# Need to do this to make R see my fonts.
windowsFonts(Times=windowsFont("Times New Roman"))
# This is the string that is used for the outlines.
# These unicodes correspond to the playing card characters.
# strsplit to get a vector of individual characters.
msg=strsplit("\u2665\u2660\u2663\u2666","")[[1]]
NF=24 # number of frames per transition
N=55 # grid with
ease='elastic-in-out' # which easing function to use
# Make a list of polygons for each character (with the first one repeated) at the end.
polys=lapply(c(msg,msg[1]),createPolygons,font="Times")
# Now make the transitions as one big dataframe
ds=do.call(rbind,lapply(1:length(msg),function(a){
d=tween_polygon(polys[[a]], polys[[a+1]], ease=ease, nframes=NF)
d$.frame<-d$.frame+NF*(a-1) # Make the frame numbers for each transition
d}))
# Split into individual frames
frames=split(ds,factor(ds[,".frame"]))
# Now get the code for the current call (the text to fill the shape with)
str=strsplit(paste(deparse(sys.call()), collapse=" "),"")[[1]];
# Now we create a list of plots, save_gif turns them into a gif.
save_gif(lapply(1:(NF*length(msg)),function(b){
polyF=frames[[b]] # This is the polgon of the current frame
d=expand.grid(x=seq(0,1,l=N),y=seq(1,0,l=N)) # Make a big grid of points
d$pip=point.in.polygon(d$x,d$y,polyF$x, polyF$y) # Check if each is in the polygon
d=d[d$pip==1,] # Only keep the points in the polygon
# Now with only the points inside the polygon, add the text to the data frame.
d$str=rep(c(str,msg),2)[1:(dim(d)[1])]
# Set some plot params
par(bg="black",mar=rep(3,4))
# Make an empty plot
plot(NA,axes=F,ann=F,xlim=c(0,1),ylim=c(0,1))
# Add the text!
text(d$x,d$y,labels = d$str,col="white",family="mono")
}),w=600,h=600,d=1/24) # finish save_gif with the resolution and the frame rate.
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment