Skip to content

Instantly share code, notes, and snippets.

@danielredondo
Created May 13, 2020 06:40
Show Gist options
  • Save danielredondo/3f1bb376c613c57c6e90632905884699 to your computer and use it in GitHub Desktop.
Save danielredondo/3f1bb376c613c57c6e90632905884699 to your computer and use it in GitHub Desktop.
# Referencia: https://twitter.com/thomasp85/status/968483985939673089
# https://gist.github.com/thomasp85/0938c3ece34b9515d889f3b1f9c3fc9c
library(magick)
library(reshape2)
library(dplyr)
library(tidygraph)
library(particles)
library(animation)
plot_fun <- function(sim) {
df <- as_tibble(sim)
plot(df$x, df$y, col = df$color, pch = '.', axes = FALSE, xlim = c(0, 1278), ylim = c(-270, 0), xlab = NA, ylab = NA)
}
# Crear antes un PNG con el texto (GIMP, Paint, ...)
logo <- image_read("Sin título.png")
logo_frame <- melt(as.matrix(as.raster(logo)), c('y', 'x'), value.name = 'color', as.is = TRUE) %>%
filter(color != 'transparent') %>%
mutate(color = as.character(color),
y = -y,
batch = as.integer(cut(-x + rnorm(n(), 0, 10), 50)),
include = FALSE,
y_drift = rnorm(n(), 300, 70),
x_drift = rnorm(n(), 900, 90))
tbl_graph(logo_frame) %>% plot_fun
saveGIF(
tbl_graph(logo_frame) %>%
simulate(alpha_decay = 0, setup = predefined_genesis(x, y)) %>%
wield(y_force, y = y_drift, include = include, strength = 0.02) %>%
wield(x_force, x = x_drift, include = include, strength = 0.02) %>%
wield(random_force, xmin = -.1, xmax = .1, ymin = -.1, ymax = .1, include = include) %>%
evolve(120, function(sim) {
sim <- record(sim)
sim <- mutate(sim, include = batch < evolutions(sim) - 10)
plot_fun(sim)
sim
}),
movie.name = 'gif.gif',
interval = 1/24, ani.width = 1278, ani.height = 281
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment