1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:18:13 +00:00

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.
This commit is contained in:
Andreas Kling 2020-03-22 21:15:49 +01:00
parent 424a3f5ac3
commit 39045bfde8
5 changed files with 56 additions and 2 deletions

View file

@ -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*) {