You are not logged in.

1

Tuesday, February 13th 2018, 12:07am

pixelx, pixelwidth, etc. always returning "0"

Hello everyone.
I now I must be missing something really obvious here, my coding skills are very limited, so I apologize in advance...
I'm trying to recover the pixel width of "Layer A" (which has been created using relative parameters), and then use that value for the x position of "Layer B".

This is my simple test code:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<krpano version="1.19" title="Virtual Tour">

	<action name="startup" autorun="onstart">
		create_layer_A();
		create_layer_B();
		check_values();
	</action>

	<action name="create_layer_A">
		addlayer(layer_A);
		set(layer[layer_A].type, container);
		set(layer[layer_A].width, 40%);
		set(layer[layer_A].height, 10%);
		set(layer[layer_A].align, topleft);
		set(layer[layer_A].x, 0);
		set(layer[layer_A].y, 10%);
		set(layer[layer_A].bgcolor, 0xfbfbfb);
		set(layer[layer_A].bgalpha, 1);
	</action>

	<action name="create_layer_B">
		addlayer(layer_B);
		set(layer[layer_B].type, container);
		set(layer[layer_B].width, 10%);
		set(layer[layer_B].height, 10%);
		set(layer[layer_B].align, topleft);
		layer[layer_A].updatepos();
		set(layer[layer_B].x, get(layer[layer_A].pixelwidth));
		set(layer[layer_B].y, 30%);
		set(layer[layer_B].bgcolor, 0xaafff6);
		set(layer[layer_B].bgalpha, 1);
	</action>

	<action name="check_values">
		trace('Layer A width: ', get(layer[layer_A].width));
		trace('Layer A pixelwidth: ', get(layer[layer_A].pixelwidth));
		trace('Layer A X: : ', get(layer[layer_A].x));
		trace('Layer A pixelX: : ', get(layer[layer_A].pixelx));
		trace('Layer B width: ', get(layer[layer_B].width));
		trace('Layer B pixelwidth: : ', get(layer[layer_B].pixelwidth));
		trace('Layer B X: : ', get(layer[layer_B].x));
		trace('Layer B pixelX: : ', get(layer[layer_B].pixelx));
	</action>

</krpano>

Layer B's x value should be equal to Layer A's width in pixels, but it's "0". And the traced value is also "0" in every "pixel" dimension.
If I change set(layer[layer_B].x, get(layer[layer_A].pixelwidth)); to set(layer[layer_B].x, get(layer[layer_A].width)); it moves to where I want it, because it takes de relative value (40%). But I really need to work with pixel values.
Any help would be much appreciated.

2

Tuesday, February 13th 2018, 12:37am

OK, so a weird update after additional testing...
It works properly if viewed in an incognito window on Chrome, returning the right pixel values and positioning the Layer B right after Layer A. It also works on MS Explorer 11 and Edge straight away, and on an Android phone using Chrome. But it doesn't work using a normal tab of Chrome, or Firefox.
Does this make any sense? *blink*

3

Tuesday, February 13th 2018, 8:46am

maybe you're too fast and your layer is not created yet...
i would try layer[name].updatepos() after creating

https://krpano.com/docu/xml/#layer.updatepos

4

Tuesday, February 13th 2018, 1:59pm

I've tried that, but still no luck.
I figured the problem must be related to the processing speed and number of threads. Layer B starts to get created before Layer A has been completed. So I also tried using nexttick in the initial action, trying to first create Layer A, and then Layer B, but I can't figure it out.
I've come up with a somewhat clumpsy solution: I delay the creation of Layer B for a small amount, giving it time to complete the previous layer. It works, but it seems to me there must be a more elegant way to work around this, since I'm afraid it'll become a mess when dealing with many layers that depend on previous ones for their sizing...
This is my temporary solution:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<krpano version="1.19" title="Virtual Tour">

	<action name="startup" autorun="onstart">
		create_layer_A();
		delayedcall(0.03, create_layer_B());
		delayedcall(0.03, check_values());
	</action>

	<action name="create_layer_A">
		addlayer(layer_A);
		set(layer[layer_A].type, container);
		set(layer[layer_A].width, 40%);
		set(layer[layer_A].height, 10%);
		set(layer[layer_A].align, topleft);
		set(layer[layer_A].x, 0);
		set(layer[layer_A].y, 10%);
		set(layer[layer_A].bgcolor, 0xfbfbfb);
		set(layer[layer_A].bgalpha, 1);		
		layer[layer_A].updatepos();
	</action>

	<action name="create_layer_B">
		addlayer(layer_B);
		set(layer[layer_B].type, container);
		set(layer[layer_B].width, 10%);
		set(layer[layer_B].height, 10%);
		set(layer[layer_B].align, topleft);
		set(layer[layer_B].x, get(layer[layer_A].pixelwidth));
		set(layer[layer_B].y, 30%);
		set(layer[layer_B].bgcolor, 0xaafff6);
		set(layer[layer_B].bgalpha, 1);
		layer[layer_B].updatepos();
	</action>

	<action name="check_values">
		trace('Layer A width: ', get(layer[layer_A].width));
		trace('Layer A pixelwidth: ', get(layer[layer_A].pixelwidth));
		trace('Layer A X: : ', get(layer[layer_A].x));
		trace('Layer A pixelX: : ', get(layer[layer_A].pixelx));
		trace('Layer B width: ', get(layer[layer_B].width));
		trace('Layer B pixelwidth: : ', get(layer[layer_B].pixelwidth));
		trace('Layer B X: : ', get(layer[layer_B].x));
		trace('Layer B pixelX: : ', get(layer[layer_B].pixelx));
	</action>

</krpano>

5

Tuesday, February 13th 2018, 10:01pm

Hi,

it might be possible that there is no size available for the krpano viewer window itself yet.

Try checking the stagewidth / stageheight variables - they might be 0.

If that's the case, you could try adding a:

Source code

1
callwhen(stageheight GT 0, ... );
around your code,

but as your code mixes relative % sizes with calculated pixelwidth sizes, it would be better to put pixelwidth/pixelheight depended code in an onresize event. Then these sizes also would get updated when changing the window size.

Or rethink the layout/code - e.g. by creating layer_B as child of layer_A it would move automatically with it. Or when width is always set to 40%, then x also could be simply set to 40%.

Best regards,
Klaus