/* * 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"; /** * FlexBitmap extends the createjs.Bitmap and adds the layout function to it, * thus, FlexBitmap can resize itself to best fit its position and area in a * layout manager, such as the BoxSizer and the GridSizer. * * @class FlexBitmap * @extends createjs.DisplayObject * @constructor * @param {Image|HTMLCanvasElement|HTMLVideoElement|String} imageOrUri The * source object or URI to an image to display. This can be either an * Image, Canvas, or Video object, or a string URI to an image file to * load and use. If it is a URI, a new Image object will be constructed * and assigned to the .image property. * @param {boolean} keepAspectRatio true if the resize must keep the aspect * ratio. Default to true. **/ var FlexBitmap = function(imageOrUri, keepAspectRatio) { this.initialize(imageOrUri, keepAspectRatio); } var p = FlexBitmap.prototype = new createjs.Bitmap(); /** * Indicates if the image must keep the aspect ratio when resizing. * * @property keepAspectRatio * @type {boolean} **/ p.keepAspectRatio = null; p.Bitmap_initialize = p.initialize; /** * Initialization method. * * @method initialize * @param {Image|HTMLCanvasElement|HTMLVideoElement|String} imageOrUri The * source object or URI to an image to display. This can be either * an Image, Canvas, or Video object, or a string URI to an image * file to load and use. If it is a URI, a new Image object will be * constructed and assigned to the .image property. * @param {boolean} keepAspectRatio true if the resize must keep the aspect * ratio. Default to true. * @protected **/ p.initialize = function(imageOrUri, keepAspectRatio) { this.Bitmap_initialize(imageOrUri); if (typeof(keepAspectRatio)==='undefined' || keepAspectRatio == null) { keepAspectRatio = true; } this.keepAspectRatio = keepAspectRatio } /** * Resize this this image to fit the area. * * @method layout * @param {createjs.Rectangle} area A rectangle containing the usable area * of the bitmap. **/ p.layout = function(area) { var width = this.image.width; var height = this.image.height; if (this.keepAspectRatio) { var size = creatine.scaleAspectRatio( width, height, area.width, area.height ); this.scaleX = size[0]/width; this.scaleY = size[1]/height; } else { this.scaleX = area.width/width; this.scaleY = area.height/height; } } /** * Returns a rectangle representing this object's bounds after the resize. * * @method getBounds * @protected **/ p.getBounds = function() { var w = this.image.width*this.scaleX; var h = this.image.height*this.scaleY; return this._rectangle.initialize(0, 0, w, h); } creatine.FlexBitmap = FlexBitmap; }());