Skip to content

Commit c5d9f12

Browse files
committed
BitmapData.alphaMask has 2 new optional parameters: sourceRect and maskRect to give more fine-grained control over where the source and mask are drawn and their size
BitmapData.draw now has two optional parameters: width and height, to let you stretch the image being drawn if needed.
1 parent 7846da7 commit c5d9f12

2 files changed

Lines changed: 54 additions & 23 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ Version 2.0.6 - "Jornhill" - -in development-
5050

5151
* BitmapData.draw can now also take a Phaser.Sprite, Phaser.Image or BitmapData object as a source type. As a result BitmapData.drawSprite is now depcreated.
5252
* BitmapData.alphaMask can now also take a Phaser.Sprite, Phaser.Image or BitmapData object as a source type.
53-
* BitmapData.alphaMask has 4 new optional parameters: x, y, x2 and y2 to control exactly where the source and mask images are drawn.
53+
* BitmapData.alphaMask has 2 new optional parameters: sourceRect and maskRect to give more fine-grained control over where the source and mask are drawn and their size
5454
* BitmapData.alphaMask 'mask' parameter is now optional, if not given it will use itself as the mask.
5555
* BitmapData.alphaMask now calls BitmapData.update after running.
56+
* BitmapData.draw now has two optional parameters: width and height, to let you stretch the image being drawn if needed.
5657

5758
### New Features
5859

src/gameobjects/BitmapData.js

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -777,14 +777,16 @@ Phaser.BitmapData.prototype = {
777777

778778
/**
779779
* Draws the given image or Game Object to this BitmapData at the coordinates specified.
780-
* If you need to only draw a part of the image use BitmapData.copyPixels instead.
780+
* You can use the optional width and height values to 'stretch' the image as it's drawn.
781781
*
782782
* @method Phaser.BitmapData#draw
783783
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to draw. If you give a string it will try and find the Image in the Game.Cache.
784784
* @param {number} [x=0] - The x coordinate to draw the image to.
785785
* @param {number} [y=0] - The y coordinate to draw the image to.
786+
* @param {number} [width] - The width when drawing the image. You can use this to optionally stretch the drawn image horizontally.
787+
* @param {number} [height] - The height when drawing the image. You can use this to optionally stretch the drawn image vertically.
786788
*/
787-
draw: function (source, x, y) {
789+
draw: function (source, x, y, width, height) {
788790

789791
if (typeof x === 'undefined') { x = 0; }
790792
if (typeof y === 'undefined') { y = 0; }
@@ -794,20 +796,40 @@ Phaser.BitmapData.prototype = {
794796
source = this.game.cache.getImage(source);
795797
}
796798

797-
if (source instanceof Phaser.BitmapData)
798-
{
799-
this.context.drawImage(source.canvas, 0, 0, source.width, source.height, x, y, source.width, source.height);
800-
}
801-
else if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
799+
var src = source;
800+
var sx = 0;
801+
var sy = 0;
802+
var sw = 0;
803+
var sh = 0;
804+
805+
if (source instanceof Phaser.Image || source instanceof Phaser.Sprite)
802806
{
807+
src = sprite.texture.baseTexture.source;
803808
var frame = sprite.texture.frame;
804-
this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height);
809+
sx = frame.x;
810+
sy = frame.y;
811+
sw = frame.width;
812+
sh = frame.height;
813+
// this.context.drawImage(sprite.texture.baseTexture.source, frame.x, frame.y, frame.width, frame.height, x, y, frame.width, frame.height);
805814
}
806815
else
807816
{
808-
this.context.drawImage(source, 0, 0, source.width, source.height, x, y, source.width, source.height);
817+
if (source instanceof Phaser.BitmapData)
818+
{
819+
src = source.canvas;
820+
// this.context.drawImage(source.canvas, 0, 0, source.width, source.height, x, y, source.width, source.height);
821+
}
822+
823+
sw = source.width;
824+
sh = source.height;
825+
// this.context.drawImage(source, 0, 0, source.width, source.height, x, y, source.width, source.height);
809826
}
810827

828+
if (typeof width === 'undefined') { width = sw; }
829+
if (typeof height === 'undefined') { height = sh; }
830+
831+
this.context.drawImage(src, sx, sy, sw, sh, x, y, width, height);
832+
811833
this.dirty = true;
812834

813835
},
@@ -837,27 +859,35 @@ Phaser.BitmapData.prototype = {
837859
*
838860
* @method Phaser.BitmapData#alphaMask
839861
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} source - The Image to draw. If you give a key it will try and find the Image in the Game.Cache.
840-
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string} mask - The Image to use as the alpha mask. If you give a key it will try and find the Image in the Game.Cache.
841-
* @param {number} [x=0] - The x coordinate to draw the source image to.
842-
* @param {number} [y=0] - The y coordinate to draw the source image to.
843-
* @param {number} [x2=0] - The x coordinate to draw the mask image to.
844-
* @param {number} [y2=0] - The y coordinate to draw the mask image to.
862+
* @param {Phaser.Sprite|Phaser.Image|Phaser.BitmapData|HTMLImage|string|null} [mask] - The Image to use as the alpha mask. If you give a key it will try and find the Image in the Game.Cache. If you pass nothing or null it will use itself.
863+
* @param {Phaser.Rectangle} [sourceRect] - A Rectangle where x/y define the coordinates to draw the Source image to and width/height define the size.
864+
* @param {Phaser.Rectangle} [maskRect] - A Rectangle where x/y define the coordinates to draw the Mask image to and width/height define the size.
845865
*/
846-
alphaMask: function (source, mask, x, y, x2, y2) {
866+
alphaMask: function (source, mask, sourceRect, maskRect) {
847867

848-
if (typeof mask === 'undefined') { mask = this; }
849-
if (typeof x === 'undefined') { x = 0; }
850-
if (typeof y === 'undefined') { y = 0; }
851-
if (typeof x2 === 'undefined') { x2 = 0; }
852-
if (typeof y2 === 'undefined') { y2 = 0; }
868+
if (typeof mask === 'undefined' || mask === null) { mask = this; }
853869

854870
var temp = this.context.globalCompositeOperation;
855871

856-
this.draw(mask, x, y);
872+
if (typeof maskRect === 'undefined' || maskRect === null)
873+
{
874+
this.draw(mask);
875+
}
876+
else
877+
{
878+
this.draw(mask, maskRect.x, maskRect.y, maskRect.width, maskRect.height);
879+
}
857880

858881
this.context.globalCompositeOperation = 'source-atop';
859882

860-
this.draw(source, x2, y2);
883+
if (typeof sourceRect === 'undefined' || sourceRect === null)
884+
{
885+
this.draw(source);
886+
}
887+
else
888+
{
889+
this.draw(source, sourceRect.x, sourceRect.y, sourceRect.width, sourceRect.height);
890+
}
861891

862892
this.context.globalCompositeOperation = temp;
863893

0 commit comments

Comments
 (0)