-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
unexpected behaviour when clicking on a scaled image #3234
Comments
Does using something like |
Has this issue been addressed? Or is there a workaround for it? I have the same problem with my app and I can reproduce the issue with the example application provided by @pieterprovoost |
Here's another repro of the problem, which is important because you can't capture clicks when you account for pixel ratios.
|
This was mentioned in internal support ticket 107146. |
There were two related problems here, both happening in the same scenario: when an imageOutput with click handlers is showing an image at less than its natural size (e.g. a 1000x1000 px .png file, being displayed in the web page at 500x500 due to max-width or for whatever other reason), any click where the image coordinate (1000x1000) exceeds the display size (500x500). In the example above, a user clicks at 300x300 in the 500x500 displayed image. We call 300x300 the "CSS coordinates". This gets scaled up into the position in the PNG's own coordinate system, "image coordinates": in this case, 600x600. Since the 600x600 image coordinate is greater than the 500x500 CSS coordinate limit, the following issues were triggered. 1. When imageOutput(click=clickOpts(clip=TRUE)) (the default), these clicks weren't registering at all. There was code that detected clicks that were inside the imageOutput but outside the actual image, but this code didn't take scaling into account. 2. Even with clip=FALSE, the click would be triggered BUT the `x` and `y` values on the click event were incorrect--they would max out at the CSS coordinate limit. This because plot and image output divide the world into "panels" and clicks snap to the nearest panel. In the case of image outputs, the server doesn't provide any panels, so the client makes one big panel that covers the whole image--but that code was erroneously using CSS sizes, not image sizes.
There were two related problems here, both happening in the same scenario: when an imageOutput with click handlers is showing an image at less than its natural size (e.g. a 1000x1000 px .png file, being displayed in the web page at 500x500 due to max-width or for whatever other reason), any click where the image coordinate (1000x1000) exceeds the display size (500x500). In the example above, a user clicks at 300x300 in the 500x500 displayed image. We call 300x300 the "CSS coordinates". This gets scaled up into the position in the PNG's own coordinate system, "image coordinates": in this case, 600x600. Since the 600x600 image coordinate is greater than the 500x500 CSS coordinate limit, the following issues were triggered. 1. When imageOutput(click=clickOpts(clip=TRUE)) (the default), these clicks weren't registering at all. There was code that detected clicks that were inside the imageOutput but outside the actual image, but this code didn't take scaling into account. 2. Even with clip=FALSE, the click would be triggered BUT the `x` and `y` values on the click event were incorrect--they would max out at the CSS coordinate limit. This because plot and image output divide the world into "panels" and clicks snap to the nearest panel. In the case of image outputs, the server doesn't provide any panels, so the client makes one big panel that covers the whole image--but that code was erroneously using CSS sizes, not image sizes.
System details
Browser Version: Google Chrome Version 87.0.4280.88 (Official Build) (x86_64), high DPI screen
Output of
sessionInfo()
:Example application
Test image: test_1000.png
Describe the problem in detail
The example application loads an existing image into an imageOutput and adds a click handler. This works fine for smaller images, but when the image is larger than the DOM element, clicks are only registering in the top left part of the image. In the example below, a 1000 by 1000 pixel image is loaded into a 610 CSS pixels wide element. Clicking on the top left crosshairs will correctly return (200, 200) in image coordinates, so the fact that the image is scaled is taken into account. However, clicking on the bottom right crosshairs does not work as its location in image coordinates (800, 800) exceeds the size of the DOM element (610).
I fiddled around with the code a bit and managed to fix my problem by using
img.naturalWidth
andimg.naturalHeight
here, but that breaks the official example.The text was updated successfully, but these errors were encountered: