Skip to content

Commit

Permalink
preload images after loading the page
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Harrington committed Sep 22, 2011
1 parent 7484f25 commit db6e212
Showing 1 changed file with 69 additions and 8 deletions.
77 changes: 69 additions & 8 deletions js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@

refit: function () {
var node = Y.one("#game-chooser"),
width = node.get("winWidth"),
height = node.get("winHeight");

node.setStyle("min-height", height);
Expand Down Expand Up @@ -242,13 +241,16 @@
attachEvents();
loadOptions();

if (save) {
active.game = Y.Solitaire[games[active.name]];
clearDOM();
active.game.loadGame(save);
} else {
playGame(active.name);
}
Preloader.preload();
Preloader.loaded(function () {
if (save) {
active.game = Y.Solitaire[games[active.name]];
clearDOM();
active.game.loadGame(save);
} else {
playGame(active.name);
}
});

GameChooser.init();
}
Expand Down Expand Up @@ -286,5 +288,64 @@
};
}

var Preloader = {
loadingCount: 0,

loaded: function (callback) {
if (this.loadingCount) {
setTimeout(function () {
this.loaded(callback);
}.bind(this), 100);
} else {
callback();
}
},

load: function (path) {
var image = new Image;

image.onload = function () {
--this.loadingCount;
}.bind(this);
image.src = path;

this.loadingCount++;
},

preload: function () {
var rank,
icons = ["agnes",
"flower-garden",
"forty-thieves",
"freecell",
"gclock",
"golf",
"klondike1t",
"klondike",
"montecarlo",
"pyramid",
"scorpion",
"spider1s",
"spider2s",
"spiderette",
"spider",
"tritowers",
"will-o-the-wisp",
"yukon"];

Y.Array.each(["s", "h", "c", "d"], function (suit) {
for (rank = 1; rank <= 13; rank++) {
this.load(Y.Solitaire.Card.base.theme + "/" + suit + rank + ".png");
}
}, this);

this.load(Y.Solitaire.Card.base.theme + "/facedown.png");

Y.Array.each(icons, function (image) {
this.load("layouts/mini/" + image + ".png");
}, this);
}
};

yui.use.apply(yui, modules().concat(main));
}());

0 comments on commit db6e212

Please sign in to comment.