JSON => PNG (Javascript)
Posted: Tue Dec 20, 2011 3:30 pm
A project I'm starting to work on will be an entirely browser based game. I had an idea to somehow use images as the save files.
The script/function set I came up with will take any javascript variable (array, scalar, object), and convert it into a PNG file. This is done by first JSON.stringify()ing it, then converting ascii to hex, then hex to RGB, then using <canvas> to set individual pixels to those RGB values. I also wrote functionality to then read a PNG file & convert it back to ASCII.
Rather than post it all here, I've made a working example here: http://jsfiddle.net/SzPMj/7/
In the future I'll probably work this all into one big class - this is just a proof of concept more than anything.
The script/function set I came up with will take any javascript variable (array, scalar, object), and convert it into a PNG file. This is done by first JSON.stringify()ing it, then converting ascii to hex, then hex to RGB, then using <canvas> to set individual pixels to those RGB values. I also wrote functionality to then read a PNG file & convert it back to ASCII.
Rather than post it all here, I've made a working example here: http://jsfiddle.net/SzPMj/7/
In the future I'll probably work this all into one big class - this is just a proof of concept more than anything.