cornerstonejs / cornerstoneTools

A framework for tools built on top of Cornerstone.
https://tools.cornerstonejs.org/
MIT License
577 stars 452 forks source link

Changing a state variable in React causes Cornerstone Tools to lose it's zoom setting #1538

Open dcox1776 opened 1 year ago

dcox1776 commented 1 year ago

Prerequisites

Description

Steps to Reproduce the issue

  1. When I zoom an image and subsequently change a React state variable in response to a button being clicked,
  2. Then the image reverts to its original size.

Expected behavior: I expect the image to retain its size as a result of zooming.

Actual behavior: (What actually happened)

CodeSandbox With Reproduction of Issue: I'll have to work on this. Need to setup a React app to reproduce the issue.

dcox1776 commented 1 year ago

I was able to deal with this issue by writing the following function:

function saveViewport() {
    const el = document.getElementById('image-element');
    viewport.current = cornerstone.getViewport(el);
    translation.current = { 
        x: viewport.current.translation.x,
        y: viewport.current.translation.y
    }
}

In response to an event such as a button click or slider change, I call this function to preserve the viewport in a reference variable. When the view is redrawn my useEffect function is called and I restore the viewport.

Notice that I copy the translation object explicitly. This is because assignment of objects in JavaScript is by reference and not value. The value attributes of the viewport are preserved when I assign the viewport to my reference variable but not the object attributes. If I don't do this, then the translation values are reset to 0.