@@ -61,8 +61,9 @@ def viewer_html(image: Image.Image, *, title: str, subtitle: str, labels: list[s
6161 #stage.dragging {{ cursor: grabbing; }}
6262 #wrapper {{ position: absolute; transform-origin: 0 0; }}
6363 img {{ display: block; image-rendering: pixelated; image-rendering: crisp-edges; }}
64- #tip {{ position: fixed; display: none; pointer-events: none; z-index: 3; max-width: 320px ; padding: 8px 10px; background: rgba(0,0,0,.9); border: 1px solid #555; border-radius: 4px; font-size: 12px; }}
64+ #tip {{ position: fixed; display: none; pointer-events: none; z-index: 3; max-width: 360px ; padding: 8px 10px; background: rgba(0,0,0,.9); border: 1px solid #555; border-radius: 4px; font-size: 12px; }}
6565 #tip strong {{ display: block; margin-bottom: 4px; font-size: 14px; }}
66+ #tip span {{ overflow-wrap: anywhere; }}
6667 </style>
6768</head>
6869<body>
@@ -94,7 +95,12 @@ def viewer_html(image: Image.Image, *, title: str, subtitle: str, labels: list[s
9495 let scale = 1, panX = 0, panY = 0, dragging = false, startX = 0, startY = 0, startPanX = 0, startPanY = 0;
9596 function update() {{ wrapper.style.transform = `translate(${{panX}}px, ${{panY}}px) scale(${{scale}})`; zoom.textContent = Math.round(scale * 100) + "%"; }}
9697 function center() {{ const r = stage.getBoundingClientRect(); panX = (r.width - image.width * scale) / 2; panY = (r.height - image.height * scale) / 2; update(); }}
97- function resetView() {{ scale = 1; center(); }}
98+ function initialScale() {{
99+ const r = stage.getBoundingClientRect();
100+ const fit = Math.min(r.width / image.width, r.height / image.height) * 0.88;
101+ return Math.max(1, Math.min(64, fit));
102+ }}
103+ function resetView() {{ scale = initialScale(); center(); }}
98104 function zoomBy(factor) {{ scale = Math.max(.1, Math.min(200, scale * factor)); center(); }}
99105 stage.addEventListener("wheel", (event) => {{
100106 event.preventDefault();
@@ -118,9 +124,8 @@ def viewer_html(image: Image.Image, *, title: str, subtitle: str, labels: list[s
118124 tip.style.left = event.clientX + 14 + "px"; tip.style.top = event.clientY + 14 + "px"; tip.style.display = "block";
119125 }} else {{ tip.style.display = "none"; }}
120126 }});
121- addEventListener("load", center ); addEventListener("resize", center );
127+ addEventListener("load", resetView ); addEventListener("resize", resetView );
122128 </script>
123129</body>
124130</html>
125131"""
126-
0 commit comments