mpetroff / pannellum

Pannellum is a lightweight, free, and open source panorama viewer for the web.
https://pannellum.org/
MIT License
4.21k stars 717 forks source link

autoRotate speed is inconsistent #832

Open i-to-z opened 4 years ago

i-to-z commented 4 years ago

Hello Matthew, I am using the latest panellum v. 2.5.6 on ubuntu 18 desktop, with various browsers (described below).

I have a simple web page that waits for a panorama to load (at initial yaw=90), then rotates it at speed -5 degrees/second for 10 seconds, then gets the end yaw (aka horizontal rotation degree). So the end yaw (after 10 seconds) should be 90 + 5*10 = 140. But the result is always wrong:

On Opera: After 10 seconds of rotating: current yaw=136.99477978932896; it should be 140 On Firefox: After 10 seconds of rotating: current yaw=120.46426666666707; it should be 140 On Chrome: After 10 seconds of rotating: current yaw=137.57165254399365; it should be 140 On Chromium: After 10 seconds of rotating: current yaw=137.41833774068527; it should be 140

I played with different speeds and initial yaw, the result is always wrong.

So I did some digging and I found this strange code: https://github.com/mpetroff/pannellum/blob/master/src/js/pannellum.js#L1255

I replaced lines 1255 and 1256 with: const yawDiff = -1 config.autoRotate timeDiff; ... and now it works properly. I will appreciate if you could make this fix in the codebase.

Below is the meaningful part of my code.

Thanks,

<script>
const startYaw = 90;
const speed = -5; // degrees per second; a positive value = counter-clockwise rotation.

const pannellumViewer = pannellum.viewer('panorama', {
    "type": "equirectangular",
    "panorama": "https://pannellum.org/images/alma.jpg",
    "autoLoad": true,
    //"autoRotate": speed, // rotation speed is in degrees per second
    "yaw": startYaw,  // the initial horiz. rotation degree , from -180 to 179.99
    "pitch": 0, // the initial vertical pitch (from -90 to 90) : vertical pitch of the view angle
    "hfov": 90, // the initial horizontal field of view, in degrees; to zoom in: decrease hfov
    "compass": false, // show/hide the compass icon in the lower rigth corner. Used along with "northOffset"
    "showFullscreenCtrl": false, //
    "draggable": false, // If set to false, mouse and touch dragging is disabled.
});

pannellumViewer.on("load", () => {
  // speed in degrees per second, negative = look to right; positive = look to the left.
  // pitch is in degrees (from -90 to 90: vertical pitch of the view angle)
  pannellumViewer.startAutoRotate(speed, 0);

  // Rotate for 10 seconds and print the yaw at the end:
  setTimeout(()=>{
    const currentYaw = pannellumViewer.getYaw();
    pannellumViewer.stopAutoRotate(); 
    pannellumViewer.stopMovement();
    //const currentYaw = pannellumViewer.getYaw();  // This line makes no difference, the end yaw is still wrong.
    console.log("####### After 10 seconds of rotating: current yaw="+currentYaw+"; it should be "+(startYaw - speed*10));
  } , 10000);
});

pannellumViewer.on("animatefinished", (eventData) => {
  console.log("####### in animatefinished: pitch="+eventData.pitch+"; yaw="+eventData.yaw+"; hfov="+eventData.hfov);
});
</script>
mpetroff commented 4 years ago

The "strange code" is there for a reason, so your proposed change potentially introduces regressions. It was added to fix some intermittent bugs with changing direction with yaw limits set, as well as some issues that occurred when switching away from and then back to the browser tab. I don't remember all of the details, since the change was made four years ago. See bf9c6f50730ae0d30223dec9d2603617f47bc34b, #170, #213.

For most browsers, the error is ~2%, which I don't consider to be a problem for this sort of functionality. I suspect the larger difference with Firefox probably has something to do with the anti-fingerprinting protections that reduce the accuracy of timers. Although I can't immediately reproduce the issues that code change was introduced to fix after reverting it, I'm hesitant to make a change to "fix" something I don't really consider to be broken due to the risk of reintroducing the bugs that the original change was meant to fix.