diff --git a/lib/ComicBook.js b/lib/ComicBook.js index a9a3c32..c63f009 100644 --- a/lib/ComicBook.js +++ b/lib/ComicBook.js @@ -12,6 +12,7 @@ - really need to speed up enhancements, try to use webworkers Nice 2 have: + - smart preload, start 5 before pointer and load from there, on navigation update preload position - offline access - thumbnail browser - page turn animation? (http://www.cynergysystems.com/blogs/page/rickbarraza?entry=the_secret_behind_the_page) @@ -60,10 +61,13 @@ function ComicBook(id, srcs, opts) { var context; // the 2d drawing context var buffer = 4; // image preload buffer level - var pointer = 0; // the current page + var loaded = 0; // the amount of 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(window.location.hash.replace('#',''),10) - 1) || 0; /** * Figure out the cursor position relative to the canvas. @@ -95,7 +99,6 @@ function ComicBook(id, srcs, opts) { * @see #ComicBook.prototype.enhance */ function init() { - // setup canvas canvas = document.getElementById(canvas_id); context = canvas.getContext("2d"); @@ -105,16 +108,14 @@ function ComicBook(id, srcs, opts) { } /* - * @param {String} id The canvas ID to draw the comic on. - * @param {Object} srcs An array of all the comic page srcs, in order * @see #preload */ ComicBook.prototype.draw = function () { - + init(); - + // preload images if needed - if (pages.length !== no_pages) { this.preload(this.srcs); } + if (pages.length !== no_pages) { this.preload(); } else { this.drawPage(); } }; @@ -132,13 +133,14 @@ function ComicBook(id, srcs, opts) { /** * Preload all images, draw the page only after a given number have been loaded. * - * @param srcs {Object} srcs * @see #drawPage */ - ComicBook.prototype.preload = function (srcs) { + ComicBook.prototype.preload = function () { + + var srcs = this.srcs; 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 // show load status panel @@ -151,7 +153,7 @@ function ComicBook(id, srcs, opts) { function preload(i) { var page = new Image(); - + $("#status p").text("loading page " + (i + 1) + " of " + no_pages); page.src = srcs[i]; @@ -186,10 +188,9 @@ function ComicBook(id, srcs, opts) { * TODO: if the current browser doesn't have canvas support, use img tags */ ComicBook.prototype.drawPage = function() { - + var zoom_scale; var offsetW = 0, offsetH = 0; - var page = pages[pointer]; var page2 = pages[pointer + 1]; @@ -265,7 +266,9 @@ function ComicBook(id, srcs, opts) { // draw the page(s) context.drawImage(page, offsetW, offsetH, page_width, page_height); if (options.displayMode === "double" && typeof page2 === "object") { context.drawImage(page2, page_width + offsetW, offsetH, page_width, page_height); } - + + window.location.hash = pointer + 1; + // apply any image enhancements previously defined $.each(options.enhance, function(action, options) { ComicBook.prototype.enhance[action](options);