@fuwu-yuan/bgew
Version:
Baguette Game Engine Web is a french 2D Web game engine
45 lines (44 loc) • 1.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Image = void 0;
const entity_1 = require("../entity");
class Image extends entity_1.Entity {
constructor(src, x, y, width = 0, height = 0, sourceX = null, sourceY = null, sourceW = null, sourceH = null, isVideo = false) {
super(x, y, width, height);
this._sourceX = sourceX;
this._sourceY = sourceY;
this._sourceW = sourceW;
this._sourceH = sourceH;
this._image = document.createElement(isVideo ? "video" : "img");
if (isVideo) {
this._image.addEventListener("ended", () => { this._image.play(); });
document.addEventListener("click", () => { this._image.play(); }, { once: true });
}
this._image.src = src;
}
draw(ctx) {
super.draw(ctx);
if (this.width === 0 || this.height === 0) {
ctx.drawImage(this.image, this.x, this.y);
}
else if (this._sourceX === null || this._sourceY === null || this._sourceW === null || this._sourceH === null) {
ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
else {
ctx.imageSmoothingEnabled = false;
ctx.drawImage(this.image, this._sourceX, this._sourceY, this._sourceW, this._sourceH, this.x, this.y, this.width, this.height);
ctx.imageSmoothingEnabled = true;
}
}
update(delta) {
super.update(delta);
if (this.width === 0 || this.height === 0) {
this.width = this.image.width;
this.height = this.image.height;
}
}
get image() {
return this._image;
}
}
exports.Image = Image;