@spibo-studio/spibo-studio
Version:
Spibo Studio is an HTML Canvas Library for creating adventure games
91 lines • 3.75 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var Boundaries_1 = __importDefault(require("./Boundaries"));
var KeyboardInteractions_1 = __importDefault(require("./KeyboardInteractions"));
var Canvas = /** @class */ (function () {
function Canvas(width, height, boundaries, framerate) {
if (framerate === void 0) { framerate = 40; }
this.background = null;
this.mainCharacter = null;
this.refreshIntervalTime = 100;
this.canvas = document.createElement('canvas');
this.canvas.id = 'game-board';
this.canvas.width = width;
this.canvas.height = height;
if (boundaries) {
this.boundaries = boundaries;
}
else {
this.boundaries = new Boundaries_1.default(0, this.width, this.height, 0);
}
this.context = this.canvas.getContext('2d');
document.body.appendChild(this.canvas);
// Listen for keyboard input
// eslint-disable-next-line @typescript-eslint/no-this-alias
var self = this;
this.framerate = framerate;
this.refreshInterval = setInterval(function () { return self.refresh(); });
this.keyboardInteractions = new KeyboardInteractions_1.default(this);
}
Canvas.prototype.reset = function () {
if (this.context) {
this.context.setTransform(1, 0, 0, 1, 0, 0);
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
};
Canvas.prototype.draw = function () {
if (this.context) {
if (this.background && this.background.pos) {
var xScale = void 0;
var yScale = void 0;
if (this.background.scrollable) {
xScale = this.background.width;
yScale = this.background.height;
}
else {
xScale = this.canvas.width;
yScale = this.canvas.height;
}
this.context.drawImage(this.background.image, this.background.pos.x, this.background.pos.y, this.background.width, this.background.height, 0, 0, xScale, yScale);
}
if (this.mainCharacter && this.mainCharacter.activeSprite && this.mainCharacter.pos) {
this.context.drawImage(this.mainCharacter.image, this.mainCharacter.activeSprite.pos.x, this.mainCharacter.activeSprite.pos.y, this.mainCharacter.activeSprite.width, this.mainCharacter.activeSprite.height, this.mainCharacter.pos.x, this.mainCharacter.pos.y, this.mainCharacter.activeSprite.width, this.mainCharacter.activeSprite.height);
}
}
};
Canvas.prototype.refresh = function () {
this.reset();
this.draw();
};
Object.defineProperty(Canvas.prototype, "height", {
get: function () {
return this.canvas.height;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Canvas.prototype, "width", {
get: function () {
return this.canvas.width;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Canvas.prototype, "framerate", {
get: function () {
return this._framerate;
},
set: function (framerate) {
this._framerate = framerate;
this.refreshIntervalTime = Math.round(1000 / this.framerate);
},
enumerable: false,
configurable: true
});
return Canvas;
}());
exports.default = Canvas;
//# sourceMappingURL=Canvas.js.map