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

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: Test uses alpha color to check darkening of paths on the double-rasterized region. Created 7 years, 4 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
« no previous file with comments | « Source/core/html/canvas/CanvasPathMethods.h ('k') | Source/core/platform/graphics/Path.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: Source/core/html/canvas/CanvasPathMethods.cpp
diff --git a/Source/core/html/canvas/CanvasPathMethods.cpp b/Source/core/html/canvas/CanvasPathMethods.cpp
index affead7696a8b644580c71a04c9faec406809075..d4b9c784d9a542386b58a5adfaf627133cf777a6 100644
--- a/Source/core/html/canvas/CanvasPathMethods.cpp
+++ b/Source/core/html/canvas/CanvasPathMethods.cpp
@@ -130,36 +130,60 @@ 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, ExceptionState& es)
+static float adjustEndAngle(float startAngle, float endAngle, bool anticlockwise)
{
- if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(r) || !std::isfinite(sa) || !std::isfinite(ea))
- return;
+ 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 2pi, or,
+ * if the anticlockwise argument is true and startAngle-endAngle is equal to or greater than 2pi,
+ * 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 2pi 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.
+ */
+ 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;
+}
- if (r < 0) {
- es.throwDOMException(IndexSizeError);
+void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, float endAngle, bool anticlockwise, ExceptionState& es)
+{
+ if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radius) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
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));
+ if (radius < 0) {
+ es.throwDOMException(IndexSizeError);
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)
« no previous file with comments | « Source/core/html/canvas/CanvasPathMethods.h ('k') | Source/core/platform/graphics/Path.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698