Skip to content

Commit

Permalink
Resizing the canvas using js/css now triggers the resize event
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-915 committed Sep 26, 2024
1 parent ca8d634 commit 53b457e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,18 +420,13 @@ function _webglGet(name_, p, type) {
var Module;
var wasm_exports;

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

if (canvas.width != displayWidth ||
canvas.height != displayHeight) {
canvas.width = displayWidth;
canvas.height = displayHeight;
if (on_resize != undefined)
on_resize(Math.floor(displayWidth), Math.floor(displayHeight))
}
canvas.width = displayWidth;
canvas.height = displayHeight;
}

function animation() {
Expand Down Expand Up @@ -1264,8 +1259,15 @@ var importObject = {
}
});

new ResizeObserver(() => {
let dpr = dpi_scale();
let width = Math.floor(canvas.clientWidth * dpr);
let height = Math.floor(canvas.clientHeight * dpr);
wasm_exports.resize?.(width, height);
}).observe(canvas);

window.onresize = function () {
resize(canvas, wasm_exports.resize);
resize(canvas);
};
window.addEventListener("copy", function (e) {
if (clipboard != null) {
Expand Down Expand Up @@ -1407,7 +1409,7 @@ var importObject = {
sapp_set_window_size: function (new_width, new_height) {
canvas.width = new_width;
canvas.height = new_height;
resize(canvas, wasm_exports.resize);
resize(canvas);
},
sapp_schedule_update: function () {
if (animation_frame_timeout) {
Expand Down

0 comments on commit 53b457e

Please sign in to comment.