diff options
author | Jamie Pate <jpate@fortinet.com> | 2022-08-15 21:12:19 -0700 |
---|---|---|
committer | Jamie Pate <jpate@fortinet.com> | 2022-08-15 21:12:19 -0700 |
commit | c2e3971013b49c68bfa590b3b491d606b4f3adfb (patch) | |
tree | 0287e5448b3c8b6c523b6125c4fd3980fc2bf956 | |
parent | d5d22ab035a611a567f73a2ee2d61a81c99c61b5 (diff) |
Improve performance of screen_get_dpi() in Javascript
Replace a bisect with a single multiplication when calling
screen_get_dpi() in Javascript
Tested the value of
window.matchMedia(`(resolution:${(window.devicePixelRatio*96).toFixed(100)}dpi)`).matches
which is true except for values that cause a lot of rounding errors
(e.g. dpr : 0.3 => resolution: 28.799999999999997dpi)
Even in these cases the value matches the result of the previous
`findDPI()` method.
See also:
https://github.com/godotengine/godot/commit/6cff589b5bd483b563fe465bde74ca94902aab41#r81273660
-rw-r--r-- | platform/javascript/js/libs/library_godot_display.js | 28 |
1 files changed, 7 insertions, 21 deletions
diff --git a/platform/javascript/js/libs/library_godot_display.js b/platform/javascript/js/libs/library_godot_display.js index c7729a8c5b..768eaf9e1d 100644 --- a/platform/javascript/js/libs/library_godot_display.js +++ b/platform/javascript/js/libs/library_godot_display.js @@ -336,26 +336,12 @@ const GodotDisplay = { $GodotDisplay__deps: ['$GodotConfig', '$GodotRuntime', '$GodotDisplayCursor', '$GodotEventListeners', '$GodotDisplayScreen', '$GodotDisplayVK'], $GodotDisplay: { window_icon: '', - findDPI: function () { - function testDPI(dpi) { - return window.matchMedia(`(max-resolution: ${dpi}dpi)`).matches; - } - function bisect(low, high, func) { - const mid = parseInt(((high - low) / 2) + low, 10); - if (high - low <= 1) { - return func(high) ? high : low; - } - if (func(mid)) { - return bisect(low, mid, func); - } - return bisect(mid, high, func); - } - try { - const dpi = bisect(0, 800, testDPI); - return dpi >= 96 ? dpi : 96; - } catch (e) { - return 96; - } + getDPI: function () { + // devicePixelRatio is given in dppx + // https://drafts.csswg.org/css-values/#resolution + // > due to the 1:96 fixed ratio of CSS *in* to CSS *px*, 1dppx is equivalent to 96dpi. + const dpi = Math.round(window.devicePixelRatio * 96); + return dpi >= 96 ? dpi : 96; }, }, @@ -461,7 +447,7 @@ const GodotDisplay = { godot_js_display_screen_dpi_get__sig: 'i', godot_js_display_screen_dpi_get: function () { - return GodotDisplay.findDPI(); + return GodotDisplay.getDPI(); }, godot_js_display_pixel_ratio_get__sig: 'f', |