CanvasPath.cpp 4.1 KB

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