image 100%+prop goes out of context

  • Hello,

    I'm having a problem with the "prop" value, which is causing a bug in my Lightbox.

    This lightbox is built with a container layer to add margin and darken the background, and inside, a layer that contains the image.

    I'd like this image to be displayed at a maximum of 100% of the parent at its largest width, while respecting its aspect ratio.

    First of all, the maxwidth and maxheight parameters don't work with percentages, which doesn't suit me (if the image is smaller than its context, then it's zoomed in, which I don't want).

    But I especially have a problem with "prop"from what I've tested:

    • If the image size is not defined, it will be displayed at its original size, regardless of the context.
    • If width and height are set to 100%, the image is displayed at 100% of the context (after deducting the margins defined in the container), but this logically breaks the aspect ratio.
    • If the width or height is set to 100% and the other value is set to prop, then the aspect ratio is respected and the 100% value respects the context and its margins, but the other axis set to prop no longer respects the context and may go outside the margins and the viewport...

    As a result, in my example, if I want to display a horizontal image on a vertical screen, the image is taken out of the screen, and if I invert height and width, then it would be the vertical images that would be taken out of the display on a horizontal screen.

    In short, what I'd like is the equivalent of the "object-fit: contain;" function in css:

    CSS
    img {
        max-width: 100%;
        max-height: 100%;
        object-fit: contain;
    }

    Do you have any ideas on how to get round this problem?

    Here's my code:

    Thank you

  • I've added an action that sets ‘prop’ to width for horizontal screens (desktop) and to height for vertical screens (mobile). It also updates it if the screen size changes.

    It doesn't fix the problem, but it makes it less obvious, so if you have any ideas, I'd love to hear them.

    PS: what would be the best method of getting the size of the image I want to open?
    PS2: Wouldn't it be easier to use an iframe? But then, how do you send the image url from KrPano to the iframe? (to have just one html+js, or php page)


    Edited once, last by MrSoul (September 3, 2024 at 1:31 AM).

  • to get the displayed pixel size of the parent use layer[boxbg].pixelwidth/height

    to get the pixel size of the image use layer[boximg].imagewidth/height (after layer[boximg].loaded=true)

    in layer[boximg].resize set the images width/height to be contained in the parents pixelwidth/height

  • to get the displayed pixel size of the parent use layer[boxbg].pixelwidth/height

    to get the pixel size of the image use layer[boximg].imagewidth/height (after layer[boximg].loaded=true)

    in layer[boximg].resize set the images width/height to be contained in the parents pixelwidth/height

    Thank you, that was very helpful!

    I've decided to compare the aspect ratio of the viewport without the margins to the aspect ratio of the image so that it works regardless of the size of the image/viewport:


    So I've got two little questions to improve things and make the code cleaner:

    - Can we add general variables like those found in the skin_settings parameters?

    - Could we have something like this on the hotspots :

    XML
    <hotspot [...] imgurl="../photos/20230815144033_4711.jpg" onclick="open_lightbox();" />

    rather than :

    XML
    <hotspot [...] onclick="set(layer[boximg].img_url, '../photos/20230815144033_4711.jpg'); open_lightbox();" />

    And how do you set it up? (retrieve the imgurl parameter in the action to reuse it)

    PS: Sorry, I have a lot of trouble with the doc, English is not my natural language, I'd even say I'm rubbish at it and this one is "exhaustive"!

    Edited 2 times, last by MrSoul (September 4, 2024 at 4:34 PM).

  • Code
    <hotspot ... imgurl="../photos/20230815144033_4711.jpg" onclick="open_lightbox();" />
    <action name="open_lightbox" scope="local">
        trace(caller.imgurl);
    </action>
    Code
    <hotspot ... imgurl="../photos/20230815144033_4711.jpg" onclick="open_lightbox(*imgurl);" />
    <action name="open_lightbox" scope="local" args="url">
        trace(url);
    </action>

    see here --> https://krpano.com/docu/xml/#action

  • Great, I used the first solution.

    However, it only seems to work with a get() (I'm having trouble understanding which variable you can call with or without a get()).


    And I've just found for the parameters in skin_settings :

    • In xml: get:skin_settings.design_margin
    • In actions : skin_settings.design_margin

    However, I have just one small question about this: if I want to retrieve several variables in the same XML parameter, it doesn't work any more:

    To fill in childmargin="30 30 120", childmargin="get:skin_settings.design_margin get:skin_settings.design_margin get:skin_settings.design_marginbottom" doesn't work, any idea how?

  • I think like:

    childmargin = skin_settings.design_margin + ' ' + skin_settings.design_margin ... etc ;

    .. a space between the apostrophes..

    On phone..Untested.

    Tuur*thumbsup*

  • trace/debug/error() etc doesn't need a get() and also calc() auto resolves variables

    most other functions don't to that and you need get(myvar) or in modern syntax *myvar

    childmargin="30 30 120" <-- this is one value and you cant just add some gets. you would write -->

    childmargin="calc:skin_settings.design_margin + ' ' + skin_settings.design_margin + ' ' + skin_settings.design_marginbottom"

    (or in code as tuur posted above)

    --> manual --> https://krpano.com/docu/actions/#get

  • in expressions get/* is not needed

    calc( var1 * var2 ) or just ( var1 * var2 )

    var3 = var1 * var2

    get/* is just needed outside expressions

    eg.

    layername = 'test';

    layer[*layername].html = layername + '_layer';

    krpano.com - Documentation - Actions / Scripting Reference

    krpano.com - Documentation - Actions / Scripting Reference

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!