Closed bakermanbrian closed 4 months ago
Please, remember that you can change panorama source by react prop and also using setPanorama Method. If you bind "src" to a state that changes when you call "setPanorama" it will load the same panorama twice! So, in this particular case, you can follow this pattern:
import { Viewer } from "@photo-sphere-viewer/core";
import * as React from "react";
import { render } from "react-dom";
import { ReactPhotoSphereViewer } from "react-photo-sphere-viewer";
const baseUrl = "https://photo-sphere-viewer-data.netlify.app/assets/";
const IMAGES = {
a: `${baseUrl}/artist-workshop.jpg`,
b: `${baseUrl}/sphere.jpg`,
};
const INITIAL_IMG = Object.keys(IMAGES)[0];
import "./styles.css";
function App() {
const [step, setStep] = React.useState(INITIAL_IMG);
const [viewer, setViewer] = React.useState<Viewer | undefined>();
React.useEffect(() => {
if (viewer) {
// When step changes, you call "setPanorama()"
viewer?.setPanorama(IMAGES[step], {
zoom: 0,
position: {
yaw: 1,
pitch: 1,
},
transition: true,
});
}
}, [viewer, step]);
const toggle = () => {
const nextStep = step === "a" ? "b" : "a";
console.log(nextStep, IMAGES[nextStep]);
setStep(nextStep); // you change the step on the state
};
return (
<div className="App">
<button onClick={toggle} className="btn">
change scene
</button>
<ReactPhotoSphereViewer
src={IMAGES[INITIAL_IMG]} // src is binded to a constant that can't change
defaultYaw={0}
defaultPitch={0}
defaultZoomLvl={0}
height={"100vh"}
width={"100%"}
onReady={setViewer}
></ReactPhotoSphereViewer>
</div>
);
}
const rootElement = document.getElementById("root");
render(<App />, rootElement);
Thanks
What happened?
It seems transitioning between panoramas causes a problem. Here is the thread in the photo sphere library on the issue: https://github.com/mistic100/Photo-Sphere-Viewer/issues/1291
I have run into the problem doing a slightly different thing, but I believe it is from the same underlying issue. The code sandbox and my provided code are different.
What should have happened?
No error
Code
Sandbox Link
https://codesandbox.io/p/sandbox/delicate-firefly-nvkmdf?file=%2Fsrc%2Findex.tsx
Library Version
latest
What operating system are you using?
macOS
What browser are you using?
None
Logs
No response
Interest to fix the bug