Updated graphics cacheAsBitmap to use getLocalBounds
Tweaks to getLocalBounds
Tweaks to getBounds
removed unnecessary variable declaration in updateTransform
This commit is contained in:
Mat Groves 2014-02-08 18:39:44 +00:00
parent c01aebc5ec
commit 312976e2aa
6 changed files with 113 additions and 74 deletions

View file

@ -4,7 +4,7 @@
* Copyright (c) 2012-2014, Mat Groves * Copyright (c) 2012-2014, Mat Groves
* http://goodboydigital.com/ * http://goodboydigital.com/
* *
* Compiled: 2014-02-04 * Compiled: 2014-02-08
* *
* pixi.js is licensed under the MIT License. * pixi.js is licensed under the MIT License.
* http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/mit-license.php
@ -911,9 +911,6 @@ PIXI.DisplayObject.prototype.updateTransform = function()
// TODO OPTIMIZE THIS!! with dirty // TODO OPTIMIZE THIS!! with dirty
if(this.rotation !== this.rotationCache) if(this.rotation !== this.rotationCache)
{ {
if(isNaN(parseFloat(this.rotation)))
throw new Error('DisplayObject rotation values must be numeric.');
this.rotationCache = this.rotation; this.rotationCache = this.rotation;
this._sr = Math.sin(this.rotation); this._sr = Math.sin(this.rotation);
this._cr = Math.cos(this.rotation); this._cr = Math.cos(this.rotation);
@ -932,16 +929,16 @@ PIXI.DisplayObject.prototype.updateTransform = function()
a11 = this._cr * this.scale.y, a11 = this._cr * this.scale.y,
a02 = this.position.x + a00 * px - py * a01, a02 = this.position.x + a00 * px - py * a01,
a12 = this.position.y + a11 * py - px * a10, a12 = this.position.y + a11 * py - px * a10,
b00 = parentTransform.a, b01 = parentTransform.b, b02 = parentTransform.tx, b00 = parentTransform.a, b01 = parentTransform.b,
b10 = parentTransform.c, b11 = parentTransform.d, b12 = parentTransform.ty; b10 = parentTransform.c, b11 = parentTransform.d;
worldTransform.a = b00 * a00 + b01 * a10; worldTransform.a = b00 * a00 + b01 * a10;
worldTransform.b = b00 * a01 + b01 * a11; worldTransform.b = b00 * a01 + b01 * a11;
worldTransform.tx = b00 * a02 + b01 * a12 + b02; worldTransform.tx = b00 * a02 + b01 * a12 + parentTransform.tx;
worldTransform.c = b10 * a00 + b11 * a10; worldTransform.c = b10 * a00 + b11 * a10;
worldTransform.d = b10 * a01 + b11 * a11; worldTransform.d = b10 * a01 + b11 * a11;
worldTransform.ty = b10 * a02 + b11 * a12 + b12; worldTransform.ty = b10 * a02 + b11 * a12 + parentTransform.ty;
this.worldAlpha = this.alpha * this.parent.worldAlpha; this.worldAlpha = this.alpha * this.parent.worldAlpha;
}; };
@ -952,8 +949,9 @@ PIXI.DisplayObject.prototype.updateTransform = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.DisplayObject.prototype.getBounds = function() PIXI.DisplayObject.prototype.getBounds = function( matrix )
{ {
matrix = matrix;//just to get passed js hinting (and preserve inheritance)
return PIXI.EmptyRectangle; return PIXI.EmptyRectangle;
}; };
@ -965,17 +963,9 @@ PIXI.DisplayObject.prototype.getBounds = function()
*/ */
PIXI.DisplayObject.prototype.getLocalBounds = function() PIXI.DisplayObject.prototype.getLocalBounds = function()
{ {
var matrixCache = this.worldTransform; //var matrixCache = this.worldTransform;
this.worldTransform = PIXI.identityMatrix; return this.getBounds(PIXI.identityMatrix);///PIXI.EmptyRectangle();
this.updateTransform();
var bounds = this.getBounds();
this.worldTransform = matrixCache;
return bounds;
}; };
/** /**
@ -1249,12 +1239,18 @@ PIXI.DisplayObjectContainer.prototype.updateTransform = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.DisplayObjectContainer.prototype.getBounds = function() PIXI.DisplayObjectContainer.prototype.getBounds = function(matrix)
{ {
if(this.children.length === 0)return PIXI.EmptyRectangle; if(this.children.length === 0)return PIXI.EmptyRectangle;
// TODO the bounds have already been calculated this render session so return what we have // TODO the bounds have already been calculated this render session so return what we have
if(matrix)
{
var matrixCache = this.worldTransform;
this.worldTransform = matrix;
this.updateTransform();
this.worldTransform = matrixCache;
}
var minX = Infinity; var minX = Infinity;
var minY = Infinity; var minY = Infinity;
@ -1276,7 +1272,7 @@ PIXI.DisplayObjectContainer.prototype.getBounds = function()
childVisible = true; childVisible = true;
childBounds = this.children[i].getBounds(); childBounds = this.children[i].getBounds( matrix );
minX = minX < childBounds.x ? minX : childBounds.x; minX = minX < childBounds.x ? minX : childBounds.x;
minY = minY < childBounds.y ? minY : childBounds.y; minY = minY < childBounds.y ? minY : childBounds.y;
@ -1304,6 +1300,24 @@ PIXI.DisplayObjectContainer.prototype.getBounds = function()
return bounds; return bounds;
}; };
PIXI.DisplayObjectContainer.prototype.getLocalBounds = function()
{
var matrixCache = this.worldTransform;
this.worldTransform = PIXI.identityMatrix;
for(var i=0,j=this.children.length; i<j; i++)
{
this.children[i].updateTransform();
}
var bounds = this.getBounds();
this.worldTransform = matrixCache;
return bounds;
};
/** /**
* Sets the container's stage reference, the stage this object is connected to * Sets the container's stage reference, the stage this object is connected to
* *
@ -1593,7 +1607,7 @@ PIXI.Sprite.prototype.onTextureUpdate = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.Sprite.prototype.getBounds = function() PIXI.Sprite.prototype.getBounds = function( matrix )
{ {
var width = this.texture.frame.width; var width = this.texture.frame.width;
@ -1605,7 +1619,7 @@ PIXI.Sprite.prototype.getBounds = function()
var h0 = height * (1-this.anchor.y); var h0 = height * (1-this.anchor.y);
var h1 = height * -this.anchor.y; var h1 = height * -this.anchor.y;
var worldTransform = this.worldTransform; var worldTransform = matrix || this.worldTransform ;
var a = worldTransform.a; var a = worldTransform.a;
var b = worldTransform.c; var b = worldTransform.c;
@ -1850,6 +1864,7 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession)
} }
}; };
// some helper functions.. // some helper functions..
/** /**
@ -2543,7 +2558,7 @@ PIXI.Text.heightCache = {};
* http://www.bmglyph.com/ for mac. * http://www.bmglyph.com/ for mac.
* *
* @class BitmapText * @class BitmapText
* @extends DisplayObjectContainer * @extends SpriteBatch
* @constructor * @constructor
* @param text {String} The copy that you would like the text to display * @param text {String} The copy that you would like the text to display
* @param style {Object} The style parameters * @param style {Object} The style parameters
@ -2552,7 +2567,7 @@ PIXI.Text.heightCache = {};
*/ */
PIXI.BitmapText = function(text, style) PIXI.BitmapText = function(text, style)
{ {
PIXI.DisplayObjectContainer.call(this); PIXI.SpriteBatch.call(this);
this._pool = []; this._pool = [];
@ -2563,7 +2578,7 @@ PIXI.BitmapText = function(text, style)
}; };
// constructor // constructor
PIXI.BitmapText.prototype = Object.create(PIXI.DisplayObjectContainer.prototype); PIXI.BitmapText.prototype = Object.create(PIXI.SpriteBatch.prototype);
PIXI.BitmapText.prototype.constructor = PIXI.BitmapText; PIXI.BitmapText.prototype.constructor = PIXI.BitmapText;
/** /**
@ -2597,6 +2612,7 @@ PIXI.BitmapText.prototype.setStyle = function(style)
this.fontSize = font.length >= 2 ? parseInt(font[font.length - 2], 10) : PIXI.BitmapText.fonts[this.fontName].size; this.fontSize = font.length >= 2 ? parseInt(font[font.length - 2], 10) : PIXI.BitmapText.fonts[this.fontName].size;
this.dirty = true; this.dirty = true;
this.tint = style.tint;
}; };
/** /**
@ -2615,6 +2631,8 @@ PIXI.BitmapText.prototype.updateText = function()
var lineWidths = []; var lineWidths = [];
var line = 0; var line = 0;
var scale = this.fontSize / data.size; var scale = this.fontSize / data.size;
for(var i = 0; i < this.text.length; i++) for(var i = 0; i < this.text.length; i++)
{ {
var charCode = this.text.charCodeAt(i); var charCode = this.text.charCodeAt(i);
@ -2663,6 +2681,7 @@ PIXI.BitmapText.prototype.updateText = function()
var lenChildren = this.children.length; var lenChildren = this.children.length;
var lenChars = chars.length; var lenChars = chars.length;
var tint = this.tint || 0xFFFFFF;
for(i = 0; i < lenChars; i++) for(i = 0; i < lenChars; i++)
{ {
var c = i < lenChildren ? this.children[i] : this._pool.pop(); // get old child if have. if not - take from pool. var c = i < lenChildren ? this.children[i] : this._pool.pop(); // get old child if have. if not - take from pool.
@ -2673,6 +2692,7 @@ PIXI.BitmapText.prototype.updateText = function()
c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale; c.position.x = (chars[i].position.x + lineAlignOffsets[chars[i].line]) * scale;
c.position.y = chars[i].position.y * scale; c.position.y = chars[i].position.y * scale;
c.scale.x = c.scale.y = scale; c.scale.x = c.scale.y = scale;
c.tint = tint;
if (!c.parent) this.addChild(c); if (!c.parent) this.addChild(c);
} }
@ -2703,7 +2723,7 @@ PIXI.BitmapText.prototype.updateTransform = function()
this.dirty = false; this.dirty = false;
} }
PIXI.DisplayObjectContainer.prototype.updateTransform.call(this); PIXI.SpriteBatch.prototype.updateTransform.call(this);
}; };
PIXI.BitmapText.fonts = {}; PIXI.BitmapText.fonts = {};
@ -8985,7 +9005,7 @@ PIXI.Graphics.prototype._renderCanvas = function(renderSession)
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.Graphics.prototype.getBounds = function() PIXI.Graphics.prototype.getBounds = function( matrix )
{ {
if(!this.bounds)this.updateBounds(); if(!this.bounds)this.updateBounds();
@ -8995,7 +9015,7 @@ PIXI.Graphics.prototype.getBounds = function()
var h0 = this.bounds.y; var h0 = this.bounds.y;
var h1 = this.bounds.height + this.bounds.y; var h1 = this.bounds.height + this.bounds.y;
var worldTransform = this.worldTransform; var worldTransform = matrix || this.worldTransform;
var a = worldTransform.a; var a = worldTransform.a;
var b = worldTransform.c; var b = worldTransform.c;
@ -9067,7 +9087,7 @@ PIXI.Graphics.prototype.updateBounds = function()
var minY = Infinity; var minY = Infinity;
var maxY = -Infinity; var maxY = -Infinity;
var points, x, y; var points, x, y, w, h;
for (var i = 0; i < this.graphicsData.length; i++) { for (var i = 0; i < this.graphicsData.length; i++) {
var data = this.graphicsData[i]; var data = this.graphicsData[i];
@ -9078,28 +9098,29 @@ PIXI.Graphics.prototype.updateBounds = function()
if(type === PIXI.Graphics.RECT) if(type === PIXI.Graphics.RECT)
{ {
x = points.x - lineWidth/2; x = points[0] - lineWidth/2;
y = points.y - lineWidth/2; y = points[1] - lineWidth/2;
var width = points.width + lineWidth; w = points[2] + lineWidth;
var height = points.height + lineWidth; h = points[3] + lineWidth;
minX = x < minX ? x : minX; minX = x < minX ? x : minX;
maxX = x + width > maxX ? x + width : maxX; maxX = x + w > maxX ? x + w : maxX;
minY = y < minY ? x : minY; minY = y < minY ? x : minY;
maxY = y + height > maxY ? y + height : maxY; maxY = y + h > maxY ? y + h : maxY;
} }
else if(type === PIXI.Graphics.CIRC || type === PIXI.Graphics.ELIP) else if(type === PIXI.Graphics.CIRC || type === PIXI.Graphics.ELIP)
{ {
x = points.x; x = points[0];
y = points.y; y = points[1];
var radius = points.radius + lineWidth/2; w = points[2] + lineWidth/2;
h = points[3] + lineWidth/2;
minX = x - radius < minX ? x - radius : minX; minX = x - w < minX ? x - w : minX;
maxX = x + radius > maxX ? x + radius : maxX; maxX = x + w > maxX ? x + w : maxX;
minY = y - radius < minY ? y - radius : minY; minY = y - h < minY ? y - h : minY;
maxY = y + radius > maxY ? y + radius : maxY; maxY = y + h > maxY ? y + h : maxY;
} }
else else
{ {
@ -9131,7 +9152,7 @@ PIXI.Graphics.prototype.updateBounds = function()
*/ */
PIXI.Graphics.prototype._generateCachedSprite = function() PIXI.Graphics.prototype._generateCachedSprite = function()
{ {
var bounds = this.getBounds(); var bounds = this.getLocalBounds();
if(!this._cachedSprite) if(!this._cachedSprite)
{ {
@ -11867,7 +11888,7 @@ PIXI.RenderTexture.prototype.resize = function(width, height)
this.projection.x = this.width / 2; this.projection.x = this.width / 2;
this.projection.y = -this.height / 2; this.projection.y = -this.height / 2;
var gl = this.gl; var gl = this.renderer.gl;
gl.bindTexture(gl.TEXTURE_2D, this.baseTexture._glTextures[gl.id]); gl.bindTexture(gl.TEXTURE_2D, this.baseTexture._glTextures[gl.id]);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null); gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, this.width, this.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);
} }

File diff suppressed because one or more lines are too long

View file

@ -410,16 +410,16 @@ PIXI.DisplayObject.prototype.updateTransform = function()
a11 = this._cr * this.scale.y, a11 = this._cr * this.scale.y,
a02 = this.position.x + a00 * px - py * a01, a02 = this.position.x + a00 * px - py * a01,
a12 = this.position.y + a11 * py - px * a10, a12 = this.position.y + a11 * py - px * a10,
b00 = parentTransform.a, b01 = parentTransform.b, b02 = parentTransform.tx, b00 = parentTransform.a, b01 = parentTransform.b,
b10 = parentTransform.c, b11 = parentTransform.d, b12 = parentTransform.ty; b10 = parentTransform.c, b11 = parentTransform.d;
worldTransform.a = b00 * a00 + b01 * a10; worldTransform.a = b00 * a00 + b01 * a10;
worldTransform.b = b00 * a01 + b01 * a11; worldTransform.b = b00 * a01 + b01 * a11;
worldTransform.tx = b00 * a02 + b01 * a12 + b02; worldTransform.tx = b00 * a02 + b01 * a12 + parentTransform.tx;
worldTransform.c = b10 * a00 + b11 * a10; worldTransform.c = b10 * a00 + b11 * a10;
worldTransform.d = b10 * a01 + b11 * a11; worldTransform.d = b10 * a01 + b11 * a11;
worldTransform.ty = b10 * a02 + b11 * a12 + b12; worldTransform.ty = b10 * a02 + b11 * a12 + parentTransform.ty;
this.worldAlpha = this.alpha * this.parent.worldAlpha; this.worldAlpha = this.alpha * this.parent.worldAlpha;
}; };
@ -430,8 +430,9 @@ PIXI.DisplayObject.prototype.updateTransform = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.DisplayObject.prototype.getBounds = function() PIXI.DisplayObject.prototype.getBounds = function( matrix )
{ {
matrix = matrix;//just to get passed js hinting (and preserve inheritance)
return PIXI.EmptyRectangle; return PIXI.EmptyRectangle;
}; };
@ -443,17 +444,9 @@ PIXI.DisplayObject.prototype.getBounds = function()
*/ */
PIXI.DisplayObject.prototype.getLocalBounds = function() PIXI.DisplayObject.prototype.getLocalBounds = function()
{ {
var matrixCache = this.worldTransform; //var matrixCache = this.worldTransform;
this.worldTransform = PIXI.identityMatrix; return this.getBounds(PIXI.identityMatrix);///PIXI.EmptyRectangle();
this.updateTransform();
var bounds = this.getBounds();
this.worldTransform = matrixCache;
return bounds;
}; };
/** /**

View file

@ -199,12 +199,18 @@ PIXI.DisplayObjectContainer.prototype.updateTransform = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.DisplayObjectContainer.prototype.getBounds = function() PIXI.DisplayObjectContainer.prototype.getBounds = function(matrix)
{ {
if(this.children.length === 0)return PIXI.EmptyRectangle; if(this.children.length === 0)return PIXI.EmptyRectangle;
// TODO the bounds have already been calculated this render session so return what we have // TODO the bounds have already been calculated this render session so return what we have
if(matrix)
{
var matrixCache = this.worldTransform;
this.worldTransform = matrix;
this.updateTransform();
this.worldTransform = matrixCache;
}
var minX = Infinity; var minX = Infinity;
var minY = Infinity; var minY = Infinity;
@ -226,7 +232,7 @@ PIXI.DisplayObjectContainer.prototype.getBounds = function()
childVisible = true; childVisible = true;
childBounds = this.children[i].getBounds(); childBounds = this.children[i].getBounds( matrix );
minX = minX < childBounds.x ? minX : childBounds.x; minX = minX < childBounds.x ? minX : childBounds.x;
minY = minY < childBounds.y ? minY : childBounds.y; minY = minY < childBounds.y ? minY : childBounds.y;
@ -254,6 +260,24 @@ PIXI.DisplayObjectContainer.prototype.getBounds = function()
return bounds; return bounds;
}; };
PIXI.DisplayObjectContainer.prototype.getLocalBounds = function()
{
var matrixCache = this.worldTransform;
this.worldTransform = PIXI.identityMatrix;
for(var i=0,j=this.children.length; i<j; i++)
{
this.children[i].updateTransform();
}
var bounds = this.getBounds();
this.worldTransform = matrixCache;
return bounds;
};
/** /**
* Sets the container's stage reference, the stage this object is connected to * Sets the container's stage reference, the stage this object is connected to
* *

View file

@ -170,7 +170,7 @@ PIXI.Sprite.prototype.onTextureUpdate = function()
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.Sprite.prototype.getBounds = function() PIXI.Sprite.prototype.getBounds = function( matrix )
{ {
var width = this.texture.frame.width; var width = this.texture.frame.width;
@ -182,7 +182,7 @@ PIXI.Sprite.prototype.getBounds = function()
var h0 = height * (1-this.anchor.y); var h0 = height * (1-this.anchor.y);
var h1 = height * -this.anchor.y; var h1 = height * -this.anchor.y;
var worldTransform = this.worldTransform; var worldTransform = matrix || this.worldTransform ;
var a = worldTransform.a; var a = worldTransform.a;
var b = worldTransform.c; var b = worldTransform.c;
@ -427,6 +427,7 @@ PIXI.Sprite.prototype._renderCanvas = function(renderSession)
} }
}; };
// some helper functions.. // some helper functions..
/** /**

View file

@ -438,7 +438,7 @@ PIXI.Graphics.prototype._renderCanvas = function(renderSession)
* @method getBounds * @method getBounds
* @return {Rectangle} the rectangular bounding area * @return {Rectangle} the rectangular bounding area
*/ */
PIXI.Graphics.prototype.getBounds = function() PIXI.Graphics.prototype.getBounds = function( matrix )
{ {
if(!this.bounds)this.updateBounds(); if(!this.bounds)this.updateBounds();
@ -448,7 +448,7 @@ PIXI.Graphics.prototype.getBounds = function()
var h0 = this.bounds.y; var h0 = this.bounds.y;
var h1 = this.bounds.height + this.bounds.y; var h1 = this.bounds.height + this.bounds.y;
var worldTransform = this.worldTransform; var worldTransform = matrix || this.worldTransform;
var a = worldTransform.a; var a = worldTransform.a;
var b = worldTransform.c; var b = worldTransform.c;
@ -585,7 +585,7 @@ PIXI.Graphics.prototype.updateBounds = function()
*/ */
PIXI.Graphics.prototype._generateCachedSprite = function() PIXI.Graphics.prototype._generateCachedSprite = function()
{ {
var bounds = this.getBounds(); var bounds = this.getLocalBounds();
if(!this._cachedSprite) if(!this._cachedSprite)
{ {