Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Orbitcontrols - how to load last view position and rotation

Tags:

three.js

how would I be able to save and load the last view position and rotation when using the orbitalcontrols? When my 3D page is refreshed, I want to load the page at the last view instead of going back to the default position, zoom, and rotation. I am using three.js version 71. Thanks,

Jim

like image 475
user3661557 Avatar asked Dec 22 '25 02:12

user3661557


1 Answers

const loadControls = (controls) => {
  const stateJSON = localStorage.getItem(`orbitControls`);

  if (stateJSON) {
    const { target0, position0, zoom0 } = JSON.parse(stateJSON);
    controls.target0.copy(target0);
    controls.position0.copy(position0);
    controls.zoom0 = zoom0;
    controls.reset();
  }
};

const saveControls = (controls) => {
  controls.saveState();
  const { target0, position0, zoom0 } = controls;
  const state = { target0, position0, zoom0 };
  localStorage.setItem(`orbitControls`, JSON.stringify(state));
};

const controls = new OrbitControls(camera, renderer.domElement);

document.addEventListener(`visibilitychange`, () => {
  saveControls(controls);
});
like image 181
David Braun Avatar answered Dec 26 '25 14:12

David Braun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!