Show:

File: libs/bitmap.js

var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/**
 * @module annie
 */
var annie;
(function (annie) {
    /**
     * 利用 Bitmap() 构造函数,可以创建包含对 BitmapData 对象的引用的 Bitmap 对象。
     * 创建了 Bitmap 对象后,使用父 Sprite 实例的 addChild() 或 addChildAt() 方法将位图放在显示列表中。
     * @class annie.Bitmap
     * @public
     * @extends annie.DisplayObject
     * @since 1.0.0
     */
    var Bitmap = /** @class */ (function (_super) {
        __extends(Bitmap, _super);
        /**
         * 构造函数
         * @method Bitmap
         * @since 1.0.0
         * @public
         * @param {Image|Video|Canvas} bitmapData 一个HTMl Image的实例,小程序或者小游戏里则只能是一个图片的地址
         * @param {annie.Rectangle} rect 设置显示Image的区域,不设置值则全部显示Image的内容,小程序或者小游戏里没有这个参数
         * @example
         *      //html5
         *      var imgEle=new Image();
         *      imgEle.onload=function (e) {
         *          var bitmap = new annie.Bitmap(imgEle)
         *          //居中对齐
         *          bitmap.x = (s.stage.desWidth - bitmap.width) / 2;
         *          bitmap.y = (s.stage.desHeight - bitmap.height) / 2;
         *          s.addChild(bitmap);
         *          //截取图片的某一部分显示
         *          var rect = new annie.Rectangle(0, 0, 200, 200),
         *          rectBitmap = new annie.Bitmap(imgEle, rect);
         *          rectBitmap.x = (s.stage.desWidth - bitmap.width) / 2;
         *          rectBitmap.y = 100;
         *          s.addChild(rectBitmap);
         *      }
         *      imgEle.src='http://test.annie2x.com/test.jpg';
         *      //小程序或者小游戏
         *      var imgEle="http://test.annie2x.com/test.jpg";
         *      var bitmap=new annie.Bitmap(imgEle);
         *      s.addChild(bitmap);
         *
         * <p><a href="http://test.annie2x.com/annie/Bitmap/index.html" target="_blank">测试链接</a></p>
         */
        function Bitmap(bitmapData) {
            var _this = _super.call(this) || this;
            _this._cacheCanvas = null;
            _this._bitmapData = null;
            var s = _this;
            s._instanceType = "annie.Bitmap";
            s.bitmapData = bitmapData;
            return _this;
        }
        Object.defineProperty(Bitmap.prototype, "bitmapData", {
            /**
             * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
             * HTML的一个Image对象或者是canvas对象或者是video对象
             * @property bitmapData
             * @public
             * @since 1.0.0
             * @type {any}
             * @default null
             */
            get: function () {
                return this._bitmapData;
            },
            set: function (value) {
                var s = this;
                if (value != s._bitmapData) {
                    s.clearBounds();
                    s._bitmapData = value;
                    s._texture = value;
                }
            },
            enumerable: true,
            configurable: true
        });
        Bitmap.prototype._updateMatrix = function (isOffCanvas) {
            if (isOffCanvas === void 0) { isOffCanvas = false; }
            _super.prototype._updateMatrix.call(this, isOffCanvas);
            var s = this;
            var texture = s._bitmapData;
            if (!texture || texture.width == 0 || texture.height == 0) {
                s._texture = null;
                return;
            }
            var bw = texture.width;
            var bh = texture.height;
            if (s._bounds.width != bw || s._bounds.height != bh) {
                s._bounds.width = bw;
                s._bounds.height = bh;
                s._updateSplitBounds();
                s._checkDrawBounds();
                if (s._filters.length > 0) {
                    s.a2x_uf = true;
                }
                s._texture = texture;
            }
            else if (s.a2x_um) {
                s._checkDrawBounds();
            }
            if (!isOffCanvas) {
                s.a2x_um = false;
                s.a2x_ua = false;
            }
            if (s.a2x_uf) {
                s.a2x_uf = false;
                if (!s._cacheCanvas) {
                    s._cacheCanvas = document.createElement("canvas");
                }
                var canvas = s._cacheCanvas;
                canvas.width = bw;
                canvas.heigth = bh;
                canvas.style.width = Math.ceil(bw / annie.devicePixelRatio) + "px";
                canvas.style.height = Math.ceil(bh / annie.devicePixelRatio) + "px";
                var ctx = canvas.getContext("2d");
                ctx.clearRect(0, 0, bw, bh);
                ////////////////////
                ctx.drawImage(texture, 0, 0);
                /////////////////////
                var cf = s._filters;
                var cfLen = cf.length;
                if (cfLen > 0) {
                    var imageData = ctx.getImageData(0, 0, bw, bh);
                    for (var i = 0; i < cfLen; i++) {
                        cf[i].drawFilter(imageData);
                    }
                    ctx.putImageData(imageData, 0, 0);
                    s._texture = canvas;
                }
                else {
                    s._texture = texture;
                }
            }
        };
        /**
         * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
         * 从Bitmap中剥离出单独的小图以供特殊用途
         * @method convertToImage
         * @static
         * @public
         * @since 1.0.0
         * @param {annie.Bitmap} bitmap
         * @param {annie.Rectangle} rect 截图范围
         * @param {boolean} isNeedImage 是否一定要返回img,如果不为true则有时返回的是canvas
         * @return {Canvas|BitmapData}
         * @example
         *      var spriteSheetImg = new Image(),
         *      rect = new annie.Rectangle(0, 0, 200, 200),
         *      yourBitmap = new annie.Bitmap(spriteSheetImg, rect);
         *      spriteSheetImg.onload=function(e){
         *          var singleSmallImg = annie.Bitmap.convertToImage(yourBitmap);//convertToImage是annie.Bitmap的一个静态方法
         *          console.log(singleSmallImg);
         *      }
         *      spriteSheetImg.src = 'http://test.annie2x.com/test.jpg';
         */
        Bitmap.convertToImage = function (bitmap, rect, isNeedImage) {
            if (isNeedImage === void 0) { isNeedImage = true; }
            var _canvas;
            if (isNeedImage) {
                _canvas = annie.DisplayObject._canvas;
            }
            else {
                _canvas = window.document.createElement("canvas");
            }
            var w = rect.width, h = rect.height, ctx = _canvas.getContext("2d");
            _canvas.width = w;
            _canvas.height = h;
            ctx.drawImage(bitmap.bitmapData, rect.x, rect.y, w, h, 0, 0, w, h);
            if (isNeedImage) {
                var img = new Image();
                img.src = _canvas.toDataURL();
                return img;
            }
            else {
                return _canvas;
            }
        };
        Bitmap.prototype.destroy = function () {
            //清除相应的数据引用
            var s = this;
            _super.prototype.destroy.call(this);
            s._bitmapData = null;
            s._cacheCanvas = null;
        };
        return Bitmap;
    }(annie.DisplayObject));
    annie.Bitmap = Bitmap;
})(annie || (annie = {}));