Skip to content

Commit

Permalink
Fix Texture.clone with frame, orig and noFrame variants (#7365)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev7355608 authored and bigtimebuddy committed Apr 3, 2021
1 parent 2f6b774 commit 88f5619
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions packages/core/src/textures/Texture.ts
Expand Up @@ -316,13 +316,22 @@ export class Texture<R extends Resource = Resource> extends EventEmitter
*/
clone(): Texture
{
return new Texture(this.baseTexture,
this.frame.clone(),
this.orig.clone(),
const clonedFrame = this._frame.clone();
const clonedOrig = this._frame === this.orig ? clonedFrame : this.orig.clone();
const clonedTexture = new Texture(this.baseTexture,
!this.noFrame && clonedFrame,
clonedOrig,
this.trim && this.trim.clone(),
this.rotate,
this.defaultAnchor
);

if (this.noFrame)
{
clonedTexture._frame = clonedFrame;
}

return clonedTexture;
}

/**
Expand Down
22 changes: 22 additions & 0 deletions packages/core/test/Texture.js
Expand Up @@ -183,6 +183,8 @@ describe('PIXI.Texture', function ()
expect(clone.trim).to.be.undefined;
expect(clone.orig).to.not.equal(texture.orig);
expect(toJSON(clone.orig)).to.deep.equal(toJSON(texture.orig));
expect(clone.frame === clone.orig).to.equal(texture.frame === texture.orig);
expect(clone.noFrame).to.equal(texture.noFrame);

clone.destroy();
texture.destroy(true);
Expand Down Expand Up @@ -211,6 +213,8 @@ describe('PIXI.Texture', function ()
expect(clone.orig).to.not.equal(texture.orig);
expect(toJSON(clone.orig)).to.deep.equal(toJSON(texture.orig));
expect(clone.rotate).to.equal(texture.rotate);
expect(clone.frame === clone.orig).to.equal(texture.frame === texture.orig);
expect(clone.noFrame).to.equal(texture.noFrame);

clone.destroy();
texture.destroy(true);
Expand All @@ -225,13 +229,28 @@ describe('PIXI.Texture', function ()

const texture = Texture.from(canvas);

expect(texture.noFrame).to.equal(true);
expect(texture.width).to.equal(50);
canvas.width = 100;
texture.update();
expect(texture.width).to.equal(100);
canvas.height = 70;
texture.update();
expect(texture.height).to.equal(70);

const clone = texture.clone();

expect(texture.noFrame).to.equal(true);
expect(clone.width).to.equal(100);
expect(clone.height).to.equal(70);
canvas.width = 40;
clone.update();
expect(clone.width).to.equal(40);
canvas.height = 60;
clone.update();
expect(clone.height).to.equal(60);

clone.destroy();
texture.destroy(true);
});

Expand All @@ -243,19 +262,22 @@ describe('PIXI.Texture', function ()

let texture = new Texture(baseTexture);

expect(texture.noFrame).to.equal(true);
expect(texture.width).to.equal(50);
baseTexture.setSize(100, 70);
expect(texture.width).to.equal(100);
expect(texture.height).to.equal(70);

texture.frame = new Rectangle(1, 1, 10, 20);
expect(texture.noFrame).to.equal(false);
baseTexture.setSize(110, 80);
expect(texture.width).to.equal(10);
expect(texture.height).to.equal(20);
texture.destroy(true);

baseTexture = new BaseTexture();
texture = new Texture(baseTexture, new Rectangle(1, 1, 10, 20));
expect(texture.noFrame).to.equal(false);
baseTexture.setSize(50, 50);
expect(texture.width).to.equal(10);
expect(texture.height).to.equal(20);
Expand Down

0 comments on commit 88f5619

Please sign in to comment.