mpetroff / pannellum

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

Get hotspot pitch, yaw after drag #1090

Open aproni34f opened 2 years ago

aproni34f commented 2 years ago

I have similar question as here: https://github.com/mpetroff/pannellum/issues/913

I do use dragHandlerFunc / dragHandlerArgs and my hotspots are draggable in editor, and after drag I want to save hotspot pitch / yaw. The problem is that mouseEventToCoords only prints the pitch and yaw of the click location, so if I click on the different place in hotspot, these values will be different, which depending on how hotspot looks like, will not reflect its actual position after save. Then when I load viewer again, hotspot are not quite where they are expected to be.

Is there a way in dragHandlerFunc to get actual hotspot pitch / yaw center position, not where clicked?

dbwodlf3 commented 2 years ago
  function getCoords(inputX: number, inputY: number){
    const container = viewer.getContainer();
    const renderer = viewer.getRenderer()!;
    const container_rect = container.getBoundingClientRect();
    const pos = {x: inputX - container_rect.left, y: inputY - container_rect.top};

    const config = viewer.getConfig()!;

    const canvas = renderer.getCanvas();
    const canvasWidth = canvas.clientWidth;
    const canvasHeight = canvas.clientHeight;
    const x = pos.x / canvasWidth * 2 - 1;
    const y = (1 - pos.y / canvasHeight * 2) * canvasHeight / canvasWidth;
    const focal = 1 / Math.tan(config.hfov * Math.PI / 360);
    const s = Math.sin(config.pitch * Math.PI / 180);
    const c = Math.cos(config.pitch * Math.PI / 180);
    const a = focal * c - y * s;
    const root = Math.sqrt(x*x + a*a);
    const pitch = Math.atan((y * c + focal * s) / root) * 180 / Math.PI;

    let yaw = Math.atan2(x / root, a / root) * 180 / Math.PI + config.yaw;
    if (yaw < -180) yaw += 360;
    if (yaw > 180) yaw -= 360;

    return [yaw, pitch];
  }

use this function. inputX and inputY is the position from getBoundingClientRect of hotspot.

mpetroff commented 2 years ago

Is there a way in dragHandlerFunc to get actual hotspot pitch / yaw center position, not where clicked?

The dragHandlerFunc is called both for the events that start the drag and for those that end it. If you call mouseEventToCoords with the end event (e.g., mouseup), you'll get the final hot spot position, since the interim move events are likewise passed through mouseEventToCoords to update the hot spot position.

aproni34f commented 2 years ago

Yes, I realized that afterwards. I guess changing hfov can make hotspot appear to be on different place visually.