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

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

Issue 14298022: Add support for new canvas ellipse method. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Rebase to upstream. Make canvas-ellipse-circumference cover more extensive cases. 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
Index: Source/core/html/canvas/CanvasPathMethods.cpp
diff --git a/Source/core/html/canvas/CanvasPathMethods.cpp b/Source/core/html/canvas/CanvasPathMethods.cpp
index d4b9c784d9a542386b58a5adfaf627133cf777a6..29e9a6a79db90b2003910a7ea8f3a8425ac17139 100644
--- a/Source/core/html/canvas/CanvasPathMethods.cpp
+++ b/Source/core/html/canvas/CanvasPathMethods.cpp
@@ -38,6 +38,7 @@
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/platform/graphics/FloatRect.h"
+#include "core/platform/graphics/transforms/AffineTransform.h"
#include "wtf/MathExtras.h"
namespace WebCore {
@@ -186,6 +187,80 @@ void CanvasPathMethods::arc(float x, float y, float radius, float startAngle, fl
m_path.addArc(FloatPoint(x, y), radius, startAngle, adjustedEndAngle, anticlockwise);
}
+inline static void lineToFloatPoint(CanvasPathMethods* path, const FloatPoint& p)
+{
+ path->lineTo(p.x(), p.y());
+}
+
+inline static FloatPoint getPointOnEllipse(float radiusX, float radiusY, float theta)
+{
+ return FloatPoint(radiusX * cosf(theta), radiusY * sinf(theta));
+}
+
+inline static void canonicalizeAngle(float* startAngle, float* endAngle)
+{
+ // Make 0 <= startAngle < 2*PI
+ float twoPi = 2 * piFloat;
+ float newStartAngle = *startAngle;
+ if (newStartAngle < 0)
+ newStartAngle = twoPi + fmodf(newStartAngle, -twoPi);
+ else
+ newStartAngle = fmodf(newStartAngle, twoPi);
+
+ float delta = newStartAngle - *startAngle;
+ *startAngle = newStartAngle;
+ *endAngle = *endAngle + delta;
+}
+
+static void degenerateEllipse(CanvasPathMethods* path, float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise)
+{
+ ASSERT(std::abs(endAngle - startAngle) < 4 * piFloat);
+
+ FloatPoint center(x, y);
+ AffineTransform rotationMatrix;
+ rotationMatrix.rotate(rad2deg(rotation));
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, startAngle)));
+ if ((!radiusX && !radiusY) || startAngle == endAngle)
+ return;
+
+ canonicalizeAngle(&startAngle, &endAngle);
+ ASSERT(std::abs(endAngle - startAngle) < 4 * piFloat);
+
+ float halfPiFloat = piFloat * 0.5;
+ if (!anticlockwise) {
+ for (float angle = startAngle - fmodf(startAngle, halfPiFloat) + halfPiFloat; angle < endAngle; angle += halfPiFloat)
Stephen White 2013/08/27 14:23:50 I'm a little confused by this loop (and the one be
Justin Novosad 2013/08/27 17:51:03 I think the idea to to generate a flat quad to dra
dshwang 2013/08/27 18:54:17 I added detail explanations as comments. To verify
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
+ } else {
+ for (float angle = startAngle - fmodf(startAngle, halfPiFloat); angle > endAngle; angle -= halfPiFloat)
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, angle)));
+ }
+
+ lineToFloatPoint(path, center + rotationMatrix.mapPoint(getPointOnEllipse(radiusX, radiusY, endAngle)));
+}
+
+void CanvasPathMethods::ellipse(float x, float y, float radiusX, float radiusY, float rotation, float startAngle, float endAngle, bool anticlockwise, ExceptionState& es)
+{
+ if (!std::isfinite(x) || !std::isfinite(y) || !std::isfinite(radiusX) || !std::isfinite(radiusY) || !std::isfinite(rotation) || !std::isfinite(startAngle) || !std::isfinite(endAngle))
+ return;
+
+ if (radiusX < 0 || radiusY < 0) {
+ es.throwDOMException(IndexSizeError);
+ return;
+ }
+
+ if (!isTransformInvertible())
Justin Novosad 2013/08/27 17:51:03 This early exit is not in the spec. If the implem
dshwang 2013/08/27 18:54:17 Good concern. However, all primitives early exit a
+ return;
+
+ float adjustedEndAngle = adjustEndAngle(startAngle, endAngle, anticlockwise);
+ if (!radiusX || !radiusY || startAngle == adjustedEndAngle) {
+ // The ellipse is empty but we still need to draw the connecting line to start point.
+ degenerateEllipse(this, x, y, radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
+ return;
+ }
+
+ m_path.addEllipse(FloatPoint(x, y), radiusX, radiusY, rotation, startAngle, adjustedEndAngle, anticlockwise);
+}
+
void CanvasPathMethods::rect(float x, float y, float width, float height)
{
if (!isTransformInvertible())

Powered by Google App Engine
This is Rietveld 408576698