Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(577)

Unified Diff: Source/core/html/canvas/CanvasPathMethods.cpp

Issue 18286007: Fix edge case bugs of canvas arc. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@ellipse
Patch Set: Add comment for alph's edge case. Make canvas-arc-circumference.html cover 160 cases as alph sugges… Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/core/html/canvas/CanvasPathMethods.cpp
diff --git a/Source/core/html/canvas/CanvasPathMethods.cpp b/Source/core/html/canvas/CanvasPathMethods.cpp
index a2d4e75549062dcf5a1c71f63fc3f2b82847c164..c89650f9ce33fc28141f04ebe2ea0a0ef54dd890 100644
--- a/Source/core/html/canvas/CanvasPathMethods.cpp
+++ b/Source/core/html/canvas/CanvasPathMethods.cpp
@@ -130,37 +130,61 @@ void CanvasPathMethods::arcTo(float x1, float y1, float x2, float y2, float r, E
m_path.addArcTo(p1, p2, r);
}
-void CanvasPathMethods::arc(float x, float y, float r, float sa, float ea, bool anticlockwise, ExceptionCode& ec)
+static float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
+{
+ float twoPi = 2 * piFloat;
+ float newEndAngle = endAngle;
+ /* http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-arc
+ * If the anticlockwise argument is false and endAngle-startAngle is equal to or greater than 2π, or,
Stephen White 2013/08/22 15:08:29 I'm not sure if unicode characters in C++ source a
+ * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2π,
+ * then the arc is the whole circumference of this ellipse.
+ */
+ if (!anticlockwise && endAngle - startAngle >= twoPi)
+ newEndAngle = startAngle + twoPi + fmodf(endAngle - startAngle, twoPi);
+ else if (anticlockwise && startAngle - endAngle >= twoPi)
+ newEndAngle = startAngle - twoPi - fmodf(startAngle - endAngle, twoPi);
+
+ /*
+ * Otherwise, the arc is the path along the circumference of this ellipse from the start point to the end point,
+ * going anti-clockwise if the anticlockwise argument is true, and clockwise otherwise.
+ * Since the points are on the ellipse, as opposed to being simply angles from zero,
+ * the arc can never cover an angle greater than 2π radians.
+ */
+ /* NOTE: When startAngle = 0, endAngle = 2Pi and anticlockwise = true, the spec does not indicate clearly.
+ * We draw the entire circle, because some web sites use arc(x, y, r, 0, 2*Math.PI, true) to draw circle.
+ * We preserve backward-compatibility.
+ */
dshwang 2013/07/10 16:53:02 Add comment for alph's edge case.
+ else if (!anticlockwise && startAngle > endAngle)
+ newEndAngle = startAngle + (twoPi - fmodf(startAngle - endAngle, twoPi));
+ else if (anticlockwise && startAngle < endAngle)
+ newEndAngle = startAngle - (twoPi - fmodf(endAngle - startAngle, twoPi));
+
+ ASSERT(std::abs(newEndAngle - startAngle) < 4 * piFloat);
+ return newEndAngle;
+}
+
+void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionCode& ec)
{
ec = 0;
- if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(r) || !std::isfinite(sa) || !std::isfinite(ea))
+ if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
return;
- if (r < 0) {
+ if (radius < 0) {
ec = IndexSizeError;
return;
}
- if (!r || sa == ea) {
- // The arc is empty but we still need to draw the connecting line.
- lineTo(x + r * cosf(sa), y + r * sinf(sa));
- return;
- }
-
if (!isTransformInvertible())
return;
- // If 'sa' and 'ea' differ by more than 2Pi, just add a circle starting/ending at 'sa'.
- if (anticlockwise && sa - ea >= 2 * piFloat) {
- m_path.addArc(FloatPoint(x, y), r, sa, sa - 2 * piFloat, anticlockwise);
- return;
- }
- if (!anticlockwise && ea - sa >= 2 * piFloat) {
- m_path.addArc(FloatPoint(x, y), r, sa, sa + 2 * piFloat, anticlockwise);
+ if (!radius || startAngle == endAngle) {
+ // The arc is empty but we still need to draw the connecting line.
+ lineTo(x + radius * cosf(startAngle), y + radius * sinf(startAngle));
return;
}
- m_path.addArc(FloatPoint(x, y), r, sa, ea, anticlockwise);
+ float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
+ m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
}
void CanvasPathMethods::rect(float x, float y, float width, float height)

Powered by Google App Engine
This is Rietveld 408576698