diff options
-rw-r--r-- | Base/home/anon/www/raf.html | 24 | ||||
-rw-r--r-- | Base/home/anon/www/welcome.html | 1 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp | 14 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/CanvasRenderingContext2D.h | 2 | ||||
-rw-r--r-- | Libraries/LibWeb/DOM/Document.cpp | 17 |
5 files changed, 56 insertions, 2 deletions
diff --git a/Base/home/anon/www/raf.html b/Base/home/anon/www/raf.html new file mode 100644 index 0000000000..25b9642b7a --- /dev/null +++ b/Base/home/anon/www/raf.html @@ -0,0 +1,24 @@ +<!DOCTYPE html> +<html> +<head><title>rAF test</title></head> +<body> +<canvas id=c width=300 height=300></canvas> +<script> +c = document.getElementById('c'); +x = c.getContext("2d"); +x.fillStyle = 'black'; +x.fillRect(0, 0, c.width, c.height); +function raf() { + x.fillStyle = 'red'; + x.fillRect( + Math.random() * c.width, + Math.random() * c.height, + Math.random() * 10, + Math.random() * 10 + ); + requestAnimationFrame(raf); +} +requestAnimationFrame(raf); +</script> +</body> +</html> diff --git a/Base/home/anon/www/welcome.html b/Base/home/anon/www/welcome.html index c2bb81c828..d4d99a920a 100644 --- a/Base/home/anon/www/welcome.html +++ b/Base/home/anon/www/welcome.html @@ -24,6 +24,7 @@ h1 { <p>Some small test pages:</p> <ul> <li><a href="demo.html">fun demo</a></li> + <li><a href="raf.html">requestAnimationFrame test</a></li> <li><a href="canvas.html">canvas 2D test</a></li> <li><a href="events.html">simple DOM events test</a></li> <li><a href="dom.html">simple DOM JS test</a></li> 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*) { |