I want to modify an array of numbers and output it to a new range using d3.scale
but with custom interpolation function. The interpolation function should be one of the easing functions used in transitions, e.g. easeInOutQuad
:
easeInOutQuad = function (x, t, b, c, d) {
if ((t/=d/2) < 1) return c/2*t*t + b;
return -c/2 * ((--t)*(t-2) - 1) + b;
}
So my input array [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
would become, more less, something like [0, 2, 5, 10, 20, 40, 70, 90, 95, 98, 100]
where numbers increase slower at the beginning of the array, then gradually faster towards the middle and then again slower towards the end.
My code so far:
var inputArr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
linearArr = [],
easingArr = [],
easing = d3.interpolate, // ?
min = d3.min(inputArr),
max = d3.max(inputArr),
linearScale = d3.scale.linear()
.domain([min,max])
.range([0,100]),
easingScale = d3.scale.linear()
.domain([min,max])
.interpolate(easing) // ?
.range([0,100]);
for (var i = 0; i < inputArr.length; i++) {
linearArr[i] = linearScale(inputArr[i]);
easingArr[i] = easingScale(inputArr[i]);
}
console.log(linearArr); // 0,10,20,30,40,50,60,70,80,90,100
console.log(easingArr); // 0,10,20,30,40,50,60,70,80,90,100
Thanks for any suggestions/examples of how such an easing function could be used with d3.interpolate
.