Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,38 @@ function _webglGet(name_, p, type) {
var Module;
var wasm_exports;

// The `dpi_scale` the canvas was last sized at, which the wasm side reads
// again on every `resize`.
var sized_dpi_scale;

function resize(canvas, on_resize) {
var dpr = dpi_scale();
var displayWidth = canvas.clientWidth * dpr;
var displayHeight = canvas.clientHeight * dpr;

// Zooming changes the ratio and the canvas's size in CSS pixels
// together, and can leave its size in pixels as it was.
if (canvas.width != displayWidth ||
canvas.height != displayHeight) {
canvas.height != displayHeight ||
dpr != sized_dpi_scale) {
canvas.width = displayWidth;
canvas.height = displayHeight;
sized_dpi_scale = dpr;
if (on_resize != undefined)
on_resize(Math.floor(displayWidth), Math.floor(displayHeight))
}
}

// Moving the window to a display with another pixel ratio changes it
// without resizing the window.
function on_device_pixel_ratio_change(f) {
var query = window.matchMedia("(resolution: " + window.devicePixelRatio + "dppx)");
query.addEventListener("change", function () {
f();
on_device_pixel_ratio_change(f);
}, { once: true });
}

function animation() {
wasm_exports.frame();
if (!window.blocking_event_loop) {
Expand Down Expand Up @@ -1308,6 +1326,9 @@ var importObject = {
window.onresize = function () {
resize(canvas, wasm_exports.resize);
};
on_device_pixel_ratio_change(function () {
resize(canvas, wasm_exports.resize);
});
window.addEventListener("copy", function (e) {
if (clipboard != null) {
event.clipboardData.setData('text/plain', clipboard);
Expand Down
3 changes: 3 additions & 0 deletions src/native/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,9 @@ pub extern "C" fn resize(width: i32, height: i32) {
let mut d = crate::native_display().lock().unwrap();
d.screen_width = width as _;
d.screen_height = height as _;
// The browser's pixel ratio changes as the page zooms or moves to
// another display.
d.dpi_scale = unsafe { dpi_scale() };
}
tl_event_handler(|event_handler| {
event_handler.resize_event(width as _, height as _);
Expand Down
Loading