/* * FlexBitmap * * Copyright (c) 2014 Renato de Pontes Pereira. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ /** * @module Creatine **/ // namespace: this.creatine = this.creatine || {}; (function() { "use strict"; var Display = function(canvas) { this.initialize(canvas); } var p = Display.prototype = new createjs.EventDispatcher(); p.canvas = null; p.width = null; p.height = null; p.sourceWidth = null; p.sourceHeight = null; p.minWidth = null; p.minHeight = null; p.maxWidth = null; p.maxHeight = null; p.scaleMode = null; p.orientation = null; p.fullscreenScaleMode = null; p._fullscreenRequest = null; p._width = null; p._height = null; // ======================================================================== p.EventDispatcher_initialize = p.initialize; p.initialize = function(canvas) { this.EventDispatcher_initialize(); this.canvas = canvas; this.width = canvas.width; this.height = canvas.height; this.sourceWidth = canvas.width; this.sourceHeight = canvas.height; this.scaleMode = creatine.NOSCALE; this.orientation = 0; this.fullscreenScaleMode = creatine.FIT; if (window['orientation']) { this.orientation = window['orientation']; } else if (window.outerWidth > window.outerHeight) { this.orientation = 90; } // CHECK FULLSCREEN var fs = [ 'requestFullscreen', 'webkitRequestFullscreen', 'msRequestFullscreen', 'mozRequestFullScreen', ]; for (var i=0; i this.width) { this.width = this.maxWidth; } if (this.minHeight && this.minHeight < this.height) { this.height = this.minHeight; } if (this.maxHeight && this.maxHeight > this.height) { this.height = this.maxHeight; } this.canvas.width = this.width; this.canvas.height = this.height; } p.resizeFit = function() { var multiplier = Math.min( (window.innerHeight/this.sourceHeight), (window.innerWidth /this.sourceWidth) ); this.width = Math.round(this.sourceWidth * multiplier); this.height = Math.round(this.sourceHeight * multiplier); this.canvas.width = this.width; this.canvas.height = this.height; } p.resizeOriginal = function() { this.width = this.sourceWidth; this.height = this.sourceHeight; this.canvas.width = this.sourceWidth; this.canvas.height = this.sourceHeight; } // ======================================================================== // ORIENTATION // ======================================================================== p.isPortrait = function() { return this.orientation === 0 || this.orientation == 180; } p.isLandscape = function() { return this.orientation === 90 || this.orientation === -90; } // ======================================================================== // FULLSCREEN // ======================================================================== p.isFullscreen = function() { return (document['fullscreenElement'] || document['mozFullScreenElement'] || document['webkitFullscreenElement']); } p.startFullscreen = function () { if (!this._fullscreenRequest || this.isFullscreen()) { return; } this._width = this.width; this._height = this.height; if (window['Element'] && Element['ALLOW_KEYBOARD_INPUT']) { this.canvas[this._fullscreenRequest](Element.ALLOW_KEYBOARD_INPUT); } else { this.canvas[this._fullscreenRequest](); } } p.stopFullscreen = function () { if (!this.isFullscreen()) { return; } if (document.cancelFullScreen) { document.cancelFullScreen(); } else if (document.exitFullscreen) { document.exitFullscreen(); } else if (document.webkitCancelFullScreen) { document.webkitCancelFullScreen(); } else if (document.webkitExitFullscreen) { document.webkitExitFullscreen(); } else if (document.msCancelFullScreen) { document.msCancelFullScreen(); } else if (document.msExitFullscreen) { document.msExitFullscreen(); } else if (document.mozCancelFullScreen) { document.mozCancelFullScreen(); } else if (document.mozExitFullscreen) { document.mozExitFullscreen(); } } // ======================================================================== // EVENTS // ======================================================================== p.onResize = function(event) { if (this.scaleMode !== creatine.NOSCALE) { this.refresh(); } this.dispatchEvent('resize'); } p.onOrientation = function(event) { this.orientation = window['orientation']; this.dispatchEvent('orientation'); if (this.isPortrait()) { this.dispatchEvent('enterportrait'); } else { this.dispatchEvent('enterlandscape'); } } p.onFullscreen = function(event) { if (this.isFullscreen()) { this.refresh(); this.dispatchEvent('enterfullscreen'); } else { this.width = this._width; this.height = this._height; this.canvas.width = this._width; this.canvas.height = this._height; this.refresh(); this.dispatchEvent('exitfullscreen'); } } creatine.Display = Display; }());