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.
This commit is contained in:
Andreas Kling 2020-04-08 11:22:40 +02:00
parent 2ffa054574
commit 348e209eb5
3 changed files with 30 additions and 28 deletions

View file

@ -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();
}

View file

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

View file

@ -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;
};