Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I can put the HTML and JavaScript together in the same .html file, but the images need to be separate files. The only solution that comes to my mind is to give to the user a .zip folder.

This is not pretty though... How else can I do this?

share|improve this question
1  
The "standard" isn't to make JS games downloadable; they only work in a browser. There may be some obscure way to do it, but frankly why force it: use the right tool for the job. –  jhocking 11 hours ago

3 Answers 3

up vote 5 down vote accepted

You can embed images in the HTML document using the dataurl-syntax which allows to put the base64 representation of the binary image data as the src-attribute of an image. This also works on any other kind of media file.

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB9EFBAoYMhVvMQIAAAAtSURBVHicY/z//z8DHoBH+v///yy4FDEyMjIwMDDhM3lgpaEuh7gTEzDiDxYA9HEPDF90e5YAAAAASUVORK5CYII=">

When you use CSS stylesheets, you can create these completely procedural in JavaScript.

share|improve this answer
    
Great, thanks!! –  Arnaud 7 hours ago

Just putting all the files into a .zip file isn't a viable solution because most web-applications need a web-server so that they can access resources via HTTP-requests. On some systems you can access files via the file:// URI-scheme, but that's not guaranteed to work everywhere because of security-reasons and will fail for things such as AJAX requests.

It might work for a very simple app where you have most resources inline but this really isn't the approach I'd recommend to anybody. There are alternatives though, such as:

Build a native application

You can package your web-application as a native app using Node-Webkit. You can even use this to add native desktop features (such as local save-games) to your game.

This isn't a "package and done" approach though.. you'll probably have to rewrite portions of your application and write different loaders (eg. with a desktop app you'll rather load the files via filesystem, whereas you use HTTP-Requests in a browser/online-game).

Use HTML5 features to allow offline access of your app

If your primary goal is to allow the player to play your game offline, then you can also use application-cache to allow offline access of your app. This is a feature that works in most modern browsers. The added benefit is that the user doesn't have to download anything and can just use a bookmark to play your game even when offline.

For resources that are being loaded asynchronously (AJAX) you'll also have to implement separate loading mechanisms. You could make use of local-storage to save these resources for offline use.

share|improve this answer
    
Node-Webkit is pretty large, and you'll end up with something like 60MB for a "hello world" JS game if you include it. –  ashes999 5 hours ago
    
This is the right answer if your application is even remotely complicated. –  Vaughan Hilts 5 hours ago
    
@ashes999 I agree with you, yet 60MB is still pretty small for a desktop app nowadays. A lot of the general-purpose frameworks create some file-size overhead (take Unity as a prominent example). With ever increasing storage space, convenience wins over file-size ;) –  bummzack 4 hours ago
    
@bummzack I don't disagree. It's just depressing to take a ~100kb JS game and see it become 60MB, before you add any art or audio. Hopefully someone will come up with a better way. +1 btw –  ashes999 3 hours ago
    
Don't forget XULRunner which is ~30 MB of download, but can be packaged with an nsis installer just fine. You can always use Firefox (or Chrome, but FF is more powerful for this) to run xul/chrome html apps directly through it (using its internal xulrunner binary). –  ivy_lynx 1 hour ago

For a Chrome-based solution to make the game run offline and enjoy some native functionality, you can consider making a Chrome App.

This way, you can distribute it in Chrome Web Store for added visibility, you can enjoy some powerful APIs, and make it look more like a standalone app.

The downside is, of course, requiring Chrome.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.