UNPKG

pixelbutler

Version:

Low-res bitmap render engine for big screens

105 lines (104 loc) 3.89 kB
'use strict'; var browser = require('./browser'); function assertMode(scaleMode) { if ((typeof scaleMode === 'number' && scaleMode > 0) || scaleMode === 'max' || scaleMode === 'fit' || scaleMode === 'none') { return; } var int = parseInt(scaleMode, 10); if (!isNaN(int) && int > 0) { return; } throw new Error('bad scaleMode: ' + scaleMode); } var AutoSize = (function () { function AutoSize(stage, opts) { var _this = this; this.stage = stage; opts = opts || {}; this.centerView = !!opts.center; this.scaleMode = opts.scale || 'none'; assertMode(this.scaleMode); stage.canvas.style.position = 'absolute'; this.update = function (event) { var viewPort = browser.getViewport(); if (_this.scaleMode === 'fit') { _this.scaleFit(viewPort); } else if (_this.scaleMode === 'max') { _this.scaleAspect(viewPort); } else { _this.stage.renderer.resize(); } if (_this.centerView || _this.scaleMode === 'max') { _this.moveScreenCenter(viewPort); } else { _this.moveScreenTo(0, 0); } }; this.setMode(this.scaleMode, this.centerView); } AutoSize.prototype.scale = function (mode) { this.setMode(mode, this.centerView); }; AutoSize.prototype.center = function (center) { if (typeof center === "undefined") { center = true; } this.setMode(this.scaleMode, center); }; AutoSize.prototype.resize = function () { this.update(); }; AutoSize.prototype.stop = function () { this.unlisten(); }; AutoSize.prototype.scaleTo = function (width, height) { this.scaleMode = 'none'; this.stage.canvas.width = width; this.stage.canvas.height = height; this.stage.renderer.resize(); }; AutoSize.prototype.scaleFit = function (viewPort) { this.stage.canvas.width = viewPort.width; this.stage.canvas.height = viewPort.height; this.stage.renderer.resize(); }; AutoSize.prototype.scaleAspect = function (viewPort) { var ratio = Math.min(viewPort.width / this.stage.width, viewPort.height / this.stage.height); this.stage.canvas.width = Math.floor(this.stage.width * ratio); this.stage.canvas.height = Math.floor(this.stage.height * ratio); this.stage.renderer.resize(); }; AutoSize.prototype.moveScreenTo = function (x, y) { this.stage.canvas.style.left = x + 'px'; this.stage.canvas.style.top = y + 'px'; }; AutoSize.prototype.moveScreenCenter = function (viewPort) { this.moveScreenTo(Math.floor((viewPort.width - this.stage.canvas.width) / 2), Math.floor((viewPort.height - this.stage.canvas.height) / 2)); }; AutoSize.prototype.listen = function () { this.unlisten(); if (this.centerView || this.scaleMode === 'fit') { window.addEventListener('resize', this.update); } }; AutoSize.prototype.unlisten = function () { window.removeEventListener('resize', this.update); }; AutoSize.prototype.setMode = function (mode, center) { assertMode(mode); this.scaleMode = mode; var multi = parseInt(this.scaleMode, 10); if (!isNaN(multi)) { this.scaleMode = multi; this.scaleTo(Math.floor(this.stage.width * multi), Math.floor(this.stage.height * multi)); this.unlisten(); } if (this.scaleMode === 'fit') { this.moveScreenTo(0, 0); } if (center || this.scaleMode === 'fit' || this.scaleMode === 'max') { this.listen(); } this.update(); }; return AutoSize; })(); exports.AutoSize = AutoSize;