summaryrefslogtreecommitdiff
path: root/Libraries
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-08 11:22:40 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-08 17:19:46 +0200
commit348e209eb527d94da5bd51d5660d9f662c952a54 (patch)
treec6674b8c42948dfd0e2621ecd6d6245b784d212d /Libraries
parent2ffa054574839b7f1e2c44e8a26e62b0d632d01e (diff)
downloadserenity-348e209eb527d94da5bd51d5660d9f662c952a54.zip
LibWeb: Make CanvasRenderingContext2D use floats instead of ints
This matches what we already do for the layout tree and things are expected to work this way regardless.
Diffstat (limited to 'Libraries')
-rw-r--r--Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp8
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp30
-rw-r--r--Libraries/LibWeb/DOM/CanvasRenderingContext2D.h20
3 files changed, 30 insertions, 28 deletions
diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
index 37ba819612..63eb9bc9c2 100644
--- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
+++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp
@@ -71,7 +71,7 @@ JS::Value CanvasRenderingContext2DWrapper::fill_rect(JS::Interpreter& interprete
return {};
auto& arguments = interpreter.call_frame().arguments;
if (arguments.size() >= 4)
- impl->fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
+ impl->fill_rect(arguments[0].to_double(), arguments[1].to_double(), arguments[2].to_double(), arguments[3].to_double());
return JS::js_undefined();
}
@@ -82,7 +82,7 @@ JS::Value CanvasRenderingContext2DWrapper::stroke_rect(JS::Interpreter& interpre
return {};
auto& arguments = interpreter.call_frame().arguments;
if (arguments.size() >= 4)
- impl->stroke_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
+ impl->stroke_rect(arguments[0].to_double(), arguments[1].to_double(), arguments[2].to_double(), arguments[3].to_double());
return JS::js_undefined();
}
@@ -93,7 +93,7 @@ JS::Value CanvasRenderingContext2DWrapper::scale(JS::Interpreter& interpreter)
return {};
auto& arguments = interpreter.call_frame().arguments;
if (arguments.size() >= 2)
- impl->scale(arguments[0].to_number().as_double(), arguments[1].to_number().as_double());
+ impl->scale(arguments[0].to_double(), arguments[1].to_double());
return JS::js_undefined();
}
@@ -104,7 +104,7 @@ JS::Value CanvasRenderingContext2DWrapper::translate(JS::Interpreter& interprete
return {};
auto& arguments = interpreter.call_frame().arguments;
if (arguments.size() >= 2)
- impl->translate(arguments[0].to_number().as_double(), arguments[1].to_number().as_double());
+ impl->translate(arguments[0].to_double(), arguments[1].to_double());
return JS::js_undefined();
}
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
index 3f4f6b885c..b89ba9e5fc 100644
--- a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.cpp
@@ -50,14 +50,14 @@ String CanvasRenderingContext2D::fill_style() const
return m_fill_style.to_string();
}
-void CanvasRenderingContext2D::fill_rect(int x, int y, int width, int height)
+void CanvasRenderingContext2D::fill_rect(float x, float y, float width, float height)
{
auto painter = this->painter();
if (!painter)
return;
- Gfx::Rect rect = compute_rect(x, y, width, height);
- painter->fill_rect(rect, m_fill_style);
+ Gfx::FloatRect rect = compute_rect(x, y, width, height);
+ painter->fill_rect(enclosing_int_rect(rect), m_fill_style);
did_draw(rect);
}
@@ -71,18 +71,18 @@ String CanvasRenderingContext2D::stroke_style() const
return m_fill_style.to_string();
}
-void CanvasRenderingContext2D::stroke_rect(int x, int y, int width, int height)
+void CanvasRenderingContext2D::stroke_rect(float x, float y, float width, float height)
{
auto painter = this->painter();
if (!painter)
return;
- Gfx::Rect rect = compute_rect(x, y, width, height);
- painter->draw_rect(rect, m_stroke_style);
+ Gfx::FloatRect rect = compute_rect(x, y, width, height);
+ painter->draw_rect(enclosing_int_rect(rect), m_stroke_style);
did_draw(rect);
}
-void CanvasRenderingContext2D::scale(double sx, double sy)
+void CanvasRenderingContext2D::scale(float sx, float sy)
{
// FIXME: Actually do something with the scale factor!
dbg() << "CanvasRenderingContext2D::scale(): " << String::format("%f", sx) << ", " << String::format("%f", sy);
@@ -90,7 +90,7 @@ void CanvasRenderingContext2D::scale(double sx, double sy)
m_scale_y = sy;
}
-void CanvasRenderingContext2D::translate(double x, double y)
+void CanvasRenderingContext2D::translate(float x, float y)
{
// FIXME: Actually do something with the translation!
dbg() << "CanvasRenderingContext2D::translate(): " << String::format("%f", x) << ", " << String::format("%f", y);
@@ -98,15 +98,17 @@ void CanvasRenderingContext2D::translate(double x, double y)
m_translate_y = y;
}
-Gfx::Rect CanvasRenderingContext2D::compute_rect(int x, int y, int width, int height)
+Gfx::FloatRect CanvasRenderingContext2D::compute_rect(float x, float y, float width, float height)
{
- return Gfx::Rect((x + m_translate_x) * m_scale_x,
- (y + m_translate_y) * m_scale_y,
- width * m_scale_x,
- height * m_scale_y);
+ return {
+ (x + m_translate_x) * m_scale_x,
+ (y + m_translate_y) * m_scale_y,
+ width * m_scale_x,
+ height * m_scale_y
+ };
}
-void CanvasRenderingContext2D::did_draw(const Gfx::Rect&)
+void CanvasRenderingContext2D::did_draw(const Gfx::FloatRect&)
{
// FIXME: Make use of the rect to reduce the invalidated area when possible.
if (!m_element)
diff --git a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
index adb46295a6..f391eb3b62 100644
--- a/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
+++ b/Libraries/LibWeb/DOM/CanvasRenderingContext2D.h
@@ -52,25 +52,25 @@ public:
void set_stroke_style(String);
String stroke_style() const;
- void fill_rect(int x, int y, int width, int height);
- void stroke_rect(int x, int y, int width, int height);
- void scale(double sx, double sy);
- void translate(double x, double y);
+ void fill_rect(float x, float y, float width, float height);
+ void stroke_rect(float x, float y, float width, float height);
+ void scale(float sx, float sy);
+ void translate(float x, float y);
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
- Gfx::Rect compute_rect(int x, int y, int width, int height);
- void did_draw(const Gfx::Rect&);
+ Gfx::FloatRect compute_rect(float x, float y, float width, float height);
+ void did_draw(const Gfx::FloatRect&);
OwnPtr<Gfx::Painter> painter();
WeakPtr<HTMLCanvasElement> m_element;
- double m_scale_x { 1 };
- double m_scale_y { 1 };
- double m_translate_x { 0 };
- double m_translate_y { 0 };
+ float m_scale_x { 1 };
+ float m_scale_y { 1 };
+ float m_translate_x { 0 };
+ float m_translate_y { 0 };
Gfx::Color m_fill_style;
Gfx::Color m_stroke_style;
};