Created
December 2, 2016 15:25
-
-
Save HelloWorld017/787cf70fb74f650793818b1c2bf96f1f to your computer and use it in GitHub Desktop.
A simple ascii text drawer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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