Since AnkiDroid doesn’t keep the WebView zoom when you reveal the answer, I wrote a small script that implements its own zoom on the IO container and restores it on the back side.
Features
- Pinch to zoom (up to 8×), one-finger drag to pan, double-tap to reset
- Uses the whole screen when zoomed, so wide images can also grow vertically
- Zoom and position are restored exactly on the answer side (no jump)
- Resets automatically on the next card
Install
Paste the script right after the existing anki.imageOcclusion.setup() block in both the Front and Back templates. Set SIDE = "front" in the front template and SIDE = "back" in the back one.
Notes
Tested only on AnkiDroid 2.25.3. One-finger scrolling over the image is disabled (touch-action: none). Feedback welcome, especially on other platforms.
Released under the MIT License: feel free to use, modify and share it, just keep the copyright notice.
<script>
(function () {
var SIDE = "front"; // <-- nel template Retro metti "back"
var KEY = "ioZoomState";
var MAX_SCALE = 8;
var MAX_AGE = 10 \* 60 \* 1000; // ms: dopo 10 minuti lo stato salvato si ignora
var el = document.getElementById("image-occlusion-container");
if (!el) return;
var s = 1, x = 0, y = 0;
// ---- storage (localStorage + window.name come ripiego) ----
function save() {
// salvo la posizione reale sullo schermo (L, T), non l'offset: così non dipende da piccole differenze di layout tra fronte e retro
var r = el.getBoundingClientRect();
var v = JSON.stringify({ s: s, L: r.left, T: r.top, t: Date.now() });
try { localStorage.setItem(KEY, v); } catch (e) {}
try { window.name = KEY + ":" + v; } catch (e) {}
}
function load() {
var v = null;
try { v = localStorage.getItem(KEY); } catch (e) {}
if (!v) {
try {
if (window.name && window.name.indexOf(KEY + ":") === 0)
v = window.name.slice(KEY.length + 1);
} catch (e) {}
}
if (!v) return null;
try {
var o = JSON.parse(v);
if (Date.now() - o.t > MAX_AGE) return null;
return o;
} catch (e) { return null; }
}
function clear() {
try { localStorage.removeItem(KEY); } catch (e) {}
try { if (window.name.indexOf(KEY + ":") === 0) window.name = ""; } catch (e) {}
}
// ---- trasformazione ----
// Il limite non è più il riquadro dell'immagine ma l'intero schermo (viewport):
// un'immagine "wide" può così espandersi in altezza usando tutto lo spazio.
function axis(pos, base, size, view) {
var c = size \* s;
var left = base + pos, lo, hi;
if (c > view) { lo = view - c; hi = 0; }
else { lo = Math.min(0, base); hi = Math.max(view - c, base); } // nessun centraggio forzato: niente scatti
return Math.min(hi, Math.max(lo, left)) - base;
}
function clamp() {
s = Math.min(MAX_SCALE, Math.max(1, s));
if (s === 1) { x = 0; y = 0; return; }
var r = el.getBoundingClientRect();
var bl = r.left - x, bt = r.top - y;
x = axis(x, bl, el.offsetWidth, window.innerWidth);
y = axis(y, bt, el.offsetHeight, window.innerHeight);
}
function apply() {
clamp();
el.style.transformOrigin = "0 0";
el.style.transform = "translate(" + x + "px," + y + "px) scale(" + s + ")";
document.documentElement.style.overflow = s > 1 ? "hidden" : "";
el.style.position = "relative";
el.style.zIndex = s > 1 ? "10" : "";
}
el.style.touchAction = "none";
// ---- gesti: pinch per zoom, un dito per spostare, doppio tap per reset ----
var pts = {}, prev = null, lastTap = 0, moved = false;
function pointers() { return Object.keys(pts).map(function (k) { return pts\[k\]; }); }
function snapshot() {
var p = pointers();
if (p.length === 1) return { cx: p\[0\].x, cy: p\[0\].y, d: 0 };
if (p.length >= 2) {
var dx = p\[0\].x - p\[1\].x, dy = p\[0\].y - p\[1\].y;
return { cx: (p\[0\].x + p\[1\].x) / 2, cy: (p\[0\].y + p\[1\].y) / 2, d: Math.sqrt(dx \* dx + dy \* dy) };
}
return null;
}
el.addEventListener("pointerdown", function (e) {
try { el.setPointerCapture(e.pointerId); } catch (err) {}
saved = null; // da qui in poi comanda l'utente
pts\[e.pointerId\] = { x: e.clientX, y: e.clientY };
prev = snapshot();
moved = false;
});
el.addEventListener("pointermove", function (e) {
if (!pts\[e.pointerId\]) return;
pts\[e.pointerId\] = { x: e.clientX, y: e.clientY };
var cur = snapshot();
if (!prev || !cur) { prev = cur; return; }
var count = pointers().length;
var rect = el.getBoundingClientRect();
var baseLeft = rect.left - x, baseTop = rect.top - y;
if (count >= 2 && prev.d > 0) {
var ns = Math.min(MAX_SCALE, Math.max(1, s \* (cur.d / prev.d)));
var px = cur.cx - baseLeft, py = cur.cy - baseTop;
x = px - (px - x) \* (ns / s) + (cur.cx - prev.cx);
y = py - (py - y) \* (ns / s) + (cur.cy - prev.cy);
s = ns;
moved = true;
} else if (count === 1 && s > 1) {
x += cur.cx - prev.cx;
y += cur.cy - prev.cy;
moved = true;
}
prev = cur;
apply();
save();
e.preventDefault();
}, { passive: false });
function end(e) {
delete pts\[e.pointerId\];
prev = snapshot();
if (!moved && Object.keys(pts).length === 0) {
var now = Date.now();
if (now - lastTap < 300) { s = 1; x = 0; y = 0; apply(); save(); }
lastTap = now;
}
}
el.addEventListener("pointerup", end);
el.addEventListener("pointercancel", end);
// ---- ripristino ----
var saved = null;
function place() {
if (!saved) { apply(); return; }
// ripristino esatto, senza clamp: l'immagine riappare nello stesso identico punto
el.style.transform = "none";
var r = el.getBoundingClientRect();
s = saved.s; x = saved.L - r.left; y = saved.T - r.top;
el.style.transformOrigin = "0 0";
el.style.transform = "translate(" + x + "px," + y + "px) scale(" + s + ")";
document.documentElement.style.overflow = s > 1 ? "hidden" : "";
el.style.position = "relative";
el.style.zIndex = s > 1 ? "10" : "";
}
function init() {
if (SIDE === "front") clear(); // nuova carta: parti da zero
else saved = load();
place();
setTimeout(place, 150); // riallinea dopo che Anki ha dimensionato l'immagine
}
if (document.readyState === "complete") init();
else window.addEventListener("load", init);
})();
</script>