Skip to content

Instantly share code, notes, and snippets.

@mvasilkov mvasilkov/load.js
Created Aug 28, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.