You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.4 KiB
JavaScript

// P_3_1_3_01
/**
* analysing and sorting the letters of a text
* changing the letters alpha value in relation to frequency
*
* MOUSE
* position x : interpolate between normal text and sorted position
*
* KEYS
* a : toggle alpha mode
* s : save png
*/
'use strict';
var joinedText;
var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ,.;!? ';
var counters = [];
var posX;
var posY;
var drawAlpha = true;
function preload() {
joinedText = loadStrings('data/library_babel_extract1.txt');
}
function setup() {
createCanvas(1920, windowHeight);
noStroke();
textFont('monospace', 30);
joinedText = joinedText.join(' ');
// use the following command, to collect all characters in the text automatically
// alphabet = getUniqCharacters();
for (var i = 0; i < alphabet.length; i++) {
counters[i] = 0;
}
countCharacters();
}
function draw() {
background(213,204,255);
posX = 20;
posY = 40;
// go through all characters in the text to draw them
for (var i = 0; i < joinedText.length; i++) {
// again, find the index of the current letter in the character set
var upperCaseChar = joinedText.charAt(i).toUpperCase();
var index = alphabet.indexOf(upperCaseChar);
if (index < 0) continue;
if (drawAlpha) {
fill(0, counters[index] * 3);
} else {
fill(0);
}
var sortY = index * 20 + 40;
var m = map(mouseX, 50, width - 50, 0, 1);
m = constrain(m, 0, 1);
var interY = lerp(posY, sortY, m);
text(joinedText.charAt(i), posX, interY);
posX += textWidth(joinedText.charAt(i));
if (posX >= width - 200 && upperCaseChar == ' ') {
posY += 30;
posX = 20;
}
}
}
function countCharacters() {
for (var i = 0; i < joinedText.length; i++) {
// get one character from the text and turn it to uppercase
var c = joinedText.charAt(i);
var upperCaseChar = c.toUpperCase();
var index = alphabet.indexOf(upperCaseChar);
// increase the respective counter
if (index >= 0) counters[index]++;
}
}
function getUniqCharacters() {
var charsArray = joinedText.toUpperCase().split('');
var uniqCharsArray = charsArray.filter(function(char, index) {
return charsArray.indexOf(char) == index;
}).sort();
return uniqCharsArray.join('');
}
function keyReleased() {
if (key == 's' || key == 'S') saveCanvas(gd.timestamp(), 'png');
if (key == 'a' || key == 'A') drawAlpha = !drawAlpha;
}