Skip to content

Instantly share code, notes, and snippets.

@briangordon
Created March 10, 2013 06:09
Show Gist options
  • Save briangordon/5127331 to your computer and use it in GitHub Desktop.
Save briangordon/5127331 to your computer and use it in GitHub Desktop.
HTML5 JavaScript snippet for turning arbitrary text into a binary array of pixel on/off
var buffer = document.createElement('canvas');
var bufContext = buffer.getContext("2d");
buffer.width = 60;
buffer.height = 20;
bufContext.fillStyle = "#FFFFFF";
bufContext.fillRect(0, 0, 60, 20);
bufContext.textBaseline = "top";
bufContext.font = "12px verdana";
bufContext.fillStyle = "#000000";
bufContext.fillText("the game", 0, 0);
var img = bufContext.getImageData(0, 0, 60, 20);
var bitmap = [];
for(var i=0;i<60;i++) {
var cur = [];
bitmap.push(cur);
for(var j =0; j<20; j++) {
cur.push([]);
}
}
for (var y=0; y<20; y++) {
for (var x=0; x<60; x++) {
var whichPixel = 60*y + x;
bitmap[x][y] = (img.data[4*whichPixel] < 140); //If the red channel of the pixel is lower than 140, we consider it "on"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment