# Extrapolate split cubic-bezier to 1,1

I need help with the solution provided here.

Create easy function 40% off set

I need to modify it so that the returned left and rights are extrapolated to `1,1` after splitting. This is because if I don't extrapolate, I can't use the returned split cubic-bezier as a css transition.

So this is the test I did. Please help because real does not match mike way :( I think the issue is I need to extrapolate the result to 1,1. I can't simply double the values though I'm pretty sure.

• REAL
• The function posted above returns the following
• `ease-in-out` is same as this is starting point
• first half, left, is given to be `cubic-bezier(0.21, 0, 0.355, 0.25)` and graphically is http://cubic-bezier.com/#.21,0,.35,.25
• code returned: `left:[0, 0, 0.21, 0, 0.355, 0.25, 0.5, 0.5]`
• second half, right, is given to be `cubic-bezier(0.645, 0.75, 0.79, 1)` and graphically is http://cubic-bezier.com/#.64,.75,.79,1
• code returned `right:[0.5, 0.5, 0.645, 0.75, 0.79, 1, 1, 1]`

Code used for getting it the Mike way is this:

``````var result = split({
z: .5,
x: [0, 0.42, 0.58, 1],
y: [0, 0, 1, 1]
});
`````` first half is ease-in which is cubic-bezier(.42,0,1,1) and graphically is http://cubic-bezier.com/#.42,0,1,1

Please verify this assumption. (curves original end points are 0,0, and 1,1 in a css timing function) The first half of the bezier curve [0,0, .42,0, .58,1, 1,1] should not be [0,0 .42,0, 1,1, 1,1] The end points are correct (after scaling to 1,1), but you have lost continuity there.

The values returned by Mike's algorithm is correct.

Try this visualisation for an explanation on why your assumption might be wrong.

The algorithm you are using to split is a well known algorithm called de Casteljau algorithm. This method can be geometrically expressed in a very simple manner. Check out the animated visualisations on how this splitting at any arbitrary point is possible https://en.wikipedia.org/wiki/B%C3%A9zier_curve.

However, You may soon hit an issue trying to correctly scale a split portion of a bezier curve to fit in a unit square, with endpoints fixed at 0,0 and 1,1. This probably you can try out quite easily on paper. The easiest way probably is to just linearly scale the control points of the bezier, you will get a squashed curve in most cases though. I created a modified version of Mike's split function so it fits it to a unit square :) It uses hkrish's pointers to do coordinate normalization.

Just set parameter `fitUnitCell` to true. :)

``````function splitCubicBezier(options) {
var z = options.z,
cz = z-1,
z2 = z*z,
cz2 = cz*cz,
z3 = z2*z,
cz3 = cz2*cz,
x = options.x,
y = options.y;

var left = [
x,
y,
z*x - cz*x,
z*y - cz*y,
z2*x - 2*z*cz*x + cz2*x,
z2*y - 2*z*cz*y + cz2*y,
z3*x - 3*z2*cz*x + 3*z*cz2*x - cz3*x,
z3*y - 3*z2*cz*y + 3*z*cz2*y - cz3*y];

var right = [
z3*x - 3*z2*cz*x + 3*z*cz2*x - cz3*x,
z3*y - 3*z2*cz*y + 3*z*cz2*y - cz3*y,
z2*x - 2*z*cz*x + cz2*x,
z2*y - 2*z*cz*y + cz2*y,
z*x - cz*x,
z*y - cz*y,
x,
y];

if (options.fitUnitSquare) {
return {
left: left.map(function(el, i) {
if (i % 2 == 0) {
//return el * (1 / left)
var Xmin = left;
var Xmax = left; //should be 1
var Sx = 1 / (Xmax - Xmin);
return (el - Xmin) * Sx;
} else {
//return el * (1 / left)
var Ymin = left;
var Ymax = left; //should be 1
var Sy = 1 / (Ymax - Ymin);
return (el - Ymin) * Sy;
}
}),
right: right.map(function(el, i) {
if (i % 2 == 0) {
//xval
var Xmin = right; //should be 0
var Xmax = right;
var Sx = 1 / (Xmax - Xmin);
return (el - Xmin) * Sx;
} else {
//yval
var Ymin = right; //should be 0
var Ymax = right;
var Sy = 1 / (Ymax - Ymin);
return (el - Ymin) * Sy;
}
})
}
} else {
return { left: left, right: right};
}
}

var easeInOut = {
xs: [0, .42, .58, 1],
ys: [0,   0,   1, 1]
};

var splitRes = splitCubicBezier({
z: .5,
x: easeInOut.xs,
y: easeInOut.ys,
fitUnitSquare: false
});