Created
August 28, 2014 22:31
-
-
Save mvasilkov/cf823e89624882c54dea to your computer and use it in GitHub Desktop.
Image loader for canvas, suitable for JS13K
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function load(images, done) { | |
var toLoad = images.length | |
var res = Array(toLoad) | |
var loaded = 0 | |
images.forEach(function (url, i) { | |
var image = new Image | |
image.onload = function () { | |
res[i] = this | |
if (++loaded == toLoad) | |
done(res) | |
} | |
image.src = url | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Usage example: | |
load(['./pub/player.png', './pub/evil_gay_mohammed.png'], function (loaded) { | |
player.texture = loaded[0] | |
enemy.texture = loaded[1] | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment