Why is my Screen Space Event Listener 1 step behind?

Hello,

please take a look at the example below and pay attention to the console:

you will notice that if you move the scroll wheel forward, a height is logged. If you then move the scroll wheel backwards, the height is decreased rather than increased as it should be. Essentially, the height is always 1 step behind its actual value.

Is there a way I can get the actual height of the camera, and not where it was 1 event ago?

1 Like

I am aware that if you use viewer.onTick, you will receive the most up-to-date value while scrolling. I thought I could assign a variable once per tick which could then be accessed by another function, but the other function, logHeight, still logs a variable which is 1 step behind:

I would simply have my task run in the onTick method, but it is far too computationally expensive to run that often.


it appears I found a solution that checks height once per frame, but also only runs a command on wheel events using a boolean flag:

Hi there,

For this kind of function which should run every frame, I use either the preRender event or postRender event. For example, here’s a sandcastle example which moves an HTML overlay to match any camera movement.

This section of a blog post explains when each of those events are triggered.

Hi Gabby,

the solution you posted has the same issue as the onTick method I posted in my initial reply. Here is an example showcasing that it does not work, as the logged altitude is still one step behind the actual altitude:

Like I said, this is more of a javascript scope / closure problem than a cesium one. Fortunately I also posted a solution to my probelm above, so this is no longer an issue.