annotorious / annotorious-openseadragon

An OpenSeadragon plugin for annotating high-res zoomable images
https://annotorious.github.io
BSD 3-Clause "New" or "Revised" License
121 stars 43 forks source link

Set the viewport position and zoom #194

Closed FedericoGarciaGarcia closed 6 months ago

FedericoGarciaGarcia commented 6 months ago

There are functions to pan and zoom to annotations, but it'd be useful to set any position and any zoom in the viewport (and to get those values, too).

For a project, I have multiple Annotorious images that need to be in sync in terms of their viewport, so I created these functions to set them to any position and zoom I want and to get the target values:

/**
 * @param {OpenSeadragon.Viewer} viewer
 * @returns
 */
function getViewport(viewer) {
  const viewport = {
    x: viewer.viewport.centerSpringX.target.value,
    y: viewer.viewport.centerSpringY.target.value,
    zoom: viewer.viewport.zoomSpring.target.value,
  }

  return viewport;
}

/**
 * @param {OpenSeadragon.Viewer} viewer
 * @param {{x: number, y: number, zoom: number}} newViewport
 */
function setViewport(viewer, newViewport) {
  viewer.viewport.centerSpringX.resetTo(newViewport.x);
  viewer.viewport.centerSpringY.resetTo(newViewport.y);
  viewer.viewport.zoomTo(newViewport.zoom, null, true);
}

It'd be nice to have something like this built-in (or if there is already a better way to do it, please tell me).

rsimon commented 6 months ago

Since you can get those values from the viewer directly, there's no need to proxy this through Annotorious?

FedericoGarciaGarcia commented 6 months ago

Oh that is correct, I should ask in the OSD repo instead. Thanks.