@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
172 lines (141 loc) • 5.84 kB
JavaScript
Tiny.Object2D.prototype.renderCanvas = function (renderSession) {
if (this.visible === false || this.alpha === 0) return;
if (this._cacheAsBitmap) {
this._renderCachedSprite(renderSession);
return;
}
if (this._mask) {
renderSession.maskManager.pushMask(this._mask, renderSession);
}
for (var i = 0, j = this.children.length; i < j; i++) {
this.children[i].renderCanvas(renderSession);
}
if (this._mask) {
renderSession.maskManager.popMask(renderSession);
}
};
Tiny.Sprite.prototype.renderCanvas = function (renderSession) {
// If the sprite is not visible or the alpha is 0 then no need to render this element
if (
this.visible === false ||
this.alpha === 0 ||
this.renderable === false ||
this.texture.crop.width <= 0 ||
this.texture.crop.height <= 0
)
return;
if (this.blendMode !== renderSession.currentBlendMode) {
renderSession.currentBlendMode = this.blendMode;
renderSession.context.globalCompositeOperation = renderSession.blendModes[renderSession.currentBlendMode];
}
if (this._mask) {
renderSession.maskManager.pushMask(this._mask, renderSession);
}
// Ignore null sources
if (this.texture.valid) {
var resolution = this.texture.base.resolution / renderSession.resolution;
renderSession.context.globalAlpha = this.worldAlpha;
// If the texture is trimmed we offset by the trim x/y, otherwise we use the frame dimensions
var dx = this.texture.trim
? this.texture.trim.x - this.anchor.x * this.texture.trim.width
: this.anchor.x * -this.texture.frame.width;
var dy = this.texture.trim
? this.texture.trim.y - this.anchor.y * this.texture.trim.height
: this.anchor.y * -this.texture.frame.height;
// Allow for pixel rounding
if (renderSession.roundPixels) {
renderSession.context.setTransform(
this.worldTransform.a,
this.worldTransform.b,
this.worldTransform.c,
this.worldTransform.d,
(this.worldTransform.tx * renderSession.resolution) | 0,
(this.worldTransform.ty * renderSession.resolution) | 0
);
dx = dx | 0;
dy = dy | 0;
} else {
renderSession.context.setTransform(
this.worldTransform.a,
this.worldTransform.b,
this.worldTransform.c,
this.worldTransform.d,
this.worldTransform.tx * renderSession.resolution,
this.worldTransform.ty * renderSession.resolution
);
}
if (this.tint.int !== 0xffffff) {
if (this.cachedTint !== this.tint.int) {
this.cachedTint = this.tint.int;
this.tintedTexture = Tiny.CanvasTinter.getTintedTexture(this, this.tint);
}
renderSession.context.drawImage(
this.tintedTexture,
0,
0,
this.texture.crop.width,
this.texture.crop.height,
dx / resolution,
dy / resolution,
this.texture.crop.width / resolution,
this.texture.crop.height / resolution
);
} else {
renderSession.context.drawImage(
this.texture.base.source,
this.texture.crop.x,
this.texture.crop.y,
this.texture.crop.width,
this.texture.crop.height,
dx / resolution,
dy / resolution,
this.texture.crop.width / resolution,
this.texture.crop.height / resolution
);
}
}
// OVERWRITE
for (var i = 0; i < this.children.length; i++) {
this.children[i].renderCanvas(renderSession);
}
if (this._mask) {
renderSession.maskManager.popMask(renderSession);
}
};
Tiny.Text.prototype.renderCanvas = function (renderSession) {
if (this.dirty || this.resolution !== renderSession.resolution) {
this.resolution = renderSession.resolution;
this.updateText();
this.dirty = false;
}
Tiny.Sprite.prototype.renderCanvas.call(this, renderSession);
};
/**
* This function will draw the display object to the texture.
*
* @method renderCanvas
* @param displayObject {DisplayObject} The display object to render this texture on
* @param [matrix] {Matrix} Optional matrix to apply to the display object before rendering.
* @param [clear] {Boolean} If true the texture will be cleared before the displayObject is drawn
* @private
*/
Tiny.RenderTexture.prototype.renderCanvas = function (displayObject, matrix, clear) {
if (!this.valid) return;
var wt = displayObject.worldTransform;
wt.identity();
if (matrix) wt.append(matrix);
// setWorld Alpha to ensure that the object is renderer at full opacity
displayObject.worldAlpha = 1;
// Time to update all the children of the displayObject with the new matrix..
var children = displayObject.children;
for (var i = 0, j = children.length; i < j; i++) {
children[i].updateTransform();
}
wt.identity();
if (clear) this.textureBuffer.clear();
var context = this.textureBuffer.context;
var realResolution = this.renderer.resolution;
this.renderer.resolution = this.base.resolution;
this.renderer.renderObject(displayObject, context);
this.renderer.resolution = realResolution;
};