From a0c603ee7aabcf30d4290f74f879f4b714068616 Mon Sep 17 00:00:00 2001 From: Hugo H Date: Thu, 25 Dec 2025 19:50:13 +0000 Subject: [PATCH] Fixed CENTERX and CENTERY variables --- editor.html | 3 ++- screen.js | 38 ++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/editor.html b/editor.html index fe24d32..ee0a527 100644 --- a/editor.html +++ b/editor.html @@ -267,8 +267,9 @@ function builtinRead(x) { if (x === 'screen' || x === './screen.js') { + const url = new URL('screen.js', window.location.href).href; const xhr = new XMLHttpRequest(); - xhr.open("GET", document.location.href + "screen.js", false); + xhr.open("GET", url, false); xhr.send(); if (xhr.status === 200) return xhr.responseText; else throw "Could not load screen.js"; diff --git a/screen.js b/screen.js index 81d8e1a..071fb98 100644 --- a/screen.js +++ b/screen.js @@ -162,29 +162,43 @@ var $builtinmodule = function (name) { }; } - Object.defineProperty(mod, 'T', { get: function () { return toPy(typeof T !== 'undefined' ? T : 0); } }); - Object.defineProperty(mod, 'W', { get: function () { return toPy(typeof W !== 'undefined' ? W : 0); } }); - Object.defineProperty(mod, 'H', { get: function () { return toPy(typeof H !== 'undefined' ? H : 0); } }); + Object.defineProperty(mod, 'T', { get: function () { return toPy(lc_instance ? lc_instance.T : (window.T || 0)); } }); + Object.defineProperty(mod, 'W', { get: function () { return toPy(lc_instance ? lc_instance.W : (window.W || 0)); } }); + Object.defineProperty(mod, 'H', { get: function () { return toPy(lc_instance ? lc_instance.H : (window.H || 0)); } }); Object.defineProperty(mod, 'MX', { get: function () { - if (typeof MX === 'undefined') return toPy(-1); + let mx = lc_instance ? lc_instance.MX : (window.MX || -1); const el = document.getElementById('canvas'); - if (!el) return toPy(MX); + if (!el || mx === -1) return toPy(mx); const r = el.getBoundingClientRect(); - return toPy((MX - r.left) * (el.width / r.width)); + return toPy((mx - r.left) * (el.width / r.width)); } }); Object.defineProperty(mod, 'MY', { get: function () { - if (typeof MY === 'undefined') return toPy(-1); + let my = lc_instance ? lc_instance.MY : (window.MY || -1); const el = document.getElementById('canvas'); - if (!el) return toPy(MY); + if (!el || my === -1) return toPy(my); const r = el.getBoundingClientRect(); - return toPy((MY - r.top) * (el.height / r.height)); + return toPy((my - r.top) * (el.height / r.height)); + } + }); + Object.defineProperty(mod, 'CENTERX', { + get: function () { + if (lc_instance && lc_instance.CENTERX !== undefined) return toPy(lc_instance.CENTERX); + if (window.CENTERX !== undefined) return toPy(window.CENTERX); + let w = lc_instance ? lc_instance.W : (window.W || 0); + return toPy(w / 2); + } + }); + Object.defineProperty(mod, 'CENTERY', { + get: function () { + if (lc_instance && lc_instance.CENTERY !== undefined) return toPy(lc_instance.CENTERY); + if (window.CENTERY !== undefined) return toPy(window.CENTERY); + let h = lc_instance ? lc_instance.H : (window.H || 0); + return toPy(h / 2); } }); - Object.defineProperty(mod, 'CENTERX', { get: function () { return toPy(typeof CENTERX !== 'undefined' ? CENTERX : 0); } }); - Object.defineProperty(mod, 'CENTERY', { get: function () { return toPy(typeof CENTERY !== 'undefined' ? CENTERY : 0); } }); mod.PI = toPy(Math.PI); mod.TWO_PI = toPy(Math.PI * 2); @@ -223,4 +237,4 @@ var $builtinmodule = function (name) { mod.resume = new Sk.builtin.func(() => { resume(); return Sk.builtin.none.none$; }); return mod; -}; \ No newline at end of file +};