Show:

File: libs/OffCanvasRender.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) {
    /**
     * Canvas 渲染器
     * @class annie.OffCanvasRender
     * @extends annie.AObject
     * @implements IRender
     * @public
     * @since 1.0.0
     */
    var OffCanvasRender = /** @class */ (function (_super) {
        __extends(OffCanvasRender, _super);
        /**
         * @method OffCanvasRender
         * @public
         * @since 1.0.0
         */
        function OffCanvasRender() {
            var _this = _super.call(this) || this;
            _this._blendMode = 0;
            _this._instanceType = "annie.OffCanvasRender";
            return _this;
        }
        /**
         * 开始渲染时执行
         * @method begin
         * @since 1.0.0
         * @public
         */
        OffCanvasRender.prototype.begin = function (color) {
            var s = this, c = s.rootContainer, ctx = s._ctx;
            ctx.setTransform(1, 0, 0, 1, 0, 0);
            if (color == "") {
                ctx.clearRect(0, 0, c.width, c.height);
            }
        };
        /**
         * 开始有遮罩时调用
         * @method beginMask
         * @param {annie.DisplayObject} target
         * @public
         * @since 1.0.0
         */
        OffCanvasRender.prototype.beginMask = function (target) {
            var s = this, ctx = s._ctx;
            ctx.save();
            ctx.globalAlpha = 0;
            s.drawMask(target);
            ctx.clip();
        };
        OffCanvasRender.prototype.drawMask = function (target) {
            var s = this, tm = target._ocMatrix, ctx = s._ctx;
            ctx.setTransform(tm.a, tm.b, tm.c, tm.d, tm.tx, tm.ty);
            if (target._instanceType == "annie.Shape") {
                target._draw(ctx, true);
            }
            else if (target._instanceType == "annie.Sprite" || target._instanceType == "annie.MovieClip") {
                for (var i = 0; i < target.children.length; i++) {
                    s.drawMask(target.children[i]);
                }
            }
            else {
                var bounds = target._bounds;
                ctx.beginPath();
                ctx.rect(0, 0, bounds.width, bounds.height);
                ctx.closePath();
            }
        };
        /**
         * 结束遮罩时调用
         * @method endMask
         * @public
         * @since 1.0.0
         */
        OffCanvasRender.prototype.endMask = function () {
            this._ctx.restore();
        };
        /**
         * 调用渲染
         * @public
         * @since 1.0.0
         * @method draw
         * @param {annie.DisplayObject} target 显示对象
         */
        OffCanvasRender.prototype.draw = function (target) {
            var s = this;
            var texture = target._texture;
            if (texture.width == 0 || texture.height == 0)
                return;
            var ctx = s._ctx;
            var tm = target._ocMatrix;
            if (ctx.globalAlpha != target._ocAlpha) {
                ctx.globalAlpha = target._ocAlpha;
            }
            if (s._blendMode != target.blendMode) {
                ctx.globalCompositeOperation = annie.BlendMode.getBlendMode(target.blendMode);
                s._blendMode = target.blendMode;
            }
            ctx.setTransform(tm.a, tm.b, tm.c, tm.d, tm.tx, tm.ty);
            if (target._offsetX != 0 || target._offsetY != 0) {
                ctx.translate(target._offsetX, target._offsetY);
            }
            ctx.drawImage(texture, 0, 0);
        };
        OffCanvasRender.prototype.end = function () {
        };
        ;
        /**
         * 初始化渲染器
         * @public
         * @since 1.0.0
         * @method init
         */
        OffCanvasRender.prototype.init = function (canvas) {
            var s = this;
            s.rootContainer = canvas;
            s._ctx = canvas.getContext('2d');
        };
        /**
         * 当尺寸改变时调用
         * @public
         * @since 1.0.0
         * @method reSize
         */
        OffCanvasRender.prototype.reSize = function (width, height) {
            var s = this, c = s.rootContainer;
            c.width = width;
            c.height = height;
            c.style.width = Math.ceil(width / annie.devicePixelRatio) + "px";
            c.style.height = Math.ceil(height / annie.devicePixelRatio) + "px";
        };
        OffCanvasRender.prototype.destroy = function () {
            var s = this;
            s.rootContainer = null;
            s._ctx = null;
        };
        return OffCanvasRender;
    }(annie.AObject));
    annie.OffCanvasRender = OffCanvasRender;
})(annie || (annie = {}));