Jump to content

How to use defaultAnchor from Texture ?


JeanLouis
 Share

Recommended Posts

Hello

I would like to configure offsets on texture (not on sprites).

Why this example does not work ?

const app = new PIXI.Application({
	resizeTo: window,
  backgroundColor: 0x999999
});
document.body.appendChild(app.view);
PIXI.Assets.add('bunny', 'https://pixijs.io/examples/examples/assets/bunny.png'); 
PIXI.Assets.load(['bunny']).then((textures) =>
{
	let texture = textures['bunny'];
  
  let tmp = new PIXI.Texture(
    texture, 
    new PIXI.Rectangle(
      0,
      0,
      20,
      20,
    )
  );
  texture.defaultAnchor.x = 30; // wrong instruction ? Does not work
  const sprite = PIXI.Sprite.from(tmp);
  app.stage.addChild(sprite);
});

Thanks,

Link to comment
Share on other sites

  • 1 month later...

Hello

It looks like there might be a misunderstanding in how to apply offsets to a texture. Instead of adjusting defaultAnchor.x on the original texture, you should set the anchor directly on the sprite. Here's the corrected code:

const app = new PIXI.Application({ resizeTo: window, backgroundColor: 0x999999 }); document.body.appendChild(app.view); PIXI.Assets.add('bunny', 'https://pixijs.io/examples/examples/assets/bunny.png'); PIXI.Assets.load(['bunny']).then((textures) => { let texture = textures['bunny']; let tmp = new PIXI.Texture( texture, new PIXI.Rectangle( 0, 0, 20, 20, ) ); const sprite = PIXI.Sprite.from(tmp); sprite.anchor.set(0.5); // Set the anchor to the center of the sprite sprite.position.set(app.view.width / 2, app.view.height / 2); // Position in the center of the stage app.stage.addChild(sprite); });

In this corrected code, I've set the anchor directly on the sprite using sprite.anchor.set(0.5), and I've also positioned the sprite at the center of the stage for clarity. Adjust the anchor and position as needed for your specific requirements.

 

Thnk you

rpa certification

 

Link to comment
Share on other sites

Based on your answer, I think you should know what's going on.
Let me briefly summarize:
The"texture.defaultAnchor.x" should be changed to "tmp.defaultAnchor.x"
And the value should not be 30, the recommended value is between 0 and 1, as you said, its unit is%.

Additionally, we should use Sprite.anchor or Sprite.pivot instead of Texture.defaultAnchor


const app = new PIXI.Application({
	resizeTo: window,
  backgroundColor: 0x999999
});
document.body.appendChild(app.view);
PIXI.Assets.add('bunny', 'https://pixijs.io/examples/examples/assets/bunny.png'); 
PIXI.Assets.load(['bunny']).then((textures) =>
{
	let texture = textures['bunny'];
  
  let tmp = new PIXI.Texture(
    texture, 
    new PIXI.Rectangle(
      0,
      0,
      20,
      20,
    )
  );
//   texture.defaultAnchor.x = 30; // wrong instruction ? Does not work
// tmp.defaultAnchor.x = 30; 
  const sprite = PIXI.Sprite.from(tmp);
  // tmp.defaultAnchor.x = 30;  // Writing here has no effect, it should be written before "Sprite.from(tmp)"
    // sprite.position.x = 30*20; // When you use "tmp.defaultAnchor.x=30;" and you want to display the "sprite"
// sprite.anchor.x=0.5 or sprite.pivot.x=10;
  app.stage.addChild(sprite);

 

Edited by junyi
Change “Sprite.povit” to "Sprite.pivot"
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...