mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-27 23:09:08 +00:00
This patch adds HTMLCanvasElement along with a LayoutCanvas object. The DOM and layout parts are very similar to <img> elements. The <canvas> element holds a Gfx::Bitmap which is sized according to the "width" and "height" attributes on the element. Calling .getContext("2d") on a <canvas> element gives you a context object that draws into the underlying Gfx::Bitmap of the <canvas>. The context weakly points to the <canvas> which allows it to outlive the canvas element if needed. This is really quite cool. :^)
44 lines
962 B
C++
44 lines
962 B
C++
#include <AK/OwnPtr.h>
|
|
#include <LibGfx/Painter.h>
|
|
#include <LibWeb/DOM/CanvasRenderingContext2D.h>
|
|
#include <LibWeb/DOM/HTMLCanvasElement.h>
|
|
|
|
namespace Web {
|
|
|
|
CanvasRenderingContext2D::CanvasRenderingContext2D(HTMLCanvasElement& element)
|
|
: m_element(element.make_weak_ptr())
|
|
{
|
|
}
|
|
|
|
CanvasRenderingContext2D::~CanvasRenderingContext2D()
|
|
{
|
|
}
|
|
|
|
void CanvasRenderingContext2D::set_fill_style(String style)
|
|
{
|
|
m_fill_style = Gfx::Color::from_string(style).value_or(Color::Black);
|
|
}
|
|
|
|
String CanvasRenderingContext2D::fill_style() const
|
|
{
|
|
return m_fill_style.to_string();
|
|
}
|
|
|
|
void CanvasRenderingContext2D::fill_rect(int x, int y, int width, int height)
|
|
{
|
|
auto painter = this->painter();
|
|
if (!painter)
|
|
return;
|
|
|
|
painter->fill_rect({ x, y, width, height }, m_fill_style);
|
|
}
|
|
|
|
OwnPtr<Gfx::Painter> CanvasRenderingContext2D::painter()
|
|
{
|
|
if (!m_element)
|
|
return nullptr;
|
|
|
|
return make<Gfx::Painter>(m_element->ensure_bitmap());
|
|
}
|
|
|
|
}
|