Skip to content

Commit 959ea08

Browse files
benmccannetimberg
authored andcommitted
[perf] simplify line drawing (#6575)
* Simplify line drawing * Remove duplicate initialization
1 parent daafbc7 commit 959ea08

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/elements/element.line.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ module.exports = Element.extend({
4040
var closePath = me._loop;
4141
var index, previous, currentVM;
4242

43-
if (me._loop && points.length) {
43+
if (!points.length) {
44+
return;
45+
}
46+
47+
if (me._loop) {
4448
for (index = 0; index < points.length; ++index) {
4549
previous = helpers.previousItem(points, index);
4650
// If the line has an open path, shift the point array
@@ -73,31 +77,27 @@ module.exports = Element.extend({
7377

7478
// Stroke Line
7579
ctx.beginPath();
76-
lastDrawnIndex = -1;
7780

78-
for (index = 0; index < points.length; ++index) {
79-
previous = helpers.previousItem(points, index);
81+
// First point moves to it's starting position no matter what
82+
currentVM = points[0]._view;
83+
if (!currentVM.skip) {
84+
ctx.moveTo(currentVM.x, currentVM.y);
85+
lastDrawnIndex = 0;
86+
}
87+
88+
for (index = 1; index < points.length; ++index) {
8089
currentVM = points[index]._view;
90+
previous = lastDrawnIndex === -1 ? helpers.previousItem(points, index) : points[lastDrawnIndex];
8191

82-
// First point moves to it's starting position no matter what
83-
if (index === 0) {
84-
if (!currentVM.skip) {
92+
if (!currentVM.skip) {
93+
if ((lastDrawnIndex !== (index - 1) && !spanGaps) || lastDrawnIndex === -1) {
94+
// There was a gap and this is the first point after the gap
8595
ctx.moveTo(currentVM.x, currentVM.y);
86-
lastDrawnIndex = index;
87-
}
88-
} else {
89-
previous = lastDrawnIndex === -1 ? previous : points[lastDrawnIndex];
90-
91-
if (!currentVM.skip) {
92-
if ((lastDrawnIndex !== (index - 1) && !spanGaps) || lastDrawnIndex === -1) {
93-
// There was a gap and this is the first point after the gap
94-
ctx.moveTo(currentVM.x, currentVM.y);
95-
} else {
96-
// Line to next point
97-
helpers.canvas.lineTo(ctx, previous._view, currentVM);
98-
}
99-
lastDrawnIndex = index;
96+
} else {
97+
// Line to next point
98+
helpers.canvas.lineTo(ctx, previous._view, currentVM);
10099
}
100+
lastDrawnIndex = index;
101101
}
102102
}
103103

0 commit comments

Comments
 (0)