summaryrefslogtreecommitdiff
path: root/js/loader.js
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2013-08-02 17:21:32 -0400
committerJules Laplace <jules@okfoc.us>2013-08-02 17:21:32 -0400
commit0003c072cb83f48386c5e264d86b0aa3a9eaa20d (patch)
treebd9a40fd1c387cc054b03ec1376927e7027d330f /js/loader.js
parentd4d9fc9513925397d71b4ac1ed3426fd6e7dc0e7 (diff)
app
Diffstat (limited to 'js/loader.js')
-rw-r--r--js/loader.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/js/loader.js b/js/loader.js
new file mode 100644
index 0000000..ddf4b7e
--- /dev/null
+++ b/js/loader.js
@@ -0,0 +1,85 @@
+/*****************************************************
+
+// sample config
+
+var config = {};
+config.images = [];
+
+// sample app
+
+function app = {};
+app.init = function(){
+ app.load();
+}
+app.load = function(){
+ app.loader = new Loader ();
+ app.loader.register("loading");
+
+ // register loaders here
+
+ app.loader.ready("loading");
+}
+app.ready = function(){
+}
+******************************************************/
+
+function Loader (readyCallback){
+ this.assets = {};
+ this.readyCallback = readyCallback || app.ready;
+}
+
+// Register an asset as loading
+Loader.prototype.register = function(s){
+ this.assets[s] = false;
+}
+
+// Signal that an asset has loaded
+Loader.prototype.ready = function(s){
+ console.log("ready >> " + s);
+
+ this.assets[s] = true;
+ if (this.loaded) return;
+ if (! this.isReady()) return;
+
+ this.loaded = true;
+ this.readyCallback();
+}
+
+// (boolean) Is the loader ready?
+Loader.prototype.isReady = function(){
+ for (var s in this.assets) {
+ if (this.assets.hasOwnProperty(s) && this.assets[s] != true) {
+ return false;
+ }
+ }
+ return true;
+}
+
+// (int) Number of assets remaining
+Loader.prototype.remainingAssets = function(){
+ var n = 0;
+ for (var s in this.assets) {
+ if (this.assets.hasOwnProperty(s) && this.assets[s] != true) {
+ n++;
+ console.log('remaining: ' + s);
+ }
+ }
+ return n;
+}
+
+// Preload the images in config.images
+Loader.prototype.preloadImages = function(){
+ for (var i = 0; i < config.images.length; i++) {
+ this.preloadImage(config.images[i]);
+ }
+}
+Loader.prototype.preloadImage = function(src){
+ var _this = this;
+ this.register(src);
+ var img = new Image();
+ img.onload = function(){
+ _this.ready(src);
+ }
+ img.src = src;
+ if (img.complete) img.onload();
+}