Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Texture.clone() #7365

Merged
merged 2 commits into from Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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