#' create a grid of points
#'
#' @param x A vector for width
#' @param y A vector for Height
#' @importFrom ambient long_grid
setup_grid <- function(
x,
y
){
ambient::long_grid(
x,
y
) %>%
data.frame()
}
#' Create spacing for the line of words using Manhattan dist
#'
#' @param line row of dots for a line
#' @param max_words maximum number of words
line_words_manhattan <- function(
line,
max_words,
seed
){
# I'd like to replace this with a function that's
# based on a probability of length in words
num_words <- sample(1:max_words, 1)
# get the number of points for the line
n_points <- length(line)-num_words
# Randomly generate word lengths
len_words <- sample(0:100, size = num_words)/100
total <- sum(len_words)
#normalize word lengths
len_words_norm <- len_words*(n_points/total)
# Add with remainder
rem <- 0
total <- 0
for (i in 1:num_words) {
new_rem <- len_words_norm[i]+rem %% 1
len_words_norm[i] <- total+round(len_words_norm[i]+rem)
total <- len_words_norm[i]
rem <- new_rem-round(new_rem)
}
return(len_words_norm)
}
#' Generate Asemic Text
#' @param lines The number of lines of text to generate
#' @param chars Number of chars per line
#' @param seed The random number seed
gen_text <- function(
lines = 10,
chars = 30,
granularity = .1,
seed = 42,
height_lim = .5,
width_lim = .5,
use_spaces = TRUE
){
grid <- setup_grid(seq(1,chars, granularity), 1:lines)
grid$group <- grid$y
num_rows <- nrow(grid)
# Create noise for lines
height_upper <- height_lim
set.seed(seed)
up_or_down <- sample(c(-1, 0, 1), num_rows, replace = TRUE)
set.seed(seed)
change_h <- runif(num_rows, -1* height_lim, height_lim)
set.seed(seed)
change_w <- runif(num_rows, -1* width_lim, width_lim )
grid$y2 <- grid$y + change_h
grid$x2 <- grid$x + change_w
prob_sapce = .75*granularity
if(use_spaces==TRUE){
set.seed(seed)
spaces <- sample(
c(TRUE, FALSE),
num_rows,
TRUE,
c(1-prob_sapce,prob_sapce
)
)
print("Adding spaces")
for (i in chars/2) {
spaces <- ifelse(lag(spaces), spaces, FALSE)
spaces <- ifelse(lead(spaces), spaces, FALSE)
}
grid$x2 <- ifelse(spaces, grid$x2, NA)
}
return(grid)
}
library(tidyverse)
data_init <- gen_text(lines = 70)
data_init$x_noise1 <- ambient::gen_perlin(data_init$x2, .01, seed = 42)
data_init$x_noise2 <- ambient::gen_perlin(data_init$x2, .005, seed = 42)
data_init$x_noise3 <- ambient::gen_perlin(data_init$x2, .0025, seed = 42)
data_init$x_noise4 <- ambient::gen_perlin(data_init$x2, .00125, seed = 42)
ggplot(data_init)+
geom_path(aes(x2+x_noise4,y2, group = group), alpha =0.003125, size= 4 )+
geom_path(aes(x2+x_noise3,y2, group = group), alpha =0.00625, size= 3 )+
geom_path(aes(x2+x_noise2,y2, group = group), alpha =.0125, size= 2.5)+
geom_path(aes(x2+x_noise1,y2, group = group), alpha =.125, size= 2 )+
geom_path(aes(x2,y2, group = group))+
theme_canvas()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.