summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2021-04-15 18:41:13 +0430
committerAndreas Kling <kling@serenityos.org>2021-04-15 17:50:16 +0200
commit801daf47f04ef8a353783f8331ef260336670d72 (patch)
tree234455e3410963c7f741d8168c539ba569b7cd9f /Userland/Libraries/LibWeb
parent6c05d6d3706bc2d3ebda1fed641dfa9c903100f3 (diff)
downloadserenity-801daf47f04ef8a353783f8331ef260336670d72.zip
LibGfx+LibWeb: Wire up CanvasRenderingContext2D.ellipse()
Note that this is *extremely* naive, and not very good at being correct.
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp59
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h3
-rw-r--r--Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl3
3 files changed, 65 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
index 4ef8f59063..00bc5029f2 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.cpp
@@ -188,6 +188,65 @@ void CanvasRenderingContext2D::quadratic_curve_to(float cx, float cy, float x, f
m_path.quadratic_bezier_curve_to({ cx, cy }, { x, y });
}
+void CanvasRenderingContext2D::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
+{
+ ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
+}
+
+void CanvasRenderingContext2D::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
+{
+ if ((!counter_clockwise && (end_angle - start_angle) >= M_TAU)
+ || (counter_clockwise && (start_angle - end_angle) >= M_TAU)) {
+ start_angle = 0;
+ end_angle = M_TAU;
+ } else {
+ start_angle = fmodf(start_angle, M_TAU);
+ end_angle = fmodf(end_angle, M_TAU);
+ }
+
+ // Then, figure out where the ends of the arc are.
+ // To do so, we can pretend that the center of this ellipse is at (0, 0),
+ // and the whole coordinate system is rotated `rotation` radians around the x axis, centered on `center`.
+ // The sign of the resulting relative positions is just whether our angle is on one of the left quadrants.
+ auto sin_rotation = sinf(rotation);
+ auto cos_rotation = cosf(rotation);
+
+ auto resolve_point_with_angle = [&](float angle) {
+ auto tan_relative = tanf(angle);
+ auto tan2 = tan_relative * tan_relative;
+
+ auto ab = radius_x * radius_y;
+ auto a2 = radius_x * radius_x;
+ auto b2 = radius_y * radius_y;
+ auto sqrt = sqrtf(b2 + a2 * tan2);
+
+ auto relative_x_position = ab / sqrt;
+ auto relative_y_position = ab * tan_relative / sqrt;
+
+ // Make sure to set the correct sign
+ float sn = sinf(angle) >= 0 ? 1 : -1;
+ relative_x_position *= sn;
+ relative_y_position *= sn;
+
+ // Now rotate it (back) around the center point by 'rotation' radians, then move it back to our actual origin.
+ auto relative_rotated_x_position = relative_x_position * cos_rotation - relative_y_position * sin_rotation;
+ auto relative_rotated_y_position = relative_x_position * sin_rotation + relative_y_position * cos_rotation;
+ return Gfx::FloatPoint { relative_rotated_x_position + x, relative_rotated_y_position + y };
+ };
+
+ auto start_point = resolve_point_with_angle(start_angle);
+ auto end_point = resolve_point_with_angle(end_angle);
+
+ m_path.move_to(start_point);
+
+ auto delta_theta = end_angle - start_angle;
+
+ // FIXME: This is still goofy for some values.
+ m_path.elliptical_arc_to(end_point, { radius_x, radius_y }, rotation, delta_theta > M_PI, !counter_clockwise);
+
+ m_path.close();
+}
+
void CanvasRenderingContext2D::rect(float x, float y, float width, float height)
{
m_path.move_to({ x, y });
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
index 352ffba31b..c083f1b574 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.h
@@ -74,6 +74,9 @@ public:
void move_to(float x, float y);
void line_to(float x, float y);
void quadratic_curve_to(float cx, float cy, float x, float y);
+
+ void arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise);
+ void ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise);
void rect(float x, float y, float width, float height);
void stroke();
diff --git a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
index 5109c016a9..25b87241fb 100644
--- a/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
+++ b/Userland/Libraries/LibWeb/HTML/CanvasRenderingContext2D.idl
@@ -15,6 +15,9 @@ interface CanvasRenderingContext2D {
undefined moveTo(double x, double y);
undefined lineTo(double x, double y);
undefined quadraticCurveTo(double cpx, double cpy, double x, double y);
+
+ undefined arc(double x, double y, double radius, double startAngle, double endAngle, optional boolean counterclockwise = false);
+ undefined ellipse(double x, double y, double radiusX, double radiusY, double rotation, double startAngle, double endAngle, optional boolean counterclockwise = false);
undefined rect(double x, double y, double width, double height);
undefined drawImage(HTMLImageElement image, double dx, double dy);