Skip to content

Commit 901a75d

Browse files
committed
setCrop will accept numbers or a Rectangle object
1 parent 3e64d1b commit 901a75d

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ The Tint component documentation has been overhauled to explain these difference
7373

7474
There is a new Game Object Component called `TextureCrop`. It replaces the Texture Component (which still exists) and adds in the ability to crop the texture being used. This component is now being used by the `Sprite` and `Image` Game Objects.
7575

76-
* You can crop the frame being used via the new `setCrop` method. The crop is a rectangle that limits the area of the texture frame that is visible during rendering. Cropping a Game Object does not change its size, dimensions, physics body or hit area, it just changes what is shown when rendered. This is ideal for hiding part of a Sprite without using a mask, or for effects like displaying a progress or loading bar. Cropping works even when the Game Object is flipped.
76+
* You can crop the frame being used via the new `setCrop` method. The crop is a rectangle that limits the area of the texture frame that is visible during rendering. Cropping a Game Object does not change its size, dimensions, physics body or hit area, it just changes what is shown when rendered. This is ideal for hiding part of a Sprite without using a mask, or for effects like displaying a progress or loading bar. Cropping works even when the Game Object is flipped, or is a trimmed frame from an atlas.
7777
* You can toggle the crop on a Game Object by changing the `isCropped` boolean at any point.
7878
* The crop is automatically re-applied when the texture or frame of a Game Object is changed. If you wish to disable this, turn off the crop before changing the frame.
7979

src/gameobjects/components/TextureCrop.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,20 @@ var TextureCrop = {
6868
* Therefore, if you had a Game Object that had an 800x600 sized texture, and you wanted to show only the left
6969
* half of it, you could call `setCrop(0, 0, 400, 600)`.
7070
*
71-
* Call this method with no arguments to reset the crop, or toggle the property `isCropped` to `false`.
71+
* It is also scaled to match the Game Object scale automatically. Therefore a crop rect of 100x50 would crop
72+
* an area of 200x100 when applied to a Game Object that had a scale factor of 2.
73+
*
74+
* You can either pass in numeric values directly, or you can provide a single Rectangle object as the first argument.
75+
*
76+
* Call this method with no arguments at all to reset the crop, or toggle the property `isCropped` to `false`.
7277
*
7378
* You should do this if the crop rectangle becomes the same size as the frame itself, as it will allow
7479
* the renderer to skip several internal calculations.
7580
*
7681
* @method Phaser.GameObjects.Components.TextureCrop#setCrop
7782
* @since 3.11.0
7883
*
79-
* @param {number} [x] - The x coordinate to start the crop from.
84+
* @param {(number|Phaser.Geom.Rectangle)} [x] - The x coordinate to start the crop from. Or a Phaser.Geom.Rectangle object, in which case the rest of the arguments are ignored.
8085
* @param {number} [y] - The y coordinate to start the crop from.
8186
* @param {number} [width] - The width of the crop rectangle in pixels.
8287
* @param {number} [height] - The height of the crop rectangle in pixels.
@@ -91,7 +96,16 @@ var TextureCrop = {
9196
}
9297
else if (this.frame)
9398
{
94-
this.frame.setCropUVs(this._crop, x, y, width, height, this.flipX, this.flipY);
99+
if (typeof x === 'number')
100+
{
101+
this.frame.setCropUVs(this._crop, x, y, width, height, this.flipX, this.flipY);
102+
}
103+
else
104+
{
105+
var rect = x;
106+
107+
this.frame.setCropUVs(this._crop, rect.x, rect.y, rect.width, rect.height, this.flipX, this.flipY);
108+
}
95109

96110
this.isCropped = true;
97111
}

0 commit comments

Comments
 (0)