Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created November 6, 2011 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/1343481 to your computer and use it in GitHub Desktop.
Save einarwh/1343481 to your computer and use it in GitHub Desktop.
JavaScript to load a base64-encoded space invader.
var PixIt = {
load : function () {
var j = {
"pixelsWide": 13,
"pixelsHigh": 10,
"pixelSize": 8,
"payload":
[
{
"color": '#000000',
"pixels":
[
[1, 5], [1, 6], [1, 7], [2, 4],
[2, 5], [3, 1], [3, 3], [3, 4],
[3, 5], [3, 6], [3, 7], [4, 2],
[4, 3], [4, 5], [4, 6], [4, 8],
[5, 3], [5, 4], [5, 5], [5, 6],
[5, 8], [6, 3], [6, 4], [6, 5],
[6, 6], [7, 3], [7, 4], [7, 5],
[7, 6], [7, 8], [8, 2], [8, 3],
[8, 5], [8, 6], [8, 8], [9, 1],
[9, 3], [9, 4], [9, 5], [9, 6],
[9, 7], [10, 4], [10, 5],
[11, 5], [11, 6], [11, 7]
]
}
]
};
$('div.invader').each(function (index) {
var inv = $(this);
j.payload[0].color = inv.text();
$.ajax({
type: 'POST',
url: "http://localhost:52984/bix.it",
contentType: "application/json; charset=utf-8",
accepts: "plain/text",
dataType: "text",
data: JSON.stringify(j),
success: function (d) {
var src = "data:image/png;base64," + d;
inv.html('<img src="' + src + '"/>');
inv.css('visibility', 'visible');
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment