UNPKG

pixelbutler

Version:

Low-res bitmap render engine for big screens

65 lines (64 loc) 2.17 kB
'use strict'; var __extends = this.__extends || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } __.prototype = b.prototype; d.prototype = new __(); }; var Bitmap = require('./Bitmap'); var CanvasRenderer = require('./../render/CanvasRenderer'); var WebGLRenderer = require('./../render/WebGLRenderer'); var autosize = require('./autosize'); var Stage = (function (_super) { __extends(Stage, _super); function Stage(opts) { _super.call(this, (opts.width || 32), (opts.height || 32), !!opts.transparent); this.canvas = (typeof opts.canvas === 'string' ? document.getElementById(opts.canvas) : opts.canvas); if (!this.canvas) { throw new Error('cannot locate canvas with id "' + opts.canvas + '"'); } this.clear(); if (opts.renderer !== 'canvas') { try { this.renderer = new WebGLRenderer(this, this.canvas); } catch (e) { console.log(e); console.log('render init error, switching to fallback'); } } if (!this.renderer) { this.renderer = new CanvasRenderer(this, this.canvas); } if (opts.center || opts.scale) { this.autoSize = new autosize.AutoSize(this, { center: opts.center, scale: opts.scale }); } } Stage.prototype.resizeTo = function (width, height) { if (width === this.width && height === this.height) { return; } _super.prototype.resizeTo.call(this, width, height); if (this.autoSize) { this.autoSize.update(); } }; Stage.prototype.render = function () { this.renderer.update(); }; Stage.prototype.destruct = function () { if (this.autoSize) { this.autoSize.stop(); this.autoSize = null; } if (this.renderer) { this.renderer.destruct(); this.renderer = null; } this.canvas = null; }; return Stage; })(Bitmap); module.exports = Stage;