-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch-05D.js
156 lines (107 loc) · 3.26 KB
/
sketch-05D.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const canvasSketch = require('canvas-sketch');
const random = require('canvas-sketch-util/random');
const settings = {
dimensions: [ 1080, 1080 ]
};
let manager;
let text = 'A';
let fontSize = 1200;
let fontFamily = 'serif';
const typeCanvas = document.createElement('canvas');
const typeContext = typeCanvas.getContext('2d');
const sketch = ({ context, width, height }) => {
const cell = 20;
const cols = Math.floor(width / cell);
const rows = Math.floor(height / cell);
const numCells = cols * rows;
typeCanvas.width = cols;
typeCanvas.height = rows;
return ({ context, width, height }) => {
typeContext.fillStyle = 'black';
typeContext.fillRect(0, 0, cols, rows);
fontSize = cols * 1.2;
typeContext.fillStyle = 'white';
typeContext.font = `${fontSize}px ${fontFamily}`;
typeContext.textBaseline = 'top';
const metrics = typeContext.measureText(text);
const mx = metrics.actualBoundingBoxLeft * -1;
const my = metrics.actualBoundingBoxAscent * -1;
const mw = metrics.actualBoundingBoxLeft + metrics.actualBoundingBoxRight;
const mh = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
const tx = (cols - mw) * 0.5 - mx;
const ty = (rows - mh) * 0.5 - my;
typeContext.save();
typeContext.translate(tx, ty);
typeContext.beginPath();
typeContext.rect(mx, my, mw, mh);
typeContext.stroke();
typeContext.fillText(text, 0, 0);
typeContext.restore();
const typeData = typeContext.getImageData(0, 0, cols, rows).data;
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
context.textBaseline = 'middle';
context.textAlign = 'center';
// context.drawImage(typeCanvas, 0, 0);
for (let i = 0; i < numCells; i++) {
const col = i % cols;
const row = Math.floor(i / cols);
const x = col * cell;
const y = row * cell;
const r = typeData[i * 4 + 0];
const g = typeData[i * 4 + 1];
const b = typeData[i * 4 + 2];
const a = typeData[i * 4 + 3];
const glyph = getGlyph(r);
context.font = `${cell * 2}px ${fontFamily}`;
if (Math.random() < 0.1) context.font = `${cell * 6}px ${fontFamily}`;
context.fillStyle = 'white';
context.save();
context.translate(x, y);
context.translate(cell * 0.5, cell * 0.5);
// context.fillRect(0, 0, cell, cell);
context.fillText(glyph, 0, 0);
context.restore();
}
};
};
const getGlyph = (v) => {
if (v < 50) return '';
if (v < 100) return '.';
if (v < 150) return '-';
if (v < 200) return '+';
const glyphs = '_= /'.split('');
return random.pick(glyphs);
};
const onKeyUp = (e) => {
text = e.key.toUpperCase();
manager.render();
};
document.addEventListener('keyup', onKeyUp);
const start = async () => {
manager = await canvasSketch(sketch, settings);
};
start();
/*
const url = 'https://picsum.photos/200';
const loadMeSomeImage = (url) => {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = () => reject();
img.src = url;
});
};
const start = async () => {
const img = await loadMeSomeImage(url);
console.log('image width', img.width);
console.log('this line');
};
// const start = () => {
// loadMeSomeImage(url).then(img => {
// console.log('image width', img.width);
// });
// console.log('this line');
// };
start();
*/