xxxxxxxxxx
57
// Image to ASCII
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/166-ascii-image.html
// https://youtu.be/55iwMYv8tGI
// ASCII video: https://editor.p5js.org/codingtrain/sketches/KTVfEcpWx
// ASCII image canvas: https://editor.p5js.org/codingtrain/sketches/r4ApYWpH_
// ASCII image DOM: https://editor.p5js.org/codingtrain/sketches/ytK7J7d5j
// ASCII image source text: https://editor.p5js.org/codingtrain/sketches/LNBpdYQHP
// ASCII image weather API: https://editor.p5js.org/codingtrain/sketches/DhdqcoWn4
let sourceText;
let poem;
let gloria;
let startIndex = 0;
function preload() {
gloria = loadImage("gloria48.jpg");
sourceText = loadStrings("gloria.txt");
}
function setup() {
createCanvas(800, 800);
poem = sourceText.join(' ');
textFont("Courier-Bold");
}
function draw() {
background(0);
frameRate(10);
let charIndex = startIndex;
let w = width / gloria.width;
let h = height / gloria.height;
gloria.loadPixels();
for (let j = 0; j < gloria.height; j++) {
for (let i = 0; i < gloria.width; i++) {
const pixelIndex = (i + j * gloria.width) * 4;
const r = gloria.pixels[pixelIndex + 0];
const g = gloria.pixels[pixelIndex + 1];
const b = gloria.pixels[pixelIndex + 2];
const avg = (r + g + b) / 3;
noStroke();
fill(avg);
textSize(w*1.2);
textAlign(CENTER, CENTER);
text(poem.charAt(charIndex % poem.length), i * w + w * 0.5, j * h + h * 0.5);
charIndex++;
}
}
startIndex++;
}