-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathsketch.js
43 lines (34 loc) · 1.25 KB
/
sketch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Image to ASCII
// The Coding Train / Daniel Shiffman
// https://thecodingtrain.com/CodingChallenges/166-ascii-image.html
// YT Link TBD
// 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
const density = "Ñ@#W$9876543210?!abc;:+=-,._ ";
let gloria;
function preload() {
gloria = loadImage("gloria48.jpg");
}
function setup() {
noCanvas();
gloria.loadPixels();
for (let j = 0; j < gloria.height; j++) {
let row = "";
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;
const len = density.length;
const charIndex = floor(map(avg, 0, 256, len, 0));
const c = density.charAt(charIndex);
if (c == " ") row += " ";
else row += c;
}
createDiv(row);
}
}