-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathsketch.js
56 lines (40 loc) · 1.46 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
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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("gloria20.jpg");
}
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0);
let w = width / gloria.width;
let h = height / gloria.height;
gloria.loadPixels();
for (let i = 0; i < gloria.width; i++) {
for (let j = 0; j < gloria.height; j++) {
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(255);
//square(i * w, j * h, w);
const len = density.length;
const charIndex = floor(map(avg,0,256,len,0));
textSize(w);
textAlign(CENTER, CENTER);
text(density.charAt(charIndex), i * w + w * 0.5, j * h + h * 0.5);
}
}
}