Simple Slot machine game using HTML5 Part 3: Loading
May 1, 2013 7 Comments
UPDATE: See also Simple Slot machine game using HTML5 Part 4: Offline mode
In previous part 1 and part 2 I implemented simple slots machine purely in HTML5 with audio support.
After adding audio, the initial loading time increased a lot up to several seconds. This slowdown is easy to miss during development, as developer has always primed browser cache and content is loaded from development server that is hosted locally or even in developers machine.
It’s very important to occasionally clear the browser cache and put the game to remote server and reload the game to get realistic estimation of new users loading time (i.e. the empty cache experience).
This part improves the landing experience by adding loading progress bar so users know that the game is loading and will start soon.
Try out version with loading bar here.
How it works
Loading bar is simple nested div, where parent draws the outline and the child is the filling progress bar.
<div id="progressbar"> <div id="progress"></div> </div>
The CSS rules set the colors and position.
#progressbar { margin-top: 10px; background: black; border: 1px solid mediumaquamarine; width: 80%; height: 15px; margin-left: auto; margin-right: auto; } #progressbar #progress { height: 100%; position: relative; width: 0%; left: 0; background: #77e0fb; }
Indicating progress is now simply matter of updating the width of #progress
div.
Any HTML5 game loading bar implementation should be done purely with HTML and CSS and not use any images, as this just makes total loading experience slower and loading bar does not appear immediately when page renders. If you must use images, consider using base64 embedded images.
#progressbar { background-image:url(data:image/png;base64,iAE28w0KGgoABBANSAxhEegBB54DIA...); ... }
If possible, void building the loading view with javascript as your page must load javascript before being able to show anything.
For best experience you should always have the CSS file in the <head>
section of the HTML file and the javascript at the end of file, just before closing </body>
. This way page can render with proper layout before any javascript has been loaded.
<!DOCTYPE HTML> <html> <head> ... <link href='http://fonts.googleapis.com/css?family=Slackey' rel='stylesheet' type='text/css'/> <link type="text/css" rel="stylesheet" href="css/reset.css"/> <link type="text/css" rel="stylesheet" href="css/slot.css"/> </head> <body> <div id="viewport"> ... </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type="text/javascript" src="js/slot.js"></script> <script type="text/javascript">$(function () { SlotGame(); });</script> </body> </html>
Both image and audio assets are loaded with same preloader, that keeps track of the progress and updates the progressbar. When loading is completed it hides the loading view. See the code for details how the preloader is used to load images and web/html5 audio files.
var progressCount = 0; // current progress count var progressTotalCount = 0; // total count function updateProgress( inc ) { progressCount += (inc || 1); if ( progressCount >= progressTotalCount ) { // done, complete progress bar and hide loading screen $('#progress').css('width', '100%'); $('#loading').slideUp(600); } else { // Update progress bar $('#progress').css('width', parseInt( 100 * progressCount / progressTotalCount) + '%'); } } // Generic preloader handler, it calls preloadFunction for each item and // passes function to it that it must call when done. function preloader( items, preloadFunction, callback ) { var itemc = items.length; var loadc = 0; // called by preloadFunction to notify result function _check( err, id ) { updateProgress(1); if ( err ) { alert('Failed to load ' + id + ': ' + err); } loadc++; if ( itemc == loadc ) callback(); } progressTotalCount += items.length; // queue each item for fetching items.forEach(function(item) { preloadFunction( item, _check); }); }
Improving loading time
You can further improve initial loading time by conventional web tricks
- Enable HTTP gzip compression in the web server
- Include jquery and library scripts from google api url, user may have those already in the cache
- Merge and minify the javascript e.g. with uglify-js
- Merge and minify the CSS with CSS compressor
- Minify PNG image files with pngout. Use JPG where possible.
- Compose all images in single montage and use that with CSS sprites and canvas ctx.drawImage
- Convert audio files to mono to save half of the size. Use e.g. Audacity or ffmpeg
- Host asset files in CDN, such as Akamai or Amazon S3. Note that if assets are loaded from different domain than the HTML document, you may encounter security problems if Cross-origin policy is not properly set for the content.
Use Chrome browsers audit tool get idea what takes most time.
Code is available in Github.
Continue to Simple Slot machine game using HTML5 Part 4: Offline mode