PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Aug 11, 2020 2:37 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: JSON => PNG (Javascript)
PostPosted: Tue Dec 20, 2011 4:30 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
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.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
PostPosted: Tue Dec 20, 2011 10:09 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine


Top
 Profile  
 
PostPosted: Tue Dec 20, 2011 11:55 pm 
Offline
Forum Commoner

Joined: Sat May 19, 2007 4:10 am
Posts: 50
what is the practical application of this procedure?


Top
 Profile  
 
PostPosted: Wed Dec 21, 2011 10:49 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
The main reason for doing this was to see if I could. I'm thinking I'll store all the game data in one central object, then write that object to file when saving. To load a save game, just upload a save file image, it'll be read in, and the game initialized.

LocalStorage is limited to 5 MB. While I doubt the PNG files I need to generate will be anywhere close to that, it's nice to not have a limit.

Image files can also be downloaded and moved around, say from computer to computer. So, when my game becomes wildly popular, the throngs of fans will be able to swap saved games.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
PostPosted: Mon Dec 26, 2011 12:31 am 
Offline
Forum Commoner

Joined: Sat May 19, 2007 4:10 am
Posts: 50
This is good as long as people don't take screenshots from their monitor or take a snap on their mobiles in hopes of getting the save data to transfer to another instance of a game(like QR code). Or will it work so?


Top
 Profile  
 
PostPosted: Wed Jan 04, 2012 10:47 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
I doubt it, the images in the example are zoomed in 2000% - the actual generated image is much smaller. Reading the image back in requires pixel perfect accuracy, so screenshots will almost certainly fail.

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
PostPosted: Wed Jan 04, 2012 4:07 pm 
Offline
DevNet Master
User avatar

Joined: Sun Feb 15, 2009 12:08 pm
Posts: 2794
Location: .za
The idea (and application) is pretty good; reminds me of steganography.

_________________
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering


Top
 Profile  
 
PostPosted: Mon Jan 09, 2012 3:00 pm 
Offline
Forum Contributor
User avatar

Joined: Thu May 11, 2006 8:58 pm
Posts: 305
Location: Utah, USA
A while back this technique was used in the 10K apart contest. Here are details: http://www.iamcal.com/png-store/


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group