API Docs for: 3.0.0
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|other} 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._cacheImg = null;
            _this._bitmapData = null;
            /**
             * <h4><font color="red">小游戏不支持 小程序不支持</font></h4>
             * 是否对图片对象使用像素碰撞检测透明度,默认关闭
             * @property hitTestWidthPixel
             * @type {boolean}
             * @default false
             * @since 1.1.0
             */
            _this.hitTestWidthPixel = false;
            var s = _this;
            s._instanceType = "annie.Bitmap";
            if (bitmapData.boundsRowAndCol != void 0) {
                s.boundsRow = bitmapData.boundsRowAndCol[0];
                s.boundsCol = bitmapData.boundsRowAndCol[1];
            }
            s._bitmapData = bitmapData;
            var bw = bitmapData.width;
            var bh = bitmapData.height;
            s._bounds.width = bw;
            s._bounds.height = bh;
            s._updateSplitBounds();
            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._bitmapData = value;
                    if (value == void 0) {
                        s._cacheImg = null;
                        s._texture = null;
                        s._bounds.width = 0;
                        s._bounds.height = 0;
                    }
                    else {
                        s.a2x_uf = true;
                    }
                }
            },
            enumerable: true,
            configurable: true
        });
        Bitmap.prototype.updateMatrix = function () {
            var s = this;
            var bitmapData = s._bitmapData;
            if (bitmapData == void 0) {
                return;
            }
            var bw = bitmapData.width;
            var bh = bitmapData.height;
            _super.prototype.updateMatrix.call(this);
            //滤镜,这里一定是UF
            if (s.a2x_uf && bw * bh > 0) {
                var cf = s.cFilters;
                var cfLen = cf.length;
                if (cfLen > 0) {
                    if (!(s._cacheImg instanceof Object)) {
                        s._cacheImg = window.document.createElement("canvas");
                        s._cacheImg.width = bw;
                        s._cacheImg.height = bh;
                    }
                    var _canvas = s._cacheImg;
                    var ctx = _canvas.getContext("2d");
                    ctx.clearRect(0, 0, bw, bh);
                    ctx.shadowBlur = 0;
                    ctx.shadowColor = "#0";
                    ctx.shadowOffsetX = 0;
                    ctx.shadowOffsetY = 0;
                    ////////////////////
                    ctx.drawImage(bitmapData, 0, 0, bw, bh, 0, 0, bw, bh);
                    /////////////////////
                    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 = s._cacheImg;
                }
                else {
                    s._texture = bitmapData;
                }
            }
            if (s._bounds.width != bw || s._bounds.height != bh) {
                s._bounds.width = bw;
                s._bounds.height = bh;
                s._updateSplitBounds();
                s._checkDrawBounds();
            }
            else if (s.a2x_um) {
                s._checkDrawBounds();
            }
            s.a2x_uf = false;
            s.a2x_um = false;
            s.a2x_ua = false;
        };
        /**
         * <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.hitTestPoint = function (hitPoint, isGlobalPoint) {
            if (isGlobalPoint === void 0) { isGlobalPoint = false; }
            var s = this;
            if (s.hitTestWidthPixel) {
                var texture = s._texture;
                if (texture.width == 0) {
                    return null;
                }
                var p = void 0;
                if (isGlobalPoint) {
                    p = s.globalToLocal(hitPoint);
                }
                else {
                    p = hitPoint;
                }
                var _canvas = annie.DisplayObject._canvas, ctx = _canvas.getContext('2d');
                _canvas.width = 1;
                _canvas.height = 1;
                ctx.clearRect(0, 0, 1, 1);
                ctx.setTransform(1, 0, 0, 1, -p.x, -p.y);
                ctx.drawImage(texture, 0, 0);
                if (ctx.getImageData(0, 0, 1, 1).data[3] > 0) {
                    return s;
                }
                else {
                    return null;
                }
            }
            else {
                return _super.prototype.hitTestPoint.call(this, hitPoint, isGlobalPoint);
            }
        };
        Bitmap.prototype.destroy = function () {
            //清除相应的数据引用
            var s = this;
            _super.prototype.destroy.call(this);
            s._bitmapData = null;
            s._cacheImg = null;
        };
        return Bitmap;
    }(annie.DisplayObject));
    annie.Bitmap = Bitmap;
})(annie || (annie = {}));