refactored a little after running through jslint

This commit is contained in:
Bala Clark 2010-07-09 16:22:16 +01:00
parent b0c0016c0a
commit 2dbbca3539

View file

@ -1,273 +1,275 @@
/* /*jslint on: true, eqeqeq: true */
TODOs: /*
TODOs:
Fo sho:
- The buffer is actually loading all the images at the same time, only load the next one on page.onload Fo sho:
- Show a progress bar when buffering - The buffer is actually loading all the images at the same time, only load the next one on page.onload
- use document.body.offsetWidth where present? (instead of window.innerWidth, the former excludes the width of scrollbars) - Show a progress bar when buffering
- scroll two pages at a time in double page mode - use document.body.offsetWidth where present? (instead of window.innerWidth, the former excludes the width of scrollbars)
- manga mode - scroll two pages at a time in double page mode
- page controls - manga mode
- thumbnail browser - page controls
- chrome frame - thumbnail browser
- chrome frame
Nice 2 have:
- remember position (use localStorage) Nice 2 have:
- maybe use local storage for the pages array too? might be easier to implement manga mode with that - remember position (use localStorage)
- offline mode - maybe use local storage for the pages array too? might be easier to implement manga mode with that
- page turn animation - offline mode
- page turn animation
*/
*/
var book
/**
window.onload = function() { * Merge two arrays. Any properties in b will replace the same properties in
* a. New properties from b will be added to a.
var pages = [ *
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/00.jpg", * @param a {Object}
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/01.jpg", * @param b {Object}
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/02.jpg", */
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/03.jpg", function merge(a, b) {
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/04.jpg",
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/05.jpg", var prop;
"http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/06.jpg"
]; if (typeof b === "undefined") { b = {}; }
var options = { for (prop in a) {
displayMode: "double", if (a.hasOwnProperty(prop)) {
zoomMode: "fitWidth" if (prop in b) { continue; }
} b[prop] = a[prop];
}
book = new ComicBook("comic", pages, options); }
book.draw();
} return b;
}
window.onresize = function() {
book.draw(); function ComicBook(id, srcs, opts) {
}
this.id = id; // canvas element id
function ComicBook(id, srcs, opts) { this.srcs = srcs; // array of image srcs for pages
this.id = id; var defaults = {
this.srcs = srcs; displayMode: "double", // single / double
zoomMode: "fitWidth" // manual / fitWidth
var defaults = { };
displayMode: "double", // single / double
zoomMode: "fitWidth" // manual / fitWidth var options = merge(defaults, opts); // options array for internal use
}
var options = merge(defaults, opts); var pages = []; // array of preloaded Image objects
var canvas; // the HTML5 canvas object
var pages = []; var context; // the 2d drawing context
var canvas;
var context; var buffer = 4; // image preload buffer level
var pointer = 0; // the current page
var buffer = 4; var loaded = 0; // the amount of images that have been loaded so far
var pointer = 0;
var loaded = 0; var scale = 1; // page zoom scale, 1 = 100%
var scale = 1; /**
* Figure out the cursor position relative to the canvas.
/* *
* @param {String} id The canvas ID to draw the comic on. * Thanks to: Mark Pilgrim & http://diveintohtml5.org/canvas.html
* @param {Object} srcs An array of all the comic page srcs, in order */
* @see #preload function getCursorPosition(e) {
*/
ComicBook.prototype.draw = function() { var x; // horizontal cursor position
// setup canvas // check if page relative positions exist, if not figure them out
canvas = document.getElementById(this.id); if (e.pageX) {
context = canvas.getContext("2d"); x = e.pageX;
} else {
// preload images if needed x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
if (pages.length != this.srcs.length) this.preload(this.srcs); }
else this.drawPage();
// make the position relative to the canvas
// add page controls x -= canvas.offsetLeft;
canvas.addEventListener("click", ComicBook.prototype.navigation, false);
} // check if the user clicked on the left or right side
return (x <= canvas.width / 2) ? 'left' : 'right';
/* }
* Zoom the canvas
* /*
* @param new_scale {Number} Scale the canvas to this ratio * @param {String} id The canvas ID to draw the comic on.
*/ * @param {Object} srcs An array of all the comic page srcs, in order
ComicBook.prototype.zoom = function(new_scale) { * @see #preload
options.zoomMode = "manual"; */
scale = new_scale; ComicBook.prototype.draw = function () {
if (typeof pages[pointer] == "object") this.drawPage();
} // setup canvas
canvas = document.getElementById(this.id);
/** context = canvas.getContext("2d");
* Preload all images, draw the page only after a given number have been loaded.
* // preload images if needed
* @param srcs {Object} srcs if (pages.length != this.srcs.length) { this.preload(this.srcs); }
* @see #drawPage else { this.drawPage(); }
*/
ComicBook.prototype.preload = function(srcs) { // add page controls
canvas.addEventListener("click", ComicBook.prototype.navigation, false);
if (srcs.length < buffer) buffer = srcs.length; // don't get stuck if the buffer level is higher than the number of pages };
srcs.forEach(function(src, i) { /*
* Zoom the canvas
var page = new Image(); *
* @param new_scale {Number} Scale the canvas to this ratio
page.src = src; */
ComicBook.prototype.zoom = function (new_scale) {
page.onload = function() { options.zoomMode = "manual";
pages[i] = this; loaded++; scale = new_scale;
if (loaded == buffer) ComicBook.prototype.drawPage(); if (typeof pages[pointer] == "object") { this.drawPage(); }
} };
});
} /**
* Preload all images, draw the page only after a given number have been loaded.
/** *
* Draw the current page in the canvas * @param srcs {Object} srcs
* * @see #drawPage
* TODO: break this down into drawSinglePage() & drawDoublePage() */
* TODO: if the current browser doesn't have canvas support, use img tags ComicBook.prototype.preload = function (srcs) {
*/
ComicBook.prototype.drawPage = function() { if (srcs.length < buffer) { buffer = srcs.length; } // don't get stuck if the buffer level is higher than the number of pages
var zoom_scale; srcs.forEach(function(src, i) {
var offsetW = 0, offsetH = 0;
var page = new Image();
var page = pages[pointer];
var page2 = pages[pointer + 1]; page.src = src;
if (typeof page != "object") throw "invalid page type '"+ typeof page +"'"; page.onload = function () {
pages[i] = this; loaded++;
var width = page.width; if (loaded == buffer) { ComicBook.prototype.drawPage(); }
};
if (options.displayMode == "double") { });
};
// for double page spreads, factor in the width of both pages
if (typeof page2 == "object") width += page2.width; /**
* Draw the current page in the canvas
// if this is the last page and there is no page2, still keep the canvas wide *
else width += width; * TODO: break this down into drawSinglePage() & drawDoublePage()
} * TODO: if the current browser doesn't have canvas support, use img tags
*/
// update the page scale if a non manual mode has been chosen ComicBook.prototype.drawPage = function () {
switch(options.zoomMode) {
var zoom_scale;
case "manual": var offsetW = 0, offsetH = 0;
zoom_scale = (options.displayMode == "double") ? scale * 2 : scale;
break; var page = pages[pointer];
var page2 = pages[pointer + 1];
case "fitWidth":
zoom_scale = (window.innerWidth > width) if (typeof page != "object") { throw "invalid page type '"+ typeof page +"'"; }
? ((window.innerWidth - width) / window.innerWidth) + 1 // scale up if the window is wider than the page
: window.innerWidth / width; // scale down if the window is narrower than the page var width = page.width;
break;
if (options.displayMode == "double") {
default:throw "invalid zoomMode";
} // for double page spreads, factor in the width of both pages
if (typeof page2 == "object") { width += page2.width; }
var canvas_width = page.width * zoom_scale;
var canvas_height = page.height * zoom_scale; // if this is the last page and there is no page2, still keep the canvas wide
else { width += width; }
var page_width = (options.zoomMode == "manual") ? page.width * scale : canvas_width; }
var page_height = (options.zoomMode == "manual") ? page.height * scale : canvas_height;
// update the page scale if a non manual mode has been chosen
canvas_height = page_height; switch(options.zoomMode) {
// make sure the canvas is always at least full screen, even if the page is more narrow than the screen case "manual":
canvas.width = (canvas_width < window.innerWidth) ? window.innerWidth : canvas_width; zoom_scale = (options.displayMode == "double") ? scale * 2 : scale;
canvas.height = (canvas_height < window.innerHeight) ? window.innerHeight : canvas_height; break;
// work out a horizonal position that will keep the pages always centred case "fitWidth":
if (canvas_width < window.innerWidth && options.zoomMode == "manual") { zoom_scale = (window.innerWidth > width) ? ((window.innerWidth - width) / window.innerWidth) + 1 // scale up if the window is wider than the page
offsetW = (window.innerWidth - page_width) / 2; : window.innerWidth / width; // scale down if the window is narrower than the page
if (options.displayMode == "double") offsetW = offsetW - page_width / 2; break;
} default:throw "invalid zoomMode";
}
// work out a vertical position that will keep the pages always centred
if (canvas_height < window.innerHeight && options.zoomMode == "manual") { var canvas_width = page.width * zoom_scale;
offsetH = (window.innerHeight - page_height) / 2; var canvas_height = page.height * zoom_scale;
}
var page_width = (options.zoomMode == "manual") ? page.width * scale : canvas_width;
// draw the page(s) var page_height = (options.zoomMode == "manual") ? page.height * scale : canvas_height;
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); canvas_height = page_height;
} // make sure the canvas is always at least full screen, even if the page is more narrow than the screen
canvas.width = (canvas_width < window.innerWidth) ? window.innerWidth : canvas_width;
/** canvas.height = (canvas_height < window.innerHeight) ? window.innerHeight : canvas_height;
* Increment the counter and draw the page in the canvas
* // work out a horizonal position that will keep the pages always centred
* @see #drawPage if (canvas_width < window.innerWidth && options.zoomMode == "manual") {
*/ offsetW = (window.innerWidth - page_width) / 2;
ComicBook.prototype.drawNextPage = function() { if (options.displayMode == "double") { offsetW = offsetW - page_width / 2; }
if (pointer + 1 < pages.length) { }
pointer++;
this.drawPage(); // work out a vertical position that will keep the pages always centred
} if (canvas_height < window.innerHeight && options.zoomMode == "manual") {
} offsetH = (window.innerHeight - page_height) / 2;
}
/**
* Decrement the counter and draw the page in the canvas // draw the page(s)
* context.drawImage(page, offsetW, offsetH, page_width, page_height);
* @see #drawPage if (options.displayMode == "double" && typeof page2 == "object") { context.drawImage(page2, page_width + offsetW, offsetH, page_width, page_height); }
*/
ComicBook.prototype.drawPrevPage = function() { };
if (pointer > 0) {
pointer--; /**
this.drawPage(); * Increment the counter and draw the page in the canvas
} *
} * @see #drawPage
*/
ComicBook.prototype.navigation = function(e) { ComicBook.prototype.drawNextPage = function () {
if (pointer + 1 < pages.length) {
switch (e.type) { pointer++;
this.drawPage();
case "click": }
switch (getCursorPosition(e)) { };
case "left": ComicBook.prototype.drawPrevPage(); break;
case "right": ComicBook.prototype.drawNextPage(); break; /**
} * Decrement the counter and draw the page in the canvas
break; *
} * @see #drawPage
} */
ComicBook.prototype.drawPrevPage = function () {
/** if (pointer > 0) {
* Figure out the cursor position relative to the canvas. pointer--;
* this.drawPage();
* Thanks to: Mark Pilgrim & http://diveintohtml5.org/canvas.html }
*/ };
function getCursorPosition(e) {
ComicBook.prototype.navigation = function (e) {
var x;
if (e.type === "click") {
// check if page relative positions exist switch (getCursorPosition(e)) {
if (e.pageX) x = e.pageX; case "left": ComicBook.prototype.drawPrevPage(); break;
case "right": ComicBook.prototype.drawNextPage(); break;
// if not figure them out }
else x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; }
};
// make the position relative to the canvas }
x -= canvas.offsetLeft;
var book;
// check if the user clicked on the left or right side
return (x <= canvas.width / 2) ? 'left' : 'right'; window.onload = function() {
}
var pages = [
/** "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/00.jpg",
* Merge two arrays. Any properties in b will replace the same properties in "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/01.jpg",
* a. New properties from b will be added to a. "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/02.jpg",
* "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/03.jpg",
* @param a {Object} "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/04.jpg",
* @param b {Object} "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/05.jpg",
*/ "http://dev.justforcomics.com/get/image/?f=comics/extracted/oni_whiteout_melt_1/06.jpg"
function merge(a, b) { ];
if (typeof b == "undefined") b = {}; var options = {
displayMode: "double",
for (prop in a) { zoomMode: "fitWidth"
if (prop in b) continue; };
b[prop] = a[prop];
} book = new ComicBook("comic", pages, options);
book.draw();
return b; };
}
} window.onresize = function() {
book.draw();
};