Skip to content

Commit 56f33aa

Browse files
committed
Move interpolation methods to helpers.math
1 parent 8946ef4 commit 56f33aa

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/elements/element.line.js

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,41 +59,14 @@ function getLineMethod(line) {
5959
: helpers.canvas._bezierCurveTo;
6060
}
6161

62-
function pointInLine(p1, p2, t) {
63-
return {
64-
x: p1.x + t * (p2.x - p1.x),
65-
y: p1.y + t * (p2.y - p1.y)
66-
};
67-
}
68-
69-
function steppeInterpolation(p1, p2, t, mode) {
70-
return {
71-
x: p1.x + t * (p2.x - p1.x),
72-
y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y
73-
: mode === 'after' ? t < 1 ? p1.y : p2.y
74-
: t > 0 ? p2.y : p1.y
75-
};
76-
}
77-
78-
function bezierInterpolation(p1, p2, t) {
79-
const cp1 = {x: p1.controlPointNextX, y: p1.controlPointNextY};
80-
const cp2 = {x: p2.controlPointPreviousX, y: p2.controlPointPreviousY};
81-
const a = pointInLine(p1, cp1, t);
82-
const b = pointInLine(cp1, cp2, t);
83-
const c = pointInLine(cp2, p2, t);
84-
const d = pointInLine(a, b, t);
85-
const e = pointInLine(b, c, t);
86-
return pointInLine(d, e, t);
87-
}
88-
8962
function getInterpolationMethod(line) {
9063
if (line.steppedLine) {
91-
return steppeInterpolation;
64+
return helpers.math.steppeInterpolation;
9265
}
9366
if (!line.tension) {
94-
return pointInLine;
67+
return helpers.math.pointInLine;
9568
}
96-
return bezierInterpolation;
69+
return helpers.math.bezierInterpolation;
9770
}
9871

9972
function lineEnd(points, bounds) {

src/helpers/helpers.math.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
'use strict';
22

3+
function pointInLine(p1, p2, t) {
4+
return {
5+
x: p1.x + t * (p2.x - p1.x),
6+
y: p1.y + t * (p2.y - p1.y)
7+
};
8+
}
9+
310
/**
411
* @alias Chart.helpers.math
512
* @namespace
@@ -38,6 +45,28 @@ var exports = {
3845
var isPowerOf10 = x === Math.pow(10, powerOf10);
3946

4047
return isPowerOf10 ? powerOf10 : exponent;
48+
},
49+
50+
pointInLine: pointInLine,
51+
52+
steppeInterpolation: function(p1, p2, t, mode) {
53+
return {
54+
x: p1.x + t * (p2.x - p1.x),
55+
y: mode === 'middle' ? t < 0.5 ? p1.y : p2.y
56+
: mode === 'after' ? t < 1 ? p1.y : p2.y
57+
: t > 0 ? p2.y : p1.y
58+
};
59+
},
60+
61+
bezierInterpolation: function(p1, p2, t) {
62+
const cp1 = {x: p1.controlPointNextX, y: p1.controlPointNextY};
63+
const cp2 = {x: p2.controlPointPreviousX, y: p2.controlPointPreviousY};
64+
const a = pointInLine(p1, cp1, t);
65+
const b = pointInLine(cp1, cp2, t);
66+
const c = pointInLine(cp2, p2, t);
67+
const d = pointInLine(a, b, t);
68+
const e = pointInLine(b, c, t);
69+
return pointInLine(d, e, t);
4170
}
4271
};
4372

0 commit comments

Comments
 (0)