gecko-dev/layout/style/test/animation_utils.js

63 lines
1.7 KiB
JavaScript

function px_to_num(str)
{
return Number(String(str).match(/^([\d.]+)px$/)[1]);
}
function bezier(x1, y1, x2, y2) {
// Cubic bezier with control points (0, 0), (x1, y1), (x2, y2), and (1, 1).
function x_for_t(t) {
var omt = 1-t;
return 3 * omt * omt * t * x1 + 3 * omt * t * t * x2 + t * t * t;
}
function y_for_t(t) {
var omt = 1-t;
return 3 * omt * omt * t * y1 + 3 * omt * t * t * y2 + t * t * t;
}
function t_for_x(x) {
// Binary subdivision.
var mint = 0, maxt = 1;
for (var i = 0; i < 30; ++i) {
var guesst = (mint + maxt) / 2;
var guessx = x_for_t(guesst);
if (x < guessx)
maxt = guesst;
else
mint = guesst;
}
return (mint + maxt) / 2;
}
return function bezier_closure(x) {
if (x == 0) return 0;
if (x == 1) return 1;
return y_for_t(t_for_x(x));
}
}
function step_end(nsteps) {
return function step_end_closure(x) {
return Math.floor(x * nsteps) / nsteps;
}
}
function step_start(nsteps) {
var stepend = step_end(nsteps);
return function step_start_closure(x) {
return 1.0 - stepend(1.0 - x);
}
}
var gTF = {
"ease": bezier(0.25, 0.1, 0.25, 1),
"linear": function(x) { return x; },
"ease_in": bezier(0.42, 0, 1, 1),
"ease_out": bezier(0, 0, 0.58, 1),
"ease_in_out": bezier(0.42, 0, 0.58, 1),
"step_start": step_start(1),
"step_end": step_end(1),
};
function is_approx(float1, float2, error, desc) {
ok(Math.abs(float1 - float2) < error,
desc + ": " + float1 + " and " + float2 + " should be within " + error);
}