Skip to content

Instantly share code, notes, and snippets.

@mvasilkov
Created August 28, 2014 22:31
Show Gist options
  • Save mvasilkov/cf823e89624882c54dea to your computer and use it in GitHub Desktop.
Save mvasilkov/cf823e89624882c54dea to your computer and use it in GitHub Desktop.
Image loader for canvas, suitable for JS13K
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
})
}
// 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