Can I read 'Player Width' into a variable? Edit: Yes! :^)

Q&A about the latest versions
User avatar
Tony
Posts: 1342
Joined: Mon Feb 15, 2010 6:54 am
Location: Adelaide, South Australia
Contact:

Hi Josh,

I tried your solution but ended up with a value of 0. Where are you placing the code?

cheers,

Tony
Tony Redhead | Panoramic Photographer | mobile: +61438501002 | website: https://tonyredhead.com - https://redsquare.com | Pano2VR Tutorials: https://tonyredhead.com/pano2vr | instagram: https://www.instagram.com/tonyredhead/
User avatar
Tony
Posts: 1342
Joined: Mon Feb 15, 2010 6:54 am
Location: Adelaide, South Australia
Contact:

Tony wrote: Tue Aug 11, 2020 5:01 am Hi Josh,

I tried your solution but ended up with a value of 0. Where are you placing the code?

Update: all good and working well. Then I saw your reply :-)

cheers,

Tony
Last edited by Tony on Tue Aug 11, 2020 6:31 am, edited 1 time in total.
Tony Redhead | Panoramic Photographer | mobile: +61438501002 | website: https://tonyredhead.com - https://redsquare.com | Pano2VR Tutorials: https://tonyredhead.com/pano2vr | instagram: https://www.instagram.com/tonyredhead/
Josh
Posts: 11
Joined: Wed Aug 05, 2020 1:54 pm

Hi Tony,

I have the code inside an action on a text box object, though realistically it could live anywhere. What's important here is that the Source for the action is something that happens when the pano loads - I used Config Loaded. The height and width won't be reported until after this action is executed (try it with a mouse click to see what I mean)

Anyhow, I suspect the reason you're getting 0 is because the player size hasn't actually changed. This code on its own won't do anything until the size of the window changes, that's the event that it's listening for so if you resize the window it should start working. To get around this you need to read the width and height into the variables first, and then have it listen for changes:

Code: Select all

javascript:
pano.setVariableValue('var_player_height', pano.getViewerSize().height);
pano.setVariableValue('var_player_width', pano.getViewerSize().width);
pano.on("sizechanged", function() {
pano.setVariableValue('var_player_height', pano.getViewerSize().height);
pano.setVariableValue('var_player_width', pano.getViewerSize().width);
});
Stick this code on an object (shouldn't matter what one) and have an event with source "config loaded", action "URL" and get that code in there. Should slap.

J

EDIT: Great, you got it working. Leaving this here to help anyone else who may come across the thread.
User avatar
Tony
Posts: 1342
Joined: Mon Feb 15, 2010 6:54 am
Location: Adelaide, South Australia
Contact:

Hi Josh,

Thanks for the explanation, it's very helpful. I'd forgotten to add 'javascript:' at the beginning.

cheers,

Tony
Tony Redhead | Panoramic Photographer | mobile: +61438501002 | website: https://tonyredhead.com - https://redsquare.com | Pano2VR Tutorials: https://tonyredhead.com/pano2vr | instagram: https://www.instagram.com/tonyredhead/
Branigan
Posts: 231
Joined: Tue May 19, 2020 3:43 pm

Josh wrote: Tue Aug 11, 2020 6:30 am
I have the code inside an action on a text box object, though realistically it could live anywhere. What's important here is that the Source for the action is something that happens when the pano loads - I used Config Loaded. The height and width won't be reported until after this action is executed (try it with a mouse click to see what I mean)

Anyhow, I suspect the reason you're getting 0 is because the player size hasn't actually changed. This code on its own won't do anything until the size of the window changes, that's the event that it's listening for so if you resize the window it should start working. To get around this you need to read the width and height into the variables first, and then have it listen for changes:
That's an interesting addition, but I literally put my two lines as the text in the text box and it does give the right values immediately. I'm not bothering with triggers because the act of starting the panorama is enough to get the initial values, and if you rotate your phone/tablet that seems to be enough to get it to update.
Josh
Posts: 11
Joined: Wed Aug 05, 2020 1:54 pm

Yeah this would be because code that is placed in a text box is executed when the tour is loaded. It gives the same result so as long as it's working for you then that's all that matters but this could be useful for you to know if you want to trigger JS on an event later on. Glad you got it working how you wanted.

J
SharonaAd
Posts: 3
Joined: Tue Sep 20, 2022 11:38 am

Hi,

I have a similar problem. I want to display a image in proportion to the height and width of the player.
So I've got the height and width but how do I put a variable into the size elements of the image. That field doesn't seem to be able to accept variables as you can see in the attachment.
Is there maybe a Javascript way?

Thanks

Sharona
Attachments
Screenshot 2023-05-15 130231.png
Screenshot 2023-05-15 130231.png (18.86 KiB) Viewed 623 times
Post Reply