mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:58:13 +00:00
LibWeb: Add canvas.strokeRect(), and fix scale & translate
Add an implementation of CanvasRenderingContext2DWrapper.strokeRect(). While implementing this I fixed fillRect() and the new strokeRect() to honor the .scale() and .translate() values that had previously been plumbed. Also enhance the canvas.html demo to utilize strokeRect(), scale(), and translate().
This commit is contained in:
parent
7291d5c86f
commit
39855fe9ef
5 changed files with 80 additions and 3 deletions
|
@ -56,11 +56,32 @@ void CanvasRenderingContext2D::fill_rect(int x, int y, int width, int height)
|
|||
if (!painter)
|
||||
return;
|
||||
|
||||
Gfx::Rect rect(x, y, width, height);
|
||||
Gfx::Rect rect = compute_rect(x, y, width, height);
|
||||
painter->fill_rect(rect, m_fill_style);
|
||||
did_draw(rect);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::set_stroke_style(String style)
|
||||
{
|
||||
m_stroke_style = Gfx::Color::from_string(style).value_or(Color::Black);
|
||||
}
|
||||
|
||||
String CanvasRenderingContext2D::stroke_style() const
|
||||
{
|
||||
return m_fill_style.to_string();
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::stroke_rect(int x, int y, int width, int height)
|
||||
{
|
||||
auto painter = this->painter();
|
||||
if (!painter)
|
||||
return;
|
||||
|
||||
Gfx::Rect rect = compute_rect(x, y, width, height);
|
||||
painter->draw_rect(rect, m_stroke_style);
|
||||
did_draw(rect);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::scale(double sx, double sy)
|
||||
{
|
||||
// FIXME: Actually do something with the scale factor!
|
||||
|
@ -77,6 +98,14 @@ void CanvasRenderingContext2D::translate(double x, double y)
|
|||
m_translate_y = y;
|
||||
}
|
||||
|
||||
Gfx::Rect CanvasRenderingContext2D::compute_rect(int x, int y, int width, int 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);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2D::did_draw(const Gfx::Rect&)
|
||||
{
|
||||
// FIXME: Make use of the rect to reduce the invalidated area when possible.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue