Skip to content

Commit 3f4dab1

Browse files
committed
feat(canvas): add rect() and ellipse()
1 parent bc1de9a commit 3f4dab1

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

src/client/canvas/rendering_context2d-inl.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,27 @@ namespace canvas
447447
skPaint->setBlendMode(globalCompositeOperation);
448448
}
449449

450+
template <typename CanvasType>
451+
void CanvasRenderingContext2D<CanvasType>::rect(float x, float y, float width, float height)
452+
{
453+
if (currentPath == nullptr)
454+
return;
455+
currentPath->addRect(SkRect::MakeXYWH(x, y, width, height));
456+
}
457+
458+
template <typename CanvasType>
459+
void CanvasRenderingContext2D<CanvasType>::ellipse(float x,
460+
float y,
461+
float radiusX,
462+
float radiusY,
463+
float rotation,
464+
float startAngle,
465+
float endAngle,
466+
bool ccw)
467+
{
468+
ellipseToSkPath(currentPath, x, y, radiusX, radiusY, rotation, startAngle, endAngle, ccw);
469+
}
470+
450471
template <typename CanvasType>
451472
const std::vector<float> &CanvasRenderingContext2D<CanvasType>::getLineDash()
452473
{

src/client/canvas/rendering_context2d.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ namespace canvas
7878
void stroke();
7979
void strokeRect(float x, float y, float width, float height);
8080
void clearRect(float x, float y, float width, float height);
81+
82+
void rect(float x, float y, float width, float height);
83+
void ellipse(float x,
84+
float y,
85+
float radiusX,
86+
float radiusY,
87+
float rotation,
88+
float startAngle,
89+
float endAngle,
90+
bool ccw = false);
91+
8192
const std::vector<float> &getLineDash();
8293
void setLineDash(const std::vector<float> &segments);
8394
void beginPath();

src/client/script_bindings/canvas/canvas_rendering_context_2d-inl.hpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,36 @@ namespace script_bindings::canvas_bindings
381381
args.GetReturnValue().SetUndefined();
382382
}
383383

384+
template <typename T, typename CanvasType>
385+
void CanvasRenderingContext2DBase<T, CanvasType>::Rect(const v8::FunctionCallbackInfo<v8::Value> &args)
386+
{
387+
v8::Isolate *isolate = args.GetIsolate();
388+
v8::HandleScope scope(isolate);
389+
390+
if (args.Length() < 4)
391+
{
392+
isolate->ThrowException(v8::Exception::TypeError(
393+
T::MakeMethodArgCountError(isolate, "rect", 4, args.Length())));
394+
return;
395+
}
396+
397+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
398+
auto x = args[0]->ToNumber(context).ToLocalChecked()->Value();
399+
auto y = args[1]->ToNumber(context).ToLocalChecked()->Value();
400+
auto width = args[2]->ToNumber(context).ToLocalChecked()->Value();
401+
auto height = args[3]->ToNumber(context).ToLocalChecked()->Value();
402+
403+
this->handle()->rect(x, y, width, height);
404+
args.GetReturnValue().SetUndefined();
405+
}
406+
384407
template <typename T, typename CanvasType>
385408
void CanvasRenderingContext2DBase<T, CanvasType>::Ellipse(const v8::FunctionCallbackInfo<v8::Value> &args)
386409
{
387410
v8::Isolate *isolate = args.GetIsolate();
388411
v8::HandleScope scope(isolate);
389412

390-
if (args.Length() < 8)
413+
if (args.Length() < 7)
391414
{
392415
isolate->ThrowException(v8::Exception::TypeError(
393416
T::MakeMethodArgCountError(isolate, "ellipse", 8, args.Length())));
@@ -404,14 +427,12 @@ namespace script_bindings::canvas_bindings
404427
auto endAngle = args[6]->ToNumber(context).ToLocalChecked()->Value();
405428
bool anticlockwise = false;
406429

407-
if (args.Length() > 7 && args[7]->IsBoolean())
430+
if (args.Length() > 7)
408431
{
409432
anticlockwise = args[7]->ToBoolean(isolate)->Value();
410433
}
411434

412-
// this->handle()->ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
413-
isolate->ThrowException(v8::Exception::Error(
414-
T::MakeMethodError(isolate, "ellipse", "Not implemented")));
435+
this->handle()->ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);
415436
args.GetReturnValue().SetUndefined();
416437
}
417438

src/client/script_bindings/canvas/canvas_rendering_context_2d.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ namespace script_bindings
4343
T::InstanceMethod(isolate, prototype, "bezierCurveTo", &T::BezierCurveTo);
4444
T::InstanceMethod(isolate, prototype, "quadraticCurveTo", &T::QuadraticCurveTo);
4545
T::InstanceMethod(isolate, prototype, "arc", &T::Arc);
46+
T::InstanceMethod(isolate, prototype, "rect", &T::Rect);
4647
T::InstanceMethod(isolate, prototype, "ellipse", &T::Ellipse);
4748

4849
// Drawing paths
@@ -133,8 +134,8 @@ namespace script_bindings
133134
void QuadraticCurveTo(const v8::FunctionCallbackInfo<v8::Value> &info);
134135
void Arc(const v8::FunctionCallbackInfo<v8::Value> &info);
135136
void ArcTo(const v8::FunctionCallbackInfo<v8::Value> &info);
136-
void Ellipse(const v8::FunctionCallbackInfo<v8::Value> &info);
137137
void Rect(const v8::FunctionCallbackInfo<v8::Value> &info);
138+
void Ellipse(const v8::FunctionCallbackInfo<v8::Value> &info);
138139

139140
// Drawing path methods
140141
void Fill(const v8::FunctionCallbackInfo<v8::Value> &info);

0 commit comments

Comments
 (0)