//资源文件加载器,确保canvas在图片加载完成后开始渲染
import {Resources} from './Resources.js'
export class ResourceLoader {
constructor() {
this.map = new Map(Resources)
//替换map中value(图片路径)为已加载好图片资源的图片本身
for (let[key, value] of this.map) {
//log(value)
var image = new Image()
image.src = value
this.map.set = (key, image)
//log('haha',this.map)
}
}
onLoaded(callback) {
let loadedCount = 0
//log('111')
for (let value of this.map.values()) {
//log(value)
//log(this.map.values())
//箭头函数,不传参数,其中this能直接指向ResourceLoader
value.onload = () => {
loadedCount++
if(loadedCount >= this.map.size) {
callback(this.map)
}
}
}
console.log('111')
}
//静态,可通过ResourceLoader.create()访问这个方法
static create() {
return new ResourceLoader()
}
}
牛奶老哥哥