Skip to content

Commit 103c37c

Browse files
committed
BitmapData.drawFull draws the given Game Object or Group to a BitmapData and then recursively iterates through all of its children, including children of Game Objects and Groups. It can draw Text, BitmapText, Sprites, Images, Emitters and Graphics objects. It's perfectly valid to pass in game.world as the parent object, and it will iterate through the entire display list.
1 parent 176289f commit 103c37c

3 files changed

Lines changed: 121 additions & 54 deletions

File tree

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,19 @@ Rich - [@photonstorm](https://twitter.com/photonstorm)
6767

6868
![patreon](http://www.phaser.io/images/patreon.png)
6969

70-
Please help support the future development of Phaser through our [Patreon campaign](https://www.patreon.com/photonstorm). We've some exciting plans and there's so much we'd like to do - let's see if we can all work together to make this possible.
70+
Please help support the future development of Phaser through our [Patreon campaign](https://www.patreon.com/photonstorm). We've some exciting plans and there's so much we'd like to do. Let's see if we can all work together to make this possible.
71+
72+
### Phaser Sponsors
73+
74+
Phaser is [sponsored](https://www.patreon.com/photonstorm) by the following great companies:
75+
76+
![qici](http://www.phaser.io/images/sponsors/qici-100.png)
77+
78+
QICI Engine: [A powerful one-stop integrated Phaser game editor](http://www.qiciengine.com/)
79+
80+
![zenva](http://www.phaser.io/images/sponsors/zenva-100.png)
81+
82+
Zenva Academy: [Online courses on Phaser, HTML5 and native app development](https://academy.zenva.com/?zva_src=phaserpatreon)
7183

7284
<a name="download"></a>
7385
## Download Phaser
@@ -261,6 +273,7 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
261273
* Line.centerOn will position the Line so that its midpoint lays on the coordinates given.
262274
* BitmapData.line draws a line to the BitmapData in the color and thickness specified.
263275
* BitmapData.op is a handy short-code to get and set the canvas global composite operator.
276+
* BitmapData.drawFull draws the given Game Object or Group to a BitmapData and then recursively iterates through all of its children, including children of Game Objects and Groups. It can draw Text, BitmapText, Sprites, Images, Emitters and Graphics objects. It's perfectly valid to pass in `game.world` as the parent object, and it will iterate through the entire display list.
264277

265278
### Updates
266279

src/gameobjects/BitmapData.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ Phaser.BitmapData.prototype = {
11841184

11851185
this._image = source;
11861186

1187-
if (source instanceof Phaser.Sprite || source instanceof Phaser.Image || source instanceof Phaser.Text)
1187+
if (source instanceof Phaser.Sprite || source instanceof Phaser.Image || source instanceof Phaser.Text || source instanceof PIXI.Sprite)
11881188
{
11891189
// Copy over sprite values
11901190
this._pos.set(source.texture.crop.x, source.texture.crop.y);
@@ -1412,6 +1412,59 @@ Phaser.BitmapData.prototype = {
14121412

14131413
},
14141414

1415+
/**
1416+
* Draws the Game Object or Group to this BitmapData and then recursively iterates through all of its children.
1417+
*
1418+
* If a child has an `exists` property then it (and its children) will be only be drawn if exists is `true`.
1419+
*
1420+
* The children will be drawn at their `x` and `y` world space coordinates. If this is outside the bounds of the BitmapData
1421+
* they won't be drawn. You should resize the BitmapData in advance to match the overall bounds of the top-level Game Object.
1422+
*
1423+
* When drawing it will take into account the child's world rotation, scale and alpha values.
1424+
*
1425+
* It's perfectly valid to pass in `game.world` as the parent object, and it will iterate through the entire display list.
1426+
*
1427+
* @method Phaser.BitmapData#drawFull
1428+
* @param {Phaser.World|Phaser.Group|Phaser.Sprite|Phaser.Image|Phaser.Text|Phaser.BitmapText} parent - The Game Object to draw onto this BitmapData and then recursively draw all of its children.
1429+
* @param {string} [blendMode=null] - The composite blend mode that will be used when drawing. The default is no blend mode at all. This is a Canvas globalCompositeOperation value such as 'lighter' or 'xor'.
1430+
* @param {boolean} [roundPx=false] - Should the x and y values be rounded to integers before drawing? This prevents anti-aliasing in some instances.
1431+
* @return {Phaser.BitmapData} This BitmapData object for method chaining.
1432+
*/
1433+
drawFull: function (parent, blendMode, roundPx) {
1434+
1435+
if (parent.worldVisible === false || parent.worldAlpha === 0 || (parent.hasOwnProperty('exists') && parent.exists === false))
1436+
{
1437+
return this;
1438+
}
1439+
1440+
if (parent.type !== Phaser.GROUP && parent.type !== Phaser.EMITTER && parent.type !== Phaser.BITMAPTEXT)
1441+
{
1442+
if (parent.type === Phaser.GRAPHICS)
1443+
{
1444+
var bounds = parent.getBounds();
1445+
this.ctx.save();
1446+
this.ctx.translate(bounds.x, bounds.y);
1447+
PIXI.CanvasGraphics.renderGraphics(parent, this.ctx);
1448+
this.ctx.restore();
1449+
}
1450+
else
1451+
{
1452+
this.copy(parent, null, null, null, null, parent.worldPosition.x, parent.worldPosition.y, null, null, parent.worldRotation, null, null, parent.worldScale.x, parent.worldScale.y, parent.worldAlpha, blendMode, roundPx);
1453+
}
1454+
}
1455+
1456+
if (parent.children)
1457+
{
1458+
for (var i = 0; i < parent.children.length; i++)
1459+
{
1460+
this.drawFull(parent.children[i], blendMode, roundPx);
1461+
}
1462+
}
1463+
1464+
return this;
1465+
1466+
},
1467+
14151468
/**
14161469
* Sets the shadow properties of this BitmapDatas context which will affect all draw operations made to it.
14171470
* You can cancel an existing shadow by calling this method and passing no parameters.

src/pixi/primitives/Graphics.js

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ PIXI.Graphics.prototype.generateTexture = function(resolution, scaleMode)
653653

654654
canvasBuffer.context.scale(resolution, resolution);
655655

656-
canvasBuffer.context.translate(-bounds.x,-bounds.y);
656+
canvasBuffer.context.translate(-bounds.x, -bounds.y);
657657

658658
PIXI.CanvasGraphics.renderGraphics(this, canvasBuffer.context);
659659

@@ -823,84 +823,84 @@ PIXI.Graphics.prototype._renderCanvas = function(renderSession)
823823
*/
824824
PIXI.Graphics.prototype.getBounds = function(matrix)
825825
{
826-
if(!this._currentBounds)
826+
if (!this._currentBounds)
827827
{
828-
829-
// return an empty object if the item is a mask!
828+
// Return an empty object if the item is a mask!
830829
if (!this.renderable)
831830
{
832831
return PIXI.EmptyRectangle;
833832
}
834833

835-
if (this.dirty)
836-
{
837-
this.updateLocalBounds();
838-
this.webGLDirty = true;
839-
this.cachedSpriteDirty = true;
840-
this.dirty = false;
841-
}
834+
if (this.dirty)
835+
{
836+
this.updateLocalBounds();
837+
this.webGLDirty = true;
838+
this.cachedSpriteDirty = true;
839+
this.dirty = false;
840+
}
842841

843-
var bounds = this._localBounds;
842+
var bounds = this._localBounds;
844843

845-
var w0 = bounds.x;
846-
var w1 = bounds.width + bounds.x;
844+
var w0 = bounds.x;
845+
var w1 = bounds.width + bounds.x;
847846

848-
var h0 = bounds.y;
849-
var h1 = bounds.height + bounds.y;
847+
var h0 = bounds.y;
848+
var h1 = bounds.height + bounds.y;
850849

851-
var worldTransform = matrix || this.worldTransform;
850+
var worldTransform = matrix || this.worldTransform;
852851

853-
var a = worldTransform.a;
854-
var b = worldTransform.b;
855-
var c = worldTransform.c;
856-
var d = worldTransform.d;
857-
var tx = worldTransform.tx;
858-
var ty = worldTransform.ty;
852+
var a = worldTransform.a;
853+
var b = worldTransform.b;
854+
var c = worldTransform.c;
855+
var d = worldTransform.d;
856+
var tx = worldTransform.tx;
857+
var ty = worldTransform.ty;
859858

860-
var x1 = a * w1 + c * h1 + tx;
861-
var y1 = d * h1 + b * w1 + ty;
859+
var x1 = a * w1 + c * h1 + tx;
860+
var y1 = d * h1 + b * w1 + ty;
862861

863-
var x2 = a * w0 + c * h1 + tx;
864-
var y2 = d * h1 + b * w0 + ty;
862+
var x2 = a * w0 + c * h1 + tx;
863+
var y2 = d * h1 + b * w0 + ty;
865864

866-
var x3 = a * w0 + c * h0 + tx;
867-
var y3 = d * h0 + b * w0 + ty;
865+
var x3 = a * w0 + c * h0 + tx;
866+
var y3 = d * h0 + b * w0 + ty;
868867

869-
var x4 = a * w1 + c * h0 + tx;
870-
var y4 = d * h0 + b * w1 + ty;
868+
var x4 = a * w1 + c * h0 + tx;
869+
var y4 = d * h0 + b * w1 + ty;
871870

872-
var maxX = x1;
873-
var maxY = y1;
871+
var maxX = x1;
872+
var maxY = y1;
874873

875-
var minX = x1;
876-
var minY = y1;
874+
var minX = x1;
875+
var minY = y1;
877876

878-
minX = x2 < minX ? x2 : minX;
879-
minX = x3 < minX ? x3 : minX;
880-
minX = x4 < minX ? x4 : minX;
877+
minX = x2 < minX ? x2 : minX;
878+
minX = x3 < minX ? x3 : minX;
879+
minX = x4 < minX ? x4 : minX;
881880

882-
minY = y2 < minY ? y2 : minY;
883-
minY = y3 < minY ? y3 : minY;
884-
minY = y4 < minY ? y4 : minY;
881+
minY = y2 < minY ? y2 : minY;
882+
minY = y3 < minY ? y3 : minY;
883+
minY = y4 < minY ? y4 : minY;
885884

886-
maxX = x2 > maxX ? x2 : maxX;
887-
maxX = x3 > maxX ? x3 : maxX;
888-
maxX = x4 > maxX ? x4 : maxX;
885+
maxX = x2 > maxX ? x2 : maxX;
886+
maxX = x3 > maxX ? x3 : maxX;
887+
maxX = x4 > maxX ? x4 : maxX;
889888

890-
maxY = y2 > maxY ? y2 : maxY;
891-
maxY = y3 > maxY ? y3 : maxY;
892-
maxY = y4 > maxY ? y4 : maxY;
889+
maxY = y2 > maxY ? y2 : maxY;
890+
maxY = y3 > maxY ? y3 : maxY;
891+
maxY = y4 > maxY ? y4 : maxY;
893892

894-
this._bounds.x = minX;
895-
this._bounds.width = maxX - minX;
893+
this._bounds.x = minX;
894+
this._bounds.width = maxX - minX;
896895

897-
this._bounds.y = minY;
898-
this._bounds.height = maxY - minY;
896+
this._bounds.y = minY;
897+
this._bounds.height = maxY - minY;
899898

900899
this._currentBounds = this._bounds;
901900
}
902901

903902
return this._currentBounds;
903+
904904
};
905905

906906
/**
@@ -927,14 +927,15 @@ PIXI.Graphics.prototype.containsPoint = function( point )
927927
// only deal with fills..
928928
if (data.shape)
929929
{
930-
if ( data.shape.contains( tempPoint.x, tempPoint.y ) )
930+
if (data.shape.contains(tempPoint.x, tempPoint.y))
931931
{
932932
return true;
933933
}
934934
}
935935
}
936936

937937
return false;
938+
938939
};
939940

940941
/**

0 commit comments

Comments
 (0)