Skip to content

Instantly share code, notes, and snippets.

@HelloWorld017
Created December 2, 2016 15:25
Show Gist options
  • Save HelloWorld017/787cf70fb74f650793818b1c2bf96f1f to your computer and use it in GitHub Desktop.
Save HelloWorld017/787cf70fb74f650793818b1c2bf96f1f to your computer and use it in GitHub Desktop.
A simple ascii text drawer
const draw = (width, height, text, font, fill) => {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = "#000";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.fillStyle = "#fff";
ctx.font = font;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText(text, canvas.width / 2, canvas.height / 2);
let string = '';
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
for(let y = 0; y < imageData.height; y++){
for(let x = 0; x < imageData.width; x++){
let data = x * 4 + y * imageData.width * 4;
if(imageData.data[data] > 0) string += fill;
else string += ' ';
}
string += '\n';
}
return string;
};
draw(300, 50, "MUSECA", "50px MUSECA", "$");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment