Skip to content

Commit 74f5a8f

Browse files
committed
Revert line method spread
1 parent 2a1db9d commit 74f5a8f

File tree

2 files changed

+41
-75
lines changed

2 files changed

+41
-75
lines changed

src/elements/element.line.js

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import defaults from '../core/core.defaults';
44
import Element from '../core/core.element';
5+
import helpers from '../helpers';
56

67
const defaultColor = defaults.global.defaultColor;
78

@@ -48,78 +49,14 @@ function startAtGap(line, options) {
4849
}
4950
}
5051

51-
const methods = {
52-
line: (ctx, previous, target) => {
53-
ctx.lineTo(target.x, target.y);
54-
},
55-
stepMiddle: (ctx, previous, target) => {
56-
const midpoint = (previous.x + target.x) / 2.0;
57-
ctx.lineTo(midpoint, previous.y);
58-
ctx.lineTo(midpoint, target.y);
59-
ctx.lineTo(target.x, target.y);
60-
},
61-
stepMiddleR: (ctx, previous, target) => {
62-
const midpoint = (previous.x + target.x) / 2.0;
63-
ctx.lineTo(midpoint, target.y);
64-
ctx.lineTo(midpoint, previous.y);
65-
ctx.lineTo(target.x, target.y);
66-
},
67-
stepAfter: (ctx, previous, target) => {
68-
ctx.lineTo(previous.x, target.y);
69-
ctx.lineTo(target.x, target.y);
70-
},
71-
stepAfterR: (ctx, previous, target) => {
72-
ctx.lineTo(target.x, previous.y);
73-
ctx.lineTo(target.x, target.y);
74-
},
75-
stepBefore: (ctx, previous, target) => {
76-
ctx.lineTo(target.x, previous.y);
77-
ctx.lineTo(target.x, target.y);
78-
},
79-
stepBeforeR: (ctx, previous, target) => {
80-
ctx.lineTo(previous.x, target.y);
81-
ctx.lineTo(target.x, target.y);
82-
},
83-
bezier: (ctx, previous, target) => {
84-
ctx.bezierCurveTo(
85-
previous.controlPointNextX,
86-
previous.controlPointNextY,
87-
target.controlPointPreviousX,
88-
target.controlPointPreviousY,
89-
target.x,
90-
target.y);
91-
},
92-
bezierR: (ctx, previous, target) => {
93-
ctx.bezierCurveTo(
94-
previous.controlPointPreviousX,
95-
previous.controlPointPreviousY,
96-
target.controlPointNextX,
97-
target.controlPointNextY,
98-
target.x,
99-
target.y);
100-
}
101-
};
102-
103-
function getLineMethod(line, reverse) {
104-
let name = 'bezier';
105-
if (line.steppedLine) {
106-
const mode = line.steppedLine;
107-
if (mode === 'middle') {
108-
name = 'stepMiddle';
109-
} else if (mode === 'after') {
110-
name = 'stepAfter';
111-
} else {
112-
name = 'stepBefore';
113-
}
114-
} else if (!line.tension) {
115-
name = 'line';
116-
reverse = false;
117-
}
52+
function lineTo(ctx, previous, target) {
53+
ctx.lineTo(target.x, target.y);
54+
}
11855

119-
if (reverse) {
120-
name += 'R';
121-
}
122-
return methods[name];
56+
function getLineMethod(line) {
57+
return line.steppedLine ? helpers.canvas._steppedLineTo
58+
: !line.tension ? lineTo
59+
: helpers.canvas._bezierCurveTo;
12360
}
12461

12562
function pointInLine(p1, p2, t) {
@@ -247,7 +184,7 @@ function loopPath(ctx, line, options) {
247184
const points = line._children;
248185
const vm = line._view;
249186
const spanGaps = vm.spanGaps;
250-
const lineMethod = getLineMethod(vm, reverse);
187+
const lineMethod = getLineMethod(vm);
251188
const count = points.length;
252189
const dir = reverse ? -1 : 1;
253190
let closed = !bound;
@@ -274,7 +211,7 @@ function loopPath(ctx, line, options) {
274211
ctx.moveTo(point.x, point.y);
275212
move = false;
276213
} else {
277-
lineMethod(ctx, prev || point, point, vm.steppedLine);
214+
lineMethod(ctx, prev || point, point, reverse, vm.steppedLine);
278215
}
279216
prev = point;
280217
}
@@ -288,7 +225,7 @@ function normalPath(ctx, line, options) {
288225
const points = line._children;
289226
const vm = line._view;
290227
const spanGaps = vm.spanGaps;
291-
const lineMethod = getLineMethod(vm, reverse);
228+
const lineMethod = getLineMethod(vm);
292229
const startIndex = bound ? lineStart(points, bounds, reverse) : 0;
293230
const count = points.length;
294231
const num = count - startIndex;
@@ -315,7 +252,7 @@ function normalPath(ctx, line, options) {
315252
ctx.moveTo(point.x, point.y);
316253
move = false;
317254
} else {
318-
lineMethod(ctx, prev || point, point, vm.steppedLine);
255+
lineMethod(ctx, prev || point, point, reverse, vm.steppedLine);
319256
}
320257
prev = point;
321258
}

src/helpers/helpers.canvas.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,33 @@ module.exports = {
170170
unclipArea: function(ctx) {
171171
ctx.restore();
172172
},
173+
174+
/**
175+
* @private
176+
*/
177+
_steppedLineTo: function(ctx, previous, target, flip, mode) {
178+
if (mode === 'middle') {
179+
const midpoint = (previous.x + target.x) / 2.0;
180+
ctx.lineTo(midpoint, flip ? target.y : previous.y);
181+
ctx.lineTo(midpoint, flip ? previous.y : target.y);
182+
} else if ((mode === 'after' && !flip) || (mode !== 'after' && flip)) {
183+
ctx.lineTo(previous.x, target.y);
184+
} else {
185+
ctx.lineTo(target.x, previous.y);
186+
}
187+
ctx.lineTo(target.x, target.y);
188+
},
189+
190+
/**
191+
* @private
192+
*/
193+
_bezierCurveTo: function(ctx, previous, target, flip) {
194+
ctx.bezierCurveTo(
195+
flip ? previous.controlPointPreviousX : previous.controlPointNextX,
196+
flip ? previous.controlPointPreviousY : previous.controlPointNextY,
197+
flip ? target.controlPointNextX : target.controlPointPreviousX,
198+
flip ? target.controlPointNextY : target.controlPointPreviousY,
199+
target.x,
200+
target.y);
201+
}
173202
};

0 commit comments

Comments
 (0)