hook-miner/src/image.js

34 lines
570 B
JavaScript

import Resource from './resource'
import { ctx } from './canvas'
let imageCache = {}
class Image {
constructor (file, img) {
this.file = file
this.img = img
}
static async load (file) {
if (imageCache[file]) return imageCache[file]
let img = await Resource.loadImage(file)
let imgCl = new Image(file, img)
imageCache[file] = imgCl
return imgCl
}
get width () {
return this.img.width
}
get height () {
return this.img.height
}
draw (x, y, w, h) {
ctx.drawImage(this.img, x, y, w, h)
}
}
export { Image }