[JS] Check canvas size each loop, force redraw.

Fix compatibility issues, achieve smoother resizing.
This commit is contained in:
Fabio Alessandrelli 2020-06-29 18:54:20 +02:00
parent a1c4c1d318
commit 07d4513886
3 changed files with 26 additions and 0 deletions

View file

@ -68,6 +68,20 @@ bool DisplayServerJavaScript::is_canvas_focused() {
/* clang-format on */
}
bool DisplayServerJavaScript::check_size_force_redraw() {
int canvas_width;
int canvas_height;
emscripten_get_canvas_element_size(DisplayServerJavaScript::canvas_id, &canvas_width, &canvas_height);
if (last_width != canvas_width || last_height != canvas_height) {
last_width = canvas_width;
last_height = canvas_height;
// Update the framebuffer size and for redraw.
emscripten_set_canvas_element_size(DisplayServerJavaScript::canvas_id, canvas_width, canvas_height);
return true;
}
return false;
}
Point2 DisplayServerJavaScript::compute_position_in_canvas(int p_x, int p_y) {
int canvas_x = EM_ASM_INT({
return Module['canvas'].getBoundingClientRect().x;
@ -1080,6 +1094,8 @@ Size2i DisplayServerJavaScript::window_get_min_size(WindowID p_window) const {
}
void DisplayServerJavaScript::window_set_size(const Size2i p_size, WindowID p_window) {
last_width = p_size.x;
last_height = p_size.y;
emscripten_set_canvas_element_size(canvas_id, p_size.x, p_size.y);
}

View file

@ -53,6 +53,9 @@ class DisplayServerJavaScript : public DisplayServer {
double last_click_ms = 0;
int last_click_button_index = -1;
int last_width = 0;
int last_height = 0;
// utilities
static Point2 compute_position_in_canvas(int p_x, int p_y);
static void focus_canvas();
@ -104,6 +107,9 @@ public:
Callable input_text_callback;
Callable drop_files_callback;
// utilities
bool check_size_force_redraw();
// from DisplayServer
virtual void alert(const String &p_alert, const String &p_title = "ALERT!");
virtual bool has_feature(Feature p_feature) const;

View file

@ -47,6 +47,10 @@ void exit_callback() {
}
void main_loop_callback() {
bool force_draw = DisplayServerJavaScript::get_singleton()->check_size_force_redraw();
if (force_draw) {
Main::force_redraw();
}
if (os->main_loop_iterate()) {
emscripten_cancel_main_loop(); // Cancel current loop and wait for finalize_async.
EM_ASM({