Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dcox1776 opened this issue Mar 31, 2023 · 1 comment

Comments

@dcox1776
Copy link

Prerequisites

  • [6.0.8 ] Which version are you using? (Is it latest?)
  • [yes ] Are you reporting to the correct repository?
  • [ yes and no] Did you search existing issues? (Were any related?)

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
Copy link
Author

dcox1776 commented Mar 31, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant