CanvasPath.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/ExtraMathConstants.h>
  8. #include <LibWeb/HTML/Canvas/CanvasPath.h>
  9. #include <LibWeb/HTML/Window.h>
  10. namespace Web::HTML {
  11. void CanvasPath::close_path()
  12. {
  13. m_path.close();
  14. }
  15. void CanvasPath::move_to(float x, float y)
  16. {
  17. m_path.move_to({ x, y });
  18. }
  19. void CanvasPath::line_to(float x, float y)
  20. {
  21. m_path.line_to({ x, y });
  22. }
  23. void CanvasPath::quadratic_curve_to(float cx, float cy, float x, float y)
  24. {
  25. m_path.quadratic_bezier_curve_to({ cx, cy }, { x, y });
  26. }
  27. void CanvasPath::bezier_curve_to(double cp1x, double cp1y, double cp2x, double cp2y, double x, double y)
  28. {
  29. m_path.cubic_bezier_curve_to(Gfx::FloatPoint(cp1x, cp1y), Gfx::FloatPoint(cp2x, cp2y), Gfx::FloatPoint(x, y));
  30. }
  31. WebIDL::ExceptionOr<void> CanvasPath::arc(float x, float y, float radius, float start_angle, float end_angle, bool counter_clockwise)
  32. {
  33. if (radius < 0)
  34. return DOM::IndexSizeError::create(m_self.global_object(), String::formatted("The radius provided ({}) is negative.", radius));
  35. return ellipse(x, y, radius, radius, 0, start_angle, end_angle, counter_clockwise);
  36. }
  37. WebIDL::ExceptionOr<void> CanvasPath::ellipse(float x, float y, float radius_x, float radius_y, float rotation, float start_angle, float end_angle, bool counter_clockwise)
  38. {
  39. if (radius_x < 0)
  40. return DOM::IndexSizeError::create(m_self.global_object(), String::formatted("The major-axis radius provided ({}) is negative.", radius_x));
  41. if (radius_y < 0)
  42. return DOM::IndexSizeError::create(m_self.global_object(), String::formatted("The minor-axis radius provided ({}) is negative.", radius_y));
  43. if (constexpr float tau = M_TAU; (!counter_clockwise && (end_angle - start_angle) >= tau)
  44. || (counter_clockwise && (start_angle - end_angle) >= tau)) {
  45. start_angle = 0;
  46. end_angle = tau;
  47. } else {
  48. start_angle = fmodf(start_angle, tau);
  49. end_angle = fmodf(end_angle, tau);
  50. }
  51. // Then, figure out where the ends of the arc are.
  52. // To do so, we can pretend that the center of this ellipse is at (0, 0),
  53. // and the whole coordinate system is rotated `rotation` radians around the x axis, centered on `center`.
  54. // The sign of the resulting relative positions is just whether our angle is on one of the left quadrants.
  55. auto sin_rotation = sinf(rotation);
  56. auto cos_rotation = cosf(rotation);
  57. auto resolve_point_with_angle = [&](float angle) {
  58. auto tan_relative = tanf(angle);
  59. auto tan2 = tan_relative * tan_relative;
  60. auto ab = radius_x * radius_y;
  61. auto a2 = radius_x * radius_x;
  62. auto b2 = radius_y * radius_y;
  63. auto sqrt = sqrtf(b2 + a2 * tan2);
  64. auto relative_x_position = ab / sqrt;
  65. auto relative_y_position = ab * tan_relative / sqrt;
  66. // Make sure to set the correct sign
  67. float sn = sinf(angle) >= 0 ? 1 : -1;
  68. relative_x_position *= sn;
  69. relative_y_position *= sn;
  70. // Now rotate it (back) around the center point by 'rotation' radians, then move it back to our actual origin.
  71. auto relative_rotated_x_position = relative_x_position * cos_rotation - relative_y_position * sin_rotation;
  72. auto relative_rotated_y_position = relative_x_position * sin_rotation + relative_y_position * cos_rotation;
  73. return Gfx::FloatPoint { relative_rotated_x_position + x, relative_rotated_y_position + y };
  74. };
  75. auto start_point = resolve_point_with_angle(start_angle);
  76. auto end_point = resolve_point_with_angle(end_angle);
  77. m_path.move_to(start_point);
  78. double delta_theta = end_angle - start_angle;
  79. // FIXME: This is still goofy for some values.
  80. m_path.elliptical_arc_to(end_point, { radius_x, radius_y }, rotation, delta_theta > M_PI, !counter_clockwise);
  81. m_path.close();
  82. return {};
  83. }
  84. void CanvasPath::rect(float x, float y, float width, float height)
  85. {
  86. m_path.move_to({ x, y });
  87. if (width == 0 || height == 0)
  88. return;
  89. m_path.line_to({ x + width, y });
  90. m_path.line_to({ x + width, y + height });
  91. m_path.line_to({ x, y + height });
  92. m_path.close();
  93. }
  94. }