Skip to content

Instantly share code, notes, and snippets.

@simon-tiger
Created September 11, 2017 18:21
Show Gist options
  • Save simon-tiger/dda29679f786c22d07dac2677eba6036 to your computer and use it in GitHub Desktop.
Save simon-tiger/dda29679f786c22d07dac2677eba6036 to your computer and use it in GitHub Desktop.
Array.prototype.choice = function() {
var i = floor(random(this.length));
return this[i];
}
var paragraphs = [];
function process(word) {
var text = textinput.value();
var words = splitTokens(text, ' .,:;!@#$%&*()\n');
var p = createP('');
p.class("text");
paragraphs.push(p);
for (var i = 0; i < word.length; i++) {
var c = word.charAt(i);
var div = createDiv("");
div.parent(p);
askForText(div, c);
}
function askForText(elmnt, c) {
var pickWords = [];
for (var i = 0; i < words.length; i++) {
if (words[i].charAt(0) === c) {
pickWords.push(words[i]);
}
}
var word = pickWords.choice();
if (!select("#highlight").checked()) {
elmnt.html(word);
} else {
for (var i = 0; i < word.length; i++) {
var span = createSpan(word.charAt(i));
span.parent(elmnt);
if (i == 0) {
span.class("firstletter");
}
}
}
}
}
function clearAll() {
for (var i = 0; i < paragraphs.length; i++) {
paragraphs[i].remove();
}
paragraphs = [];
}
var wordinput;
var textinput;
var submit;
var clear;
var spam;
var sample;
var dropZone;
function preload() {
spam = loadStrings("spam.txt");
}
function setup() {
noCanvas();
wordinput = select("#word");
textinput = select("#textinput");
submit = select("#submit");
clear = select('#clear');
sample = select('#sample');
clear.mousePressed(clearAll);
submit.mousePressed(handleInput);
sample.mousePressed(useSample);
dropZone = select("#drop_zone");
dropZone.dragOver(highlight);
dropZone.drop(gotFile, unHighlight)
spam = join(spam, "\n");
}
function highlight() {
dropZone.style("background-color", "#AAA");
}
function unHighlight() {
dropZone.style("background-color", "#FFF");
}
function gotFile(file) {
textinput.value(file.data);
}
function useSample() {
textinput.value(spam);
}
function handleInput() {
process(wordinput.value());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment