From 61174cd61e968daf7220db1b5c23b2d25f11efea Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 17 Oct 2019 10:23:09 -0700 Subject: [PATCH 1/2] Simplify line drawing --- src/elements/element.line.js | 41 ++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/elements/element.line.js b/src/elements/element.line.js index 5fa728bfe2c..00e190e66dc 100644 --- a/src/elements/element.line.js +++ b/src/elements/element.line.js @@ -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 @@ -75,29 +79,26 @@ module.exports = Element.extend({ 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; } } From dcb38e4c2c2b0d0cc449de8db6cb46d2054e302e Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Thu, 17 Oct 2019 15:33:14 -0700 Subject: [PATCH 2/2] Remove duplicate initialization --- src/elements/element.line.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/elements/element.line.js b/src/elements/element.line.js index 00e190e66dc..782772bfbdc 100644 --- a/src/elements/element.line.js +++ b/src/elements/element.line.js @@ -77,7 +77,6 @@ module.exports = Element.extend({ // Stroke Line ctx.beginPath(); - lastDrawnIndex = -1; // First point moves to it's starting position no matter what currentVM = points[0]._view;