diff --git a/lib/ComicBook.js b/lib/ComicBook.js index 958e239..8b76821 100644 --- a/lib/ComicBook.js +++ b/lib/ComicBook.js @@ -59,23 +59,24 @@ function ComicBook(id, srcs, opts) { var pages = []; // array of preloaded Image objects var canvas; // the HTML5 canvas object var context; // the 2d drawing context - - var buffer = 4; // image preload buffer level - var loaded = 0; // the amount of images that have been loaded so far + var buffer = 1; // image preload buffer level + var loaded = []; // the images that have been loaded so far var scale = 1; // page zoom scale, 1 = 100% // the current page, can pass a default as a url hash var pointer = (parseInt(location.hash.substring(1),10) - 1) || 0; - // enables the back button by tracking any url hash changes and loading the correct page. - setInterval(function() { + // enables the back button + function checkHash() { + var hash = parseInt(location.hash.substring(1),10) - 1; - if (hash !== pointer) { + + if (hash !== pointer && loaded.indexOf(hash) > -1) { pointer = hash; ComicBook.prototype.draw(); } - }, 300); - + } + /** * Figure out the cursor position relative to the canvas. * @@ -111,7 +112,10 @@ function ComicBook(id, srcs, opts) { context = canvas.getContext("2d"); // add page controls + // TODO: add IE event listeners too. canvas.addEventListener("click", ComicBook.prototype.navigation, false); + window.addEventListener("hashchange", checkHash, false); + //setInterval(function() { checkHash(); }, 300); // TODO: enable this when there is no onhashchange event } /* @@ -148,8 +152,9 @@ function ComicBook(id, srcs, opts) { if (no_pages < buffer) { buffer = no_pages; } // don't get stuck if the buffer level is higher than the number of pages - var i = 0; // the current page counter for this method - + var i = pointer; // the current page counter for this method + //if (i - buffer >= 0) { i = i - buffer; } // start loading from the first requested page - buffer + // show load status panel if ($("#status").length === 0) { $(canvas).after('