-
Notifications
You must be signed in to change notification settings - Fork 2
/
make_lotto.R
110 lines (86 loc) · 3.1 KB
/
make_lotto.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
library("magrittr")
# needed later
fs::dir_create("balloons")
fs::dir_create("tags")
fs::dir_create("frames")
# Obviously this has to be the random seed
set.seed(31)
########### WINNERS
winners <- rtweet::get_followers("lockedata") %>%
dplyr::sample_n(size = 31)%>%
.$user_id %>%
rtweet::lookup_users()
winners <- glue::glue("@{winners$screen_name}")
create_balloon <- function(name){
magick::image_read("assets/Balloon.png") %>%
magick::image_annotate(name,
location = "+870+1900",
size = 70,
font = "roboto") %>%
magick::image_write(glue::glue("balloons/Balloon_{name}.png"))
magick::image_read("assets/Label.png") %>%
magick::image_annotate(name,
location = "+870+1900",
size = 70,
font = "roboto") %>%
magick::image_write(glue::glue("tags/Label_{name}.png"))
}
purrr::walk(winners, create_balloon)
########### VIZ
# initial state
data1 <- data.frame(
x = runif(min = -20, max = 20, n = 31),
y = runif(min = -50, max = -10, n = 31),
stringsAsFactors = FALSE
)
# then all balloons on a horizontal line at the top
data2 <- data1
data2$y <- 150
data2$x <- seq(-100, 100, length = 31)
# transition between the two
data <- tweenr::tween_states(list(data1, data2), 3, 1, 'linear', 31)
# then we sprinkle the tags
data3 <- data1
data3$x <- rep(seq(-40, 100, length = 5), 7)[1:31]
data3$y <- rep(seq(50, 150, length = 7), each = 5)[31:1]
data4 <- tweenr::tween_states(list(data2, data3), 3, 2, 'linear', 10)
data$image <- rep(glue::glue("balloons/Balloon_{winners}.png"), nrow(data)/31)
data4$image <- rep(glue::glue("tags/Label_{winners}.png"), nrow(data4)/31)
data4$.frame <- data4$.frame + max(data$.frame)
data <- rbind(data, data4)
data$name <- rep(winners, nrow(data)/31)
#
library("ggplot2")
library("ggimage")
chibi <- magick::image_read("assets/chibi_happy_steph.png") %>%
magick::image_resize("500x500")
box <- magick::image_read("assets/Box.png") %>%
magick::image_resize("1500x1500")
plot_one_step <- function(step, data, chibi = chibi,
box = box){
data <- data[data$.frame == step,]
p <- ggplot(data) +
geom_image(aes(x, y, image = image),
size = 0.5) +
ylim(c(-50, 170)) +
xlim(c(-110, 110)) +
theme_void()
outfil <- paste0("frames/frame_", stringr::str_pad(step, 2, pad = "0"), ".png")
ggsave(outfil, p, width=5, height=5)
magick::image_read(outfil) %>%
magick::image_composite(chibi,
offset = "+100+900") %>%
magick::image_composite(box,
offset = "+0+600") %>%
magick::image_crop("1500x1200+0+300")%>%
magick::image_write(outfil)
}
purrr::walk(1:(nrow(data)/31), plot_one_step, data, chibi = chibi,
box = box)
paste0("The winners are ", toString(winners))
frames <- fs::dir_ls("frames")
gifski::gifski(c(frames, rep(frames[length(frames)], 15)),
gif_file = "thirtyone.gif",
delay = 0.5,
width = 1500,
height = 1200)