visgl / deck.gl

WebGL2 powered visualization framework
https://deck.gl
MIT License
12.07k stars 2.08k forks source link

[Doc] Reset Camera Position #8198

Open Dev-Lan opened 11 months ago

Dev-Lan commented 11 months ago

Link

https://deck.gl/docs/developer-guide/interactivity

Description

I am using deckgl to view images of cells taken with a microscope. Basic built-in camera controls are sufficient for my use case. However, I would like to reset the camera position. There is a section in the docs titled "Reset Camera Position", however, I'm running into an issue and am unsure if it's a bug or a miscommunication in the docs.

Resetting the initialViewState works, but only once. That is,

  1. User pans/zooms image.
  2. User clicks "reset" button. WORKS!
  3. User pans/zooms image again.
  4. User clicks "reset" button again. Does nothing :'(

Now if the object passed into the initialViewState changes, it works again, so it seems this approach only triggers an update if the initialViewState changes.

The reason I have submitted this to docs initially is that based on this discussion, it seems like maybe setting the initialViewState isn't intended as a way to reset the view.

Any clarification here would be much appreciated. Thanks!

Dev-Lan commented 11 months ago

Similar to the discussion, if I add small amount of noise to my target the behavior works as I want, but feels like a hack.

function resetView() {
    deckgl.setProps({
        initialViewState: {
            zoom: 0,
            bearing: 0,
            target: [
                width / 2 + Math.random() * 0.001,
                height / 2 + Math.random() * 0.001,
                0,
            ],
        },
    });
}
Pessimistress commented 11 months ago

The way it currently works, if the new value supplied to initialViewState is deeply equal to the old value, it is considered "not changed" and ignored. I agree that it is inconvenient in this use case. Maybe we should change it to shallow comparison in the next version? @ibgreen @chrisgervang

ibgreen commented 11 months ago

Maybe we should change it to shallow comparison in the next version?

Yes I could get behind that. It would certainly give the user more options, but also require more care to avoid triggering a reset (like when using react with a fixed array as initial state). So we might break some existing apps...

Alternatively adding new props but this does not seem very attractive

chrisgervang commented 11 months ago

It does seem inconvenient that it only resets if the view is different. A shallow comparison should work, but would also reset when the object is spread or copied.

ibgreen commented 11 months ago

Yes, effectively the app would now be using spread or copying as a technique to trigger the reset.

chrisgervang commented 11 months ago

Don't react/redux applications rely on copying even when the values haven't changed?

chrisgervang commented 11 months ago

initialViewStateShallow looks good to me if it's just this prop.

If deck is tracking changes for other props maybe an object to configure them all..

comparisons: {
  initialViewState: "shallow"
  initialEffects ...
}
ibgreen commented 11 months ago

Don't react/redux applications rely on copying even when the values haven't changed?

Not sure what you mean, maybe you can exemplify?

Basically, in react/redux, if you don't call a reducer/setState to update the redux store or the component state, the value won't be changed, and would still shallowly compare equal since the selectors would be returning the original object in the store.

Dev-Lan commented 6 months ago

I just implemented a camera reset function in a new view and was surprised again why it wasn't working 😂

Thankfully I had a vague memory of writing an issue related to cameras in deckgl.

chrisgervang commented 6 months ago

Since this is only a behavior on initialViewState, what do we think about adding a property to the view state to configure this behavior?

initialViewState: {
  ...,
  longitude: 90,
  compare: 'shallow' | 'deep'
}