Skip to content

Commit 6cabb03

Browse files
committed
Sprite.crop() now takes a Phaser.Rectangle instead of explicit parameters.
Phaser.Image is a brand new display object perfect for logos, backgrounds, etc. You can scale, rotate, tint and blend and Image, but it has no animation, physics body or input events. Previously if you used Sprite.crop() it would crop all Sprites using the same base image. It now takes a local copy of the texture data and crops just that.
1 parent 4b7fc8d commit 6cabb03

3 files changed

Lines changed: 58 additions & 7 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ Significant API changes:
6868
* PIXI.Rectangle is now aliased to Phaser.Rectangle - saves on code duplication and works exactly the same.
6969
* PIXI.Circle is now aliased to Phaser.Circle - saves on code duplication and works exactly the same.
7070
* Sprite.deltaX and deltaY swapped to functions: Sprite.deltaX() and Sprite.deltaY()
71+
* Sprite.crop() now takes a Phaser.Rectangle instead of explicit parameters.
7172

7273

7374
New features:
7475

76+
* Phaser.Image is a brand new display object perfect for logos, backgrounds, etc. You can scale, rotate, tint and blend and Image, but it has no animation, physics body or input events.
7577

7678
New Examples:
7779

@@ -84,6 +86,7 @@ Bug Fixes:
8486
* Explicitly paused Timer continues if you un-focus and focus the browser window (thanks georgiee)
8587
* Added TimerEvent.pendingDelete and checks in Timer.update, so that removing an event in a callback no longer throws an exception (thanks georgiee)
8688
* Fixed TypeScript defs on lines 1741-1748 (thanks wombatbuddy)
89+
* Previously if you used Sprite.crop() it would crop all Sprites using the same base image. It now takes a local copy of the texture data and crops just that.
8790

8891

8992
You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md

examples/wip/image2.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
// var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
23
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
34

45
function preload() {
@@ -9,13 +10,18 @@ function preload() {
910

1011
var image;
1112
var image2;
13+
var r;
1214

1315
function create() {
1416

1517
image = game.add.image(32, 50, 'pic');
1618

1719
image2 = game.add.image(32, 250, 'pic');
1820

21+
r = new Phaser.Rectangle(0, 0, 100, 100);
22+
23+
image2.crop(r);
24+
1925
game.input.onDown.add(tint, this);
2026

2127
}
@@ -28,12 +34,21 @@ function tint() {
2834

2935
function update() {
3036

31-
// image.angle += 1;
37+
if (r && r.width < 300)
38+
{
39+
r.width += 1;
40+
image2.crop(r);
41+
}
42+
else
43+
{
44+
image2.crop();
45+
r = null;
46+
}
3247

3348
}
3449

3550
function render() {
3651

37-
// game.debug.renderText(sprite.position.y, 32, 32);
52+
game.debug.renderText(image2.width, 32, 32);
3853

3954
}

src/gameobjects/Image.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ Phaser.Image = function (game, x, y, key, frame) {
9090
*/
9191
this.fixedToCamera = false;
9292

93+
94+
9395
};
9496

9597
Phaser.Image.prototype = Object.create(PIXI.Sprite.prototype);
@@ -243,13 +245,44 @@ Phaser.Image.prototype.loadTexture = function (key, frame) {
243245
*
244246
* @method Phaser.Image#crop
245247
* @memberof Phaser.Image
246-
* @param {number} frame - If this Sprite is using part of a sprite sheet or texture atlas you can specify the exact frame to use by giving a string or numeric index.
248+
* @param {Phaser.Rectangle} rect - The Rectangle to crop the Image to. Pass as null to clear any previously set crop.
247249
*/
248-
Phaser.Image.prototype.crop = function(x, y, width, height) {
250+
Phaser.Image.prototype.crop = function(rect) {
251+
252+
if (typeof rect === 'undefined' || rect === null)
253+
{
254+
// Clear any crop that may be set
255+
if (this.texture.hasOwnProperty('sourceWidth'))
256+
{
257+
this.texture.setFrame(new Phaser.Rectangle(0, 0, this.texture.sourceWidth, this.texture.sourceHeight));
258+
}
259+
}
260+
else
261+
{
262+
// Do we need to clone the PIXI.Texture object?
263+
if (this.texture instanceof PIXI.Texture)
264+
{
265+
// Yup, let's rock it ...
266+
var local = {};
267+
268+
Phaser.Utils.extend(true, local, this.texture);
269+
270+
local.sourceWidth = local.width;
271+
local.sourceHeight = local.height;
272+
local.frame = rect;
273+
local.width = rect.width;
274+
local.height = rect.height;
249275

250-
// this.crop = new Phaser.Rectangle(0, 0, this._cache.width, this._cache.height);
251-
// this.texture.setFrame(this.crop);
252-
// this.cropEnabled = false;
276+
this.texture = local;
277+
278+
this.texture.updateFrame = true;
279+
PIXI.Texture.frameUpdates.push(this.texture);
280+
}
281+
else
282+
{
283+
this.texture.setFrame(rect);
284+
}
285+
}
253286

254287
};
255288

0 commit comments

Comments
 (0)