From d434ae71b3281ed0f90ecbcd1056bc4b93ea1a3a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 15 Mar 2021 19:40:42 +0100 Subject: [PATCH] LibWeb: Add CanvasRenderingContext2D.clearRect() Similar to fillRect, except this API fills with transparent black. --- .../LibWeb/HTML/CanvasRenderingContext2D.cpp | 11 +++++++++++ .../Libraries/LibWeb/HTML/CanvasRenderingContext2D.h | 1 + .../LibWeb/HTML/CanvasRenderingContext2D.idl | 1 + 3 files changed, 13 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp index 3c08f1e73cf..ebb7640ce65 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp @@ -64,6 +64,17 @@ void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float he did_draw(rect); } +void CanvasRenderingContext2D::clear_rect(float x, float y, float width, float height) +{ + auto painter = this->painter(); + if (!painter) + return; + + auto rect = m_transform.map(Gfx::FloatRect(x, y, width, height)); + painter->clear_rect(enclosing_int_rect(rect), Color()); + did_draw(rect); +} + void CanvasRenderingContext2D::set_stroke_style(String style) { m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h index 58cccfc6e6e..8a8a1f860f9 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h @@ -57,6 +57,7 @@ public: void fill_rect(float x, float y, float width, float height); void stroke_rect(float x, float y, float width, float height); + void clear_rect(float x, float y, float width, float height); void draw_image(const HTMLImageElement&, float x, float y); diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl index bcfa544c674..97f0462601a 100644 --- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl +++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl @@ -2,6 +2,7 @@ interface CanvasRenderingContext2D { undefined fillRect(double x, double y, double w, double h); undefined strokeRect(double x, double y, double w, double h); + undefined clearRect(double x, double y, double w, double h); undefined scale(double x, double y); undefined translate(double x, double y);