summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-03-22 21:15:49 +0100
committerAndreas Kling <kling@serenityos.org>2020-03-22 21:18:03 +0100
commit39045bfde82be80052eda5e9502b43b22ef3eb9e (patch)
treea3dc6e9e0d1ac4f683f110929ad2111e2e9b30a2 /Libraries/LibWeb
parent424a3f5ac3c659d57d38aba36f4062c8ecbc4044 (diff)
downloadserenity-39045bfde82be80052eda5e9502b43b22ef3eb9e.zip
LibWeb: Add basic support for requestAnimationFrame()
We now support rAF, driven by GUI::DisplayLink callbacks. It's a bit strange how we keep registering new callbacks over and over. That's something we can definitely optimize. This allows you to update animations/whatever without doing it more often than the browser can display.
Diffstat (limited to 'Libraries/LibWeb')
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp14
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.h2
-rw-r--r--Libraries/LibWeb/DOM/Document.cpp17
3 files changed, 31 insertions, 2 deletions
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
index 4e2bc49d7b..393e26d2d3 100644
--- a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
@@ -30,7 +30,19 @@ void CanvasRenderingContext2D::fill_rect(int x, int y, int width, int height)
if (!painter)
return;
- painter->fill_rect({ x, y, width, height }, m_fill_style);
+ Gfx::Rect rect(x, y, width, height);
+ painter->fill_rect(rect, m_fill_style);
+ did_draw(rect);
+}
+
+void CanvasRenderingContext2D::did_draw(const Gfx::Rect&)
+{
+ // FIXME: Make use of the rect to reduce the invalidated area when possible.
+ if (!m_element)
+ return;
+ if (!m_element->layout_node())
+ return;
+ m_element->layout_node()->set_needs_display();
}
OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
index 8a36742015..ec84dfe815 100644
--- a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
@@ -28,6 +28,8 @@ public:
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
+ void did_draw(const Gfx::Rect&);
+
OwnPtr<Gfx::Painter> painter();
WeakPtr<HTMLCanvasElement> m_element;
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index f4a120bc6e..4f7498c2ec 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -28,6 +28,7 @@
#include <AK/StringBuilder.h>
#include <LibCore/Timer.h>
#include <LibGUI/Application.h>
+#include <LibGUI/DisplayLink.h>
#include <LibGUI/MessageBox.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Function.h>
@@ -360,11 +361,25 @@ JS::Interpreter& Document::interpreter()
(void)Core::Timer::construct(
arguments[1].to_i32(), [this, callback] {
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter, {});
- }).leak_ref();
+ })
+ .leak_ref();
return JS::js_undefined();
});
+ m_interpreter->global_object().put_native_function("requestAnimationFrame", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
+ if (arguments.size() < 1)
+ return JS::js_undefined();
+ ASSERT(arguments[0].is_object());
+ ASSERT(arguments[0].as_object()->is_function());
+ auto callback = make_handle(const_cast<JS::Object*>(arguments[0].as_object()));
+ i32 link_id = GUI::DisplayLink::register_callback([this, callback](i32 link_id) {
+ const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter, {});
+ GUI::DisplayLink::unregister_callback(link_id);
+ });
+ return JS::Value(link_id);
+ });
+
m_interpreter->global_object().put_native_property(
"document",
[this](JS::Object*) {