Jump to content

Sprites resized to fit the canvas, how to retrieve the current (new) size?


Recommended Posts

So I create a canvas as stated in the code below, with climateResources the textures are loaded (1024x1024) however because the screen is resized the sprites are resized to fit the screen.

const app = new Application({
        antialias: true,    // default: false
        resolution: 1       // default: 1
app.renderer.view.style.position = "absolute";
app.renderer.view.style.display = "block";
PIXI.AbstractRenderer.autoDensity = true;
app.renderer.resize(window.innerWidth, window.innerHeight);

climateResources[gameLocation[0].name].forEach(arr => app.loader.add(arr));
app.loader.onError.add((loader, res)=>{
    //error stuff
app.loader.onProgress.add((loader, res) => {
    //progress stuff
app.loader.onComplete.add((loader, res) => {
    //complete, start setup

But I have no clue how to retrieve the resized size, I tried to retrieve the local position once a mouse down is clicked. But this does not honor the original size of the sprite, it gives some values from the absolute middle. But I cannot find on what it's based.

this.sprite.on('mousedown', (e) => {

Result (this is not based on the 1024x1024 size of the sprite?):

Top left:
Point {x: -538.7999999999998, y: -536.9999999999999}
Bottom right:
Point {x: 537.5999999999999, y: 538.1999999999998}

Does anyone know how I can retrieve the new size of my sprite in the mousedown or any other given moment so that I can calculate where the mousedown has taken place within my sprite?
Or is there any other better method that I can use to record a click on a sprite?

With thanks,

Edited by darkshifty
Link to comment
Share on other sites

1 hour ago, ivan.popelyshev said:

It should be (-512,-512) (512,512) because your sprite anchor is 0.5 , right? Please provide the demo on codepen/jsfiddle/pixiplayground.

toLocal and toGlobal are easy to debug, just look which matrices they take from. Did you try to place breakpoint there and see how coords are changing?

Thank you for your response, sadly it's close to 512 but not accurately. 

Here is a pixiplayground:

Both display a different size and position, if you open it on mobile its also different.

Link to comment
Share on other sites

41 minutes ago, ivan.popelyshev said:

Your texture size is 1080. Coords are very close to 540 which is half. Sprite anchor is 0.5, so texture is positioned by its center in (0,0)

Sigh... I feel so stupid now. I have been in a tunnel vision for three days.

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.

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.


  • Recently Browsing   0 members

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