Skip to content

Commit 3b28d56

Browse files
committed
Ironed out the issues in BitmapData.fastCopy.
1 parent 773be31 commit 3b28d56

1 file changed

Lines changed: 42 additions & 24 deletions

File tree

src/gameobjects/BitmapData.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -752,20 +752,39 @@ Phaser.BitmapData.prototype = {
752752

753753
},
754754

755-
756-
fastCopy: function (source, from, x, y, rotate, scaleX, scaleY, alpha, blendMode) {
757-
758-
x = x || 0;
759-
y = y || 0;
760-
rotate = rotate || 0;
761-
scaleX = scaleX || 1;
762-
scaleY = scaleY || 1;
763-
alpha = alpha || 1;
764-
blendMode = blendMode || 'source-atop';
765-
766-
// To keep this fast we're not going to do any look-ups or parameter validation
767-
768-
if (alpha <= 0)
755+
/**
756+
* Copies a rectangular block, defined by the `from` parameter, from the source image to this BitmapData.
757+
* You can optionally translate, rotate, scale, alpha or blend the block as it's drawn.
758+
* All rotation, scaling and drawing takes place around the blocks center point by default, but can be changed with the anchor parameters.
759+
*
760+
* @method Phaser.BitmapData#fastCopy
761+
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage} source - The Image to copy from.
762+
* @param {Phaser.Rectangle} from - The area to copy from the source image.
763+
* @param {number} [x] - The x coordinate to translate to before drawing. If not specified it will default to `from.x`.
764+
* @param {number} [y] - The y coordinate to translate to before drawing. If not specified it will default to `from.y`.
765+
* @param {number} [rotate=0] - The angle in degrees to rotate the block to before drawing. Rotation takes place around the center by default, but can be changed with the `anchor` parameters.
766+
* @param {number} [anchorX=0.5] - The anchor point around with the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
767+
* @param {number} [anchorY=0.5] - The anchor point around with the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
768+
* @param {number} [scaleX=1] - The horizontal scale factor of the block. A value of 1 means no scaling. 2 would be twice the size, and so on.
769+
* @param {number} [scaleY=1] - The vertical scale factor of the block. A value of 1 means no scaling. 2 would be twice the size, and so on.
770+
* @param {number} [alpha=1] - The alpha that will be set on the context before the block is drawn. A value between 0 (fully transparent) and 1, opaque.
771+
* @param {number} [blendMode='source-over'] - The composite blend mode that will be used when drawing the block. The default is no blend mode at all.
772+
* @param {boolean} [roundPx=false] - Should the x and y values be rounded to integers before drawing? This prevents anti-aliasing in some instances.
773+
*/
774+
fastCopy: function (source, from, x, y, rotate, anchorX, anchorY, scaleX, scaleY, alpha, blendMode, roundPx) {
775+
776+
if (typeof x === 'undefined' || x === null) { x = from.x; }
777+
if (typeof y === 'undefined' || y === null) { y = from.y; }
778+
if (typeof rotate === 'undefined' || rotate === null) { rotate = 0; }
779+
if (typeof anchorX === 'undefined' || anchorX === null) { anchorX = 0.5; }
780+
if (typeof anchorY === 'undefined' || anchorY === null) { anchorY = 0.5; }
781+
if (typeof scaleX === 'undefined' || scaleX === null) { scaleX = 1; }
782+
if (typeof scaleY === 'undefined' || scaleY === null) { scaleY = 1; }
783+
if (typeof alpha === 'undefined' || alpha === null) { alpha = 1; }
784+
if (typeof blendMode === 'undefined' || blendMode === null) { blendMode = 'source-over'; }
785+
if (typeof roundPx === 'undefined') { roundPx = false; }
786+
787+
if (alpha <= 0 || scaleX === 0 || scaleY === 0)
769788
{
770789
// Because why bother wasting CPU cycles drawing something you can't see?
771790
return;
@@ -790,20 +809,21 @@ Phaser.BitmapData.prototype = {
790809
this.context.save();
791810

792811
this.context.globalAlpha = alpha;
793-
794812
this.context.globalCompositeOperation = blendMode;
795813

796-
this.context.scale(scaleX, scaleY);
814+
if (roundPx)
815+
{
816+
x |= 0;
817+
y |= 0;
818+
}
797819

798820
this.context.translate(x, y);
799821

800-
this.context.rotate(rotate * Math.PI / 180);
822+
this.context.scale(scaleX, scaleY);
801823

802-
var w = from.width;
803-
var h = from.height;
824+
this.context.rotate(rotate * Math.PI / 180);
804825

805-
// this.context.drawImage(source, sx + from.x, sy + from.y, from.width, from.height, to.x, to.y, to.width, to.height);
806-
this.context.drawImage(source, sx + from.x, sy + from.y, w, h, -from.width/2, -from.height/2, w, h);
826+
this.context.drawImage(source, sx + from.x, sy + from.y, from.width, from.height, -from.width * anchorX, -from.height * anchorY, from.width, from.height);
807827

808828
this.context.restore();
809829

@@ -824,8 +844,6 @@ Phaser.BitmapData.prototype = {
824844
* @param {object} [transform] - A transform object containing the properties scaleX, scaleY, skewX, skewY, translateX and translateY.
825845
* @param {string} [blendMode] - ?
826846
*/
827-
828-
829847
blit: function (image, source, dest, alpha, transform, blendMode) {
830848

831849
var setAlpha = false;
@@ -858,7 +876,7 @@ Phaser.BitmapData.prototype = {
858876

859877
if (typeof blendMode === 'undefined')
860878
{
861-
blendMode = 'source-atop';
879+
blendMode = 'source-over';
862880
}
863881

864882
var src = image;

0 commit comments

Comments
 (0)