1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 14:37:46 +00:00

LibWeb: Add some basic path drawing functionality to the canvas element

This patch adds the following methods to CanvasRenderingContext2D:

- beginPath()
- moveTo(x, y)
- lineTo(x, y)
- closePath()
- stroke()

We also add the lineWidth property. :^)
This commit is contained in:
Andreas Kling 2020-04-16 21:06:03 +02:00
parent 60c2e41079
commit 0d93e249c3
6 changed files with 163 additions and 0 deletions

View file

@ -134,4 +134,35 @@ OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
return make<Gfx::Painter>(*m_element->bitmap());
}
void CanvasRenderingContext2D::begin_path()
{
m_path = Gfx::Path();
}
void CanvasRenderingContext2D::close_path()
{
m_path.close();
}
void CanvasRenderingContext2D::move_to(float x, float y)
{
m_path.move_to({ x, y });
}
void CanvasRenderingContext2D::line_to(float x, float y)
{
m_path.line_to({ x, y });
}
void CanvasRenderingContext2D::stroke()
{
dbg() << "stroke path " << m_path;
auto painter = this->painter();
if (!painter)
return;
painter->stroke_path(m_path, m_stroke_style, m_line_width);
}
}

View file

@ -30,6 +30,7 @@
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Path.h>
#include <LibWeb/Bindings/Wrappable.h>
namespace Web {
@ -61,6 +62,15 @@ public:
void scale(float sx, float sy);
void translate(float x, float y);
void set_line_width(float line_width) { m_line_width = line_width; }
float line_width() const { return m_line_width; }
void begin_path();
void close_path();
void move_to(float x, float y);
void line_to(float x, float y);
void stroke();
private:
explicit CanvasRenderingContext2D(HTMLCanvasElement&);
@ -73,6 +83,9 @@ private:
Gfx::AffineTransform m_transform;
Gfx::Color m_fill_style;
Gfx::Color m_stroke_style;
float m_line_width { 1 };
Gfx::Path m_path;
};
}