]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
[perf] simplify line drawing (#6575)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Fri, 18 Oct 2019 11:25:37 +0000 (04:25 -0700)
committerEvert Timberg <evert.timberg+github@gmail.com>
Fri, 18 Oct 2019 11:25:37 +0000 (07:25 -0400)
* Simplify line drawing

* Remove duplicate initialization

src/elements/element.line.js

index 5fa728bfe2cfcaa01553394f2a2361577005f0cd..782772bfbdc6234b8e3424316674375254dbc153 100644 (file)
@@ -40,7 +40,11 @@ module.exports = Element.extend({
                var closePath = me._loop;
                var index, previous, currentVM;
 
-               if (me._loop && points.length) {
+               if (!points.length) {
+                       return;
+               }
+
+               if (me._loop) {
                        for (index = 0; index < points.length; ++index) {
                                previous = helpers.previousItem(points, index);
                                // If the line has an open path, shift the point array
@@ -73,31 +77,27 @@ module.exports = Element.extend({
 
                // Stroke Line
                ctx.beginPath();
-               lastDrawnIndex = -1;
 
-               for (index = 0; index < points.length; ++index) {
-                       previous = helpers.previousItem(points, index);
+               // First point moves to it's starting position no matter what
+               currentVM = points[0]._view;
+               if (!currentVM.skip) {
+                       ctx.moveTo(currentVM.x, currentVM.y);
+                       lastDrawnIndex = 0;
+               }
+
+               for (index = 1; index < points.length; ++index) {
                        currentVM = points[index]._view;
+                       previous = lastDrawnIndex === -1 ? helpers.previousItem(points, index) : points[lastDrawnIndex];
 
-                       // First point moves to it's starting position no matter what
-                       if (index === 0) {
-                               if (!currentVM.skip) {
+                       if (!currentVM.skip) {
+                               if ((lastDrawnIndex !== (index - 1) && !spanGaps) || lastDrawnIndex === -1) {
+                                       // There was a gap and this is the first point after the gap
                                        ctx.moveTo(currentVM.x, currentVM.y);
-                                       lastDrawnIndex = index;
-                               }
-                       } else {
-                               previous = lastDrawnIndex === -1 ? previous : points[lastDrawnIndex];
-
-                               if (!currentVM.skip) {
-                                       if ((lastDrawnIndex !== (index - 1) && !spanGaps) || lastDrawnIndex === -1) {
-                                               // There was a gap and this is the first point after the gap
-                                               ctx.moveTo(currentVM.x, currentVM.y);
-                                       } else {
-                                               // Line to next point
-                                               helpers.canvas.lineTo(ctx, previous._view, currentVM);
-                                       }
-                                       lastDrawnIndex = index;
+                               } else {
+                                       // Line to next point
+                                       helpers.canvas.lineTo(ctx, previous._view, currentVM);
                                }
+                               lastDrawnIndex = index;
                        }
                }