-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Image occlusion doesn't scale when page zoomed in #2588
Comments
I have tested by start review and zoom in then go back to deck selection the zoom is correct but incorrect for reviewer (in io notetype). Again, restart review and zoom out and then again go back to deck selection and the zoom is again incorrect. Also removing CSS for all three class and checking zoom have different result than expected. The possible solution should be in JS implementation, and we may not need to change CSS of above class. Should this solution be used? diff --git a/ts/image-occlusion/review.ts b/ts/image-occlusion/review.ts
index 580d46fe3..620fd6b16 100644
--- a/ts/image-occlusion/review.ts
+++ b/ts/image-occlusion/review.ts
@@ -11,6 +11,8 @@ import { Polygon } from "./shapes/polygon";
import { Rectangle } from "./shapes/rectangle";
import type { Size } from "./types";
+let scale = 1.0;
+
export function setupImageCloze(): void {
window.addEventListener("load", () => {
window.addEventListener("resize", setupImageCloze);
@@ -32,6 +34,14 @@ function setupImageClozeInner(): void {
return;
}
+ container.addEventListener("wheel", (e) => {
+ e.preventDefault();
+ const step = 0.05;
+ const delta = Math.sign(e.deltaY) * step;
+ scale = Math.max(scale + delta, 0.1);
+ container.style.transform = `translate(-50%, 0) scale(${scale})`;
+ });
+
// Enforce aspect ratio of image
container.style.aspectRatio = `${image.naturalWidth / image.naturalHeight}`;
|
That seems to work well when using the mouse wheel. Unfortunately, users may also be zooming in via the View>Zoom In/Out or Reset Zoom options - I wonder if there's a way we can handle that case too? Interestingly, no patch is required to make it work correctly when using pinch-to-zoom on a Mac, as some sort of different zooming strategy seems to be used for the pinch gesture. |
Introduces a custom JS-based zoom system for the reviewer that enables zooming into viewport-constrained images. Additionally adds zoom persistence across Anki sessions. Fixes Image occlusion doesn't scale when page zoomed in ankitects#2588 (but also addresses general image zoom issues across all note types)
I would say that handling this on an image-level could be a viable option for IO 👍 However, this definitely is an issue for other note types as well and has been a source of annoyance for me for a while now, so I tried to shoot for a more generic approach in #2591. Did not come out ideal given that I had to go with two zoom modes, but I do think it could be a reasonable option if we want to cover more problem cases other than IO. |
Edit: sorry, ignore that - I confused this issue with a different one. |
Maybe related, but I noticed that if you zoom in the page with 2023-10-30.11-04-53.mp4Also, the image zoom is reset every time the window zoom is changed with 2023-10-30.11-06-33.mp4 |
For mask editor, zoom issues fixed by #2809. |
Zoom in during reviews workaroundHi all, Here is a workaround that has been doing a good job so far. After you add this to your template, you would be able to hover over the image in the IO card, hold Ctrl, and then scroll the mousewheel. For laptop users, you can use the pinch feature on the trackpad. See this GIF. BeforeAfterThe code to addThis is for the Front side. The same logic applies to the Back! {{#Header}}<div>{{Header}}</div>{{/Header}}
<div style="display: none">{{cloze:Occlusion}}</div>
<div id="err"></div>
<div id="image-occlusion-container">
<div id="image-occlusion-img">{{Image}}</div>
<canvas id="image-occlusion-canvas"></canvas>
</div>
<script>
try {
anki.setupImageCloze();
} catch (exc) {
document.getElementById("err").innerHTML = `Error loading image occlusion. Is your Anki version up to date?<br><br>${exc}`;
}
// * Code to allow users to zoom in during review.
// First, you need to get the image container and assign it to imgContainer
var imgContainer = document.querySelector('#image-occlusion-container');
// Then get the image element
var imgElement = document.querySelector('#image-occlusion-img img');
// Finally, get the canvas
var canvas = document.querySelector('#image-occlusion-canvas');
var imgSize = 100;
// Create an event listener that will detect when the user holds
// the Ctrl key and then scrolls the wheel of the mouse
imgContainer.addEventListener('wheel', function(event) {
if (event.ctrlKey) {
event.preventDefault();
if (event.deltaY < 0) {
// Increase image size
imgSize += 5; // Increase by 5%. You can modify this to your liking!
} else {
// Decrease image size
imgSize -= 5; // Decrease by 5%.
}
imgElement.style.width = imgSize + '%';
imgElement.style.height = 'auto';
imgContainer.style.width = imgSize + '%';
imgContainer.style.height = 'auto';
canvas.style.width = imgSize + '%';
canvas.style.height = 'auto';
}
});
</script> |
Because a max-height is set on the occlusion container, zooming the page in with ctrl+mousewheel results in the image actually shrinking slightly, due to the change in margins.
If we use an explicit height, instead of capping the height relative to the viewport, then scaling works, but images do not fit the screen by default.
If anyone has ideas on how we could both scale-to-fit by default (and when window is resized), but also support zooming, please let us know!
Heads up @krmanik @glutanimate
The text was updated successfully, but these errors were encountered: