Skip to content

Commit 804eea9

Browse files
committed
Optimised BitmapData.copy and refactoring continues.
1 parent 15e6edc commit 804eea9

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/gameobjects/BitmapData.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ Phaser.BitmapData.prototype = {
839839
* @param {number} [newWidth] - The new width of the block being copied. If not specified it will default to the `width` parameter.
840840
* @param {number} [newHeight] - The new height of the block being copied. If not specified it will default to the `height` parameter.
841841
* @param {number} [rotate=0] - The angle in radians to rotate the block to before drawing. Rotation takes place around the center by default, but can be changed with the `anchor` parameters.
842-
* @param {number} [anchorX=0.5] - The anchor point around which the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
843-
* @param {number} [anchorY=0.5] - The anchor point around which the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
842+
* @param {number} [anchorX=0] - The anchor point around which the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
843+
* @param {number} [anchorY=0] - The anchor point around which the block is rotated and scaled. A value between 0 and 1, where 0 is the top-left and 1 is bottom-right.
844844
* @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.
845845
* @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.
846846
* @param {number} [alpha=1] - The alpha that will be set on the context before drawing. A value between 0 (fully transparent) and 1, opaque.
@@ -867,7 +867,7 @@ Phaser.BitmapData.prototype = {
867867
// Reset
868868
this._pos.set(0);
869869
this._scale.set(1);
870-
this._anchor.set(0.5);
870+
this._anchor.set(0);
871871
this._rotate = 0;
872872
this._alpha.current = 1;
873873

@@ -893,8 +893,10 @@ Phaser.BitmapData.prototype = {
893893
// The source region to copy from
894894
if (typeof x === 'undefined' || x === null) { x = 0; }
895895
if (typeof y === 'undefined' || y === null) { y = 0; }
896-
if (typeof width === 'undefined' || width === null) { width = source.width; }
897-
if (typeof height === 'undefined' || height === null) { height = source.height; }
896+
897+
// With a Sprite, if it's scaled the source.width = the scaled width, not the source image width
898+
if (typeof width === 'undefined' || width === null) { width = this._image.width; }
899+
if (typeof height === 'undefined' || height === null) { height = this._image.height; }
898900

899901
// The destination region to copy to
900902
if (typeof tx === 'undefined' || tx === null) { tx = x; }
@@ -995,13 +997,13 @@ Phaser.BitmapData.prototype = {
995997
*/
996998
copyPixels: function (source, area, x, y, alpha) {
997999

998-
return this.copy(source, area.x, area.y, area.width, area.height, x, y, area.width, area.height, 0, 0.5, 0.5, 1, 1, alpha);
1000+
return this.copy(source, area.x, area.y, area.width, area.height, x, y, area.width, area.height, 0, 0.5, 0.5, 1, 1, alpha, blendMode, roundPx);
9991001

10001002
},
10011003

10021004
/**
10031005
* Draws the given Phaser.Sprite or Phaser.Image to this BitmapData at the coordinates specified.
1004-
* You can use the optional width and height values to 'stretch' the sprite as it's drawn.
1006+
* You can use the optional width and height values to 'stretch' the sprite as it's drawn. This uses drawImage stretching, not scaling.
10051007
* When drawing it will take into account the Sprites rotation, scale and alpha values.
10061008
*
10071009
* @method Phaser.BitmapData#draw
@@ -1016,8 +1018,8 @@ Phaser.BitmapData.prototype = {
10161018
*/
10171019
draw: function (source, x, y, width, height, blendMode, roundPx) {
10181020

1019-
// copy(source, x, y, width, height, tx, ty, newWidth, newHeight, rotate, anchorX, anchorY, scaleX, scaleY, alpha, blendMode, roundPx) {
1020-
return this.copy(source, 0, 0, source.width, source.height, x, y, width, height, source.rotation, source.anchor.x, source.anchor.y, source.scale.x, source.scale.y, source.alpha, blendMode, roundPx);
1021+
// By specifying null for most parameters it will tell `copy` to use the Sprite values instead, which is what we want here
1022+
return this.copy(source, null, null, null, null, x, y, width, height, null, null, null, null, null, null, blendMode, roundPx);
10211023

10221024
},
10231025

@@ -1315,10 +1317,30 @@ Phaser.BitmapData.prototype = {
13151317
this.context.globalCompositeOperation = 'luminosity';
13161318
return this;
13171319

1318-
},
1320+
}
13191321

13201322
};
13211323

1324+
/**
1325+
* @name Phaser.Sprite#smoothed
1326+
* @property {boolean} smoothed - Gets or sets this BitmapData.contexts smoothing enabled value.
1327+
*/
1328+
Object.defineProperty(Phaser.BitmapData.prototype, "smoothed", {
1329+
1330+
get: function () {
1331+
1332+
Phaser.Canvas.getSmoothingEnabled(this.context);
1333+
1334+
},
1335+
1336+
set: function (value) {
1337+
1338+
Phaser.Canvas.setSmoothingEnabled(this.context, value);
1339+
1340+
}
1341+
1342+
});
1343+
13221344
/**
13231345
* Gets a JavaScript object that has 6 properties set that are used by BitmapData in a transform.
13241346
*

src/system/Canvas.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,14 @@ Phaser.Canvas = {
260260

261261
},
262262

263+
getSmoothingEnabled: function (context) {
264+
265+
return (context['imageSmoothingEnabled'] || context['mozImageSmoothingEnabled'] || context['oImageSmoothingEnabled'] || context['webkitImageSmoothingEnabled'] || context['msImageSmoothingEnabled']);
266+
267+
},
268+
263269
/**
264-
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast on webkit').
270+
* Sets the CSS image-rendering property on the given canvas to be 'crisp' (aka 'optimize contrast' on webkit).
265271
* Note that if this doesn't given the desired result then see the setSmoothingEnabled.
266272
*
267273
* @method Phaser.Canvas.setImageRenderingCrisp

0 commit comments

Comments
 (0)