var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
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 = (function (_super) {
__extends(OffCanvasRender, _super);
/**
* @method OffCanvasRender
* @param {annie.Stage} stage
* @public
* @since 1.0.0
*/
function OffCanvasRender() {
_super.call(this);
/**
* 渲染器所在最上层的对象
* @property canvas
* @public
* @since 1.0.0
* @type {any}
* @default null
*/
this.canvas = null;
this._blendMode = 0;
this._instanceType = "annie.OffCanvasRender";
}
/**
* 开始渲染时执行
* @method begin
* @since 1.0.0
* @public
*/
OffCanvasRender.prototype.begin = function (color) {
var s = this, c = s.canvas, ctx = s._ctx;
ctx.globalAlpha = 1;
ctx.setTransform(1, 0, 0, 1, 0, 0);
if (color == "") {
ctx.clearRect(0, 0, c.width, c.height);
}
else {
ctx.fillStyle = color;
ctx.fillRect(0, 0, c.width, c.height);
}
s.isFirstObj = true;
};
/**
* 开始有遮罩时调用
* @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;
ctx.beginPath();
s.drawMask(target);
ctx.closePath();
ctx.clip();
};
OffCanvasRender.prototype.drawMask = function (target) {
var s = this, tm = target._cMatrix, 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.rect(0, 0, bounds.width, bounds.height);
}
};
/**
* 结束遮罩时调用
* @method endMask
* @public
* @since 1.0.0
*/
OffCanvasRender.prototype.endMask = function () {
this._ctx.restore();
};
OffCanvasRender.prototype.render = function (target) {
var s = this;
var isFistObj = s.isFirstObj;
if (s.isFirstObj) {
s.isFirstObj = false;
}
if (target._visible) {
var children = target.children;
var texture = target._texture;
var ctx = s._ctx, tm = target._matrix;
ctx.save();
if (!isFistObj) {
ctx.globalAlpha *= target._alpha;
ctx.transform(tm.a, tm.b, tm.c, tm.d, tm.tx, tm.ty);
ctx.translate(target._offsetX, target._offsetY);
}
else {
ctx.translate(-target._offsetX, -target._offsetY);
}
if (texture != null && !isFistObj) {
if (texture.width == 0 || texture.height == 0) {
return;
}
var cf_1 = target._filters;
var cfLen_1 = cf_1.length;
var fId = -1;
if (cfLen_1) {
for (var i = 0; i < cfLen_1; i++) {
if (target._filters[i].type == "Shadow") {
fId = i;
break;
}
}
}
if (fId >= 0) {
var ctx_1 = this._ctx;
ctx_1.shadowBlur = cf_1[fId].blur;
ctx_1.shadowColor = cf_1[fId].color;
ctx_1.shadowOffsetX = cf_1[fId].offsetX;
ctx_1.shadowOffsetY = cf_1[fId].offsetY;
}
if (s._blendMode != target.blendMode) {
ctx.globalCompositeOperation = annie.BlendMode.getBlendMode(target.blendMode);
s._blendMode = target.blendMode;
}
ctx.drawImage(texture, 0, 0);
}
else if (children != void 0) {
var maskObj = void 0;
var child = void 0;
var len = children.length;
for (var i = 0; i < len; i++) {
child = children[i];
if (child._isUseToMask > 0) {
continue;
}
if (maskObj != null) {
if (child.mask != null && child.mask.parent == child.parent) {
if (child.mask != maskObj) {
s.endMask();
maskObj = child.mask;
s.beginMask(maskObj);
}
}
else {
s.endMask();
maskObj = null;
}
}
else {
if (child.mask != null && child.mask.parent == child.parent) {
maskObj = child.mask;
s.beginMask(maskObj);
}
}
s.render(child);
}
if (maskObj != null) {
s.endMask();
}
}
ctx.restore();
//看看是否有滤镜
var cf = target._filters;
var cfLen = cf.length;
if (cfLen > 0) {
var imageData = ctx.getImageData(0, 0, target._bounds.width, target._bounds.height);
for (var i = 0; i < cfLen; i++) {
cf[i].drawFilter(imageData);
}
ctx.putImageData(imageData, 0, 0);
}
}
};
OffCanvasRender.prototype.end = function () { };
;
/**
* 初始化渲染器
* @public
* @since 1.0.0
* @method init
*/
OffCanvasRender.prototype.init = function (canvas) {
var s = this;
s.canvas = canvas;
s._ctx = canvas.getContext('2d');
};
/**
* 当尺寸改变时调用
* @public
* @since 1.0.0
* @method reSize
*/
OffCanvasRender.prototype.reSize = function (width, height) {
var s = this, c = s.canvas;
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.canvas = null;
s._ctx = null;
};
return OffCanvasRender;
}(annie.AObject));
annie.OffCanvasRender = OffCanvasRender;
})(annie || (annie = {}));