Published
Edited
Nov 17, 2019
1 star
Insert cell
Insert cell
draw('Grant Custter')
Insert cell
function draw(string) {
let binary = convert(string)
let canvas = document.createElement('canvas');
let ps = 10;
let cols = 2;
let rows = 4;
// let cols = 4;
// let rows = 2;
canvas.width = binary.length * cols * ps;
canvas.height = rows * ps;
let ctx = canvas.getContext('2d');
for (let b = 0; b < binary.length; b++) {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
let byte = binary[b];
let bit = byte[r * cols + c];
if (parseInt(bit) === 1) {
let x = b * cols * ps + c * ps;
let y = r * ps;
ctx.fillRect(x, y, ps, ps)
}
}
}
}
return canvas;
}
Insert cell
// adapted from https://stackoverflow.com/a/14430733/8691291
function convertChar(char) {
return char.charCodeAt(0).toString(2).padStart(8, '0');
}
Insert cell
function convert(string) {
let chars = string.split('')
let binary = chars.map(char => convertChar(char))
return binary
}
Insert cell
convert('Grant Custer')
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more