1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:37:35 +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

@ -53,6 +53,14 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
put_native_property("strokeStyle", stroke_style_getter, stroke_style_setter);
put_native_function("strokeRect", stroke_rect, 4);
put_native_function("drawImage", draw_image, 3);
put_native_function("beginPath", begin_path, 0);
put_native_function("closePath", close_path, 0);
put_native_function("stroke", stroke, 0);
put_native_function("moveTo", move_to, 0);
put_native_function("lineTo", line_to, 0);
put_native_property("lineWidth", line_width_getter, line_width_setter);
}
CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper()
@ -162,5 +170,68 @@ void CanvasRenderingContext2DWrapper::stroke_style_setter(JS::Interpreter& inter
impl->set_stroke_style(value.to_string());
}
JS::Value CanvasRenderingContext2DWrapper::line_width_getter(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
return JS::Value(impl->line_width());
}
void CanvasRenderingContext2DWrapper::line_width_setter(JS::Interpreter& interpreter, JS::Value value)
{
if (auto* impl = impl_from(interpreter))
impl->set_line_width(value.to_double());
}
JS::Value CanvasRenderingContext2DWrapper::begin_path(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
impl->begin_path();
return JS::js_undefined();
}
JS::Value CanvasRenderingContext2DWrapper::close_path(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
impl->close_path();
return JS::js_undefined();
}
JS::Value CanvasRenderingContext2DWrapper::stroke(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
impl->stroke();
return JS::js_undefined();
}
JS::Value CanvasRenderingContext2DWrapper::move_to(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
double x = interpreter.argument(0).to_double();
double y = interpreter.argument(1).to_double();
impl->move_to(x, y);
return JS::js_undefined();
}
JS::Value CanvasRenderingContext2DWrapper::line_to(JS::Interpreter& interpreter)
{
auto* impl = impl_from(interpreter);
if (!impl)
return {};
double x = interpreter.argument(0).to_double();
double y = interpreter.argument(1).to_double();
impl->line_to(x, y);
return JS::js_undefined();
}
}
}

View file

@ -51,6 +51,13 @@ private:
static void fill_style_setter(JS::Interpreter&, JS::Value);
static JS::Value stroke_style_getter(JS::Interpreter&);
static void stroke_style_setter(JS::Interpreter&, JS::Value);
static JS::Value line_width_getter(JS::Interpreter&);
static void line_width_setter(JS::Interpreter&, JS::Value);
static JS::Value begin_path(JS::Interpreter&);
static JS::Value close_path(JS::Interpreter&);
static JS::Value stroke(JS::Interpreter&);
static JS::Value move_to(JS::Interpreter&);
static JS::Value line_to(JS::Interpreter&);
NonnullRefPtr<CanvasRenderingContext2D> m_impl;
};