ref: 532d662722da1fa8cce211ced79da575059d359c
parent: 5af0cd4ac1f906fb09e0bf1cac967c1c27dc240f
author: Ben Harris <bjh21@bjh21.me.uk>
date: Thu Oct 27 06:12:54 EDT 2022
js: Very bad attempt at making puzzles change size when zooming This has the entertaining consequence that repeatedly zooming in and out causes puzzles to gradually shrink, thus demonstrating that recording the nominal size correctly will be necessary.
--- a/emcclib.js
+++ b/emcclib.js
@@ -544,12 +544,15 @@
* size.
*/
js_canvas_set_size: function(w, h) {
+ var dpr = window.devicePixelRatio || 1;
onscreen_canvas.width = w;
offscreen_canvas.width = w;
- resizable_div.style.width = w / (window.devicePixelRatio || 1) + "px";
+ resizable_div.style.width = w / dpr + "px";
+ nominal_width = w / dpr;
onscreen_canvas.height = h;
offscreen_canvas.height = h;
+ nominal_height = h / dpr;
},
/*
--- a/emccpre.js
+++ b/emccpre.js
@@ -30,6 +30,12 @@
// by js_canvas_end_draw.
var update_xmin, update_xmax, update_ymin, update_ymax;
+// Nominal size of the canvas in CSS pixels. This is set when the
+// canvas is explicitly resized, and used as the basis of calls to
+// midend_size whenever the device pixel ratio changes. That way
+// changes of zoom levels in browsers will generally be reversible.
+var nominal_width, nominal_height;
+
// Module object for Emscripten. We fill in these parameters to ensure
// that Module.run() won't be called until we're ready (we want to do
// our own init stuff first), and that when main() returns nothing
@@ -534,11 +540,10 @@
var dpr = window.devicePixelRatio;
if (mql !== null)
mql.removeListener(update_pixel_ratio);
- resizable_div.style.width = onscreen_canvas.width / dpr + "px";
+ resize_puzzle(nominal_width * dpr, nominal_height * dpr);
mql = window.matchMedia(`(resolution: ${dpr}dppx)`);
mql.addListener(update_pixel_ratio);
}
- update_pixel_ratio();
Module.onRuntimeInitialized = function() {
// Run the C setup function, passing argv[1] as the fragment
@@ -546,6 +551,7 @@
// can launch the specified id).
Module.callMain([decodeURIComponent(location.hash)]);
+ update_pixel_ratio();
// And if we get here with everything having gone smoothly, i.e.
// we haven't crashed for one reason or another during setup, then
// it's probably safe to hide the 'sorry, no puzzle here' div and