2015-03-25 02:34:12 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2017-03-08 08:51:23 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-25 02:34:12 +00:00
|
|
|
*
|
2018-05-11 02:06:46 +00:00
|
|
|
* @format
|
2015-03-25 02:34:12 +00:00
|
|
|
*/
|
|
|
|
|
2017-10-03 00:03:03 +00:00
|
|
|
'use strict';
|
2015-03-25 02:34:12 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const keyOf = require('fbjs/lib/keyOf');
|
2015-03-25 02:34:12 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const X_DIM = keyOf({x: null});
|
|
|
|
const Y_DIM = keyOf({y: null});
|
|
|
|
const Z_DIM = keyOf({z: null});
|
2015-03-25 02:34:12 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const InitialOperationField = {
|
2015-03-25 02:34:12 +00:00
|
|
|
transformTranslate: [0, 0, 0],
|
|
|
|
transformScale: [1, 1, 1],
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const InterpolateMatrix = {
|
2017-10-03 00:03:03 +00:00
|
|
|
transformScale: function(mat, x, y, z) {
|
|
|
|
mat[0] = mat[0] * x;
|
|
|
|
mat[1] = mat[1] * x;
|
|
|
|
mat[2] = mat[2] * x;
|
|
|
|
mat[3] = mat[3] * x;
|
|
|
|
mat[4] = mat[4] * y;
|
|
|
|
mat[5] = mat[5] * y;
|
|
|
|
mat[6] = mat[6] * y;
|
|
|
|
mat[7] = mat[7] * y;
|
|
|
|
mat[8] = mat[8] * z;
|
|
|
|
mat[9] = mat[9] * z;
|
|
|
|
mat[10] = mat[10] * z;
|
|
|
|
mat[11] = mat[11] * z;
|
|
|
|
},
|
|
|
|
transformTranslate: function(mat, x, y, z) {
|
|
|
|
mat[12] = mat[0] * x + mat[4] * y + mat[8] * z + mat[12];
|
|
|
|
mat[13] = mat[1] * x + mat[5] * y + mat[9] * z + mat[13];
|
|
|
|
mat[14] = mat[2] * x + mat[6] * y + mat[10] * z + mat[14];
|
|
|
|
mat[15] = mat[3] * x + mat[7] * y + mat[11] * z + mat[15];
|
2018-05-11 02:06:46 +00:00
|
|
|
},
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const computeNextValLinear = function(anim, from, to, value) {
|
|
|
|
const hasRoundRatio = 'round' in anim;
|
|
|
|
const roundRatio = anim.round;
|
|
|
|
let ratio = (value - anim.min) / (anim.max - anim.min);
|
2015-03-25 02:34:12 +00:00
|
|
|
if (!anim.extrapolate) {
|
2018-05-11 02:06:46 +00:00
|
|
|
ratio = ratio > 1 ? 1 : ratio < 0 ? 0 : ratio;
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
let nextVal = from * (1 - ratio) + to * ratio;
|
2017-10-03 00:03:03 +00:00
|
|
|
if (hasRoundRatio) {
|
|
|
|
nextVal = Math.round(roundRatio * nextVal) / roundRatio;
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
2018-09-11 18:29:35 +00:00
|
|
|
if (!isFinite(nextVal)) {
|
|
|
|
nextVal = null;
|
|
|
|
}
|
2017-10-03 00:03:03 +00:00
|
|
|
return nextVal;
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const computeNextValLinearScalar = function(anim, value) {
|
2017-10-03 00:03:03 +00:00
|
|
|
return computeNextValLinear(anim, anim.from, anim.to, value);
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const setNextValAndDetectChange = function(result, name, nextVal, didChange) {
|
2017-10-03 00:03:03 +00:00
|
|
|
if (!didChange) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const prevVal = result[name];
|
2017-10-03 00:03:03 +00:00
|
|
|
result[name] = nextVal;
|
2018-05-11 02:06:46 +00:00
|
|
|
didChange = didChange || nextVal !== prevVal;
|
2015-03-25 02:34:12 +00:00
|
|
|
} else {
|
2017-10-03 00:03:03 +00:00
|
|
|
result[name] = nextVal;
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
2017-10-03 00:03:03 +00:00
|
|
|
return didChange;
|
|
|
|
};
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const initIdentity = function(mat) {
|
2017-10-03 00:03:03 +00:00
|
|
|
mat[0] = 1;
|
|
|
|
mat[1] = 0;
|
|
|
|
mat[2] = 0;
|
|
|
|
mat[3] = 0;
|
|
|
|
mat[4] = 0;
|
|
|
|
mat[5] = 1;
|
|
|
|
mat[6] = 0;
|
|
|
|
mat[7] = 0;
|
|
|
|
mat[8] = 0;
|
|
|
|
mat[9] = 0;
|
|
|
|
mat[10] = 1;
|
|
|
|
mat[11] = 0;
|
|
|
|
mat[12] = 0;
|
|
|
|
mat[13] = 0;
|
|
|
|
mat[14] = 0;
|
|
|
|
mat[15] = 1;
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const computeNextMatrixOperationField = function(
|
|
|
|
anim,
|
|
|
|
name,
|
|
|
|
dim,
|
|
|
|
index,
|
|
|
|
value,
|
|
|
|
) {
|
2017-10-03 00:03:03 +00:00
|
|
|
if (anim.from[dim] !== undefined && anim.to[dim] !== undefined) {
|
|
|
|
return computeNextValLinear(anim, anim.from[dim], anim.to[dim], value);
|
|
|
|
} else {
|
|
|
|
return InitialOperationField[name][index];
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-11 02:06:46 +00:00
|
|
|
const computeTransform = function(
|
|
|
|
anim,
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
result,
|
|
|
|
didChange,
|
|
|
|
didMatrix,
|
|
|
|
) {
|
|
|
|
const transform =
|
|
|
|
result.transform !== undefined
|
|
|
|
? result.transform
|
|
|
|
: (result.transform = [{matrix: []}]);
|
2018-05-10 22:44:52 +00:00
|
|
|
const mat = transform[0].matrix;
|
|
|
|
const m0 = mat[0];
|
|
|
|
const m1 = mat[1];
|
|
|
|
const m2 = mat[2];
|
|
|
|
const m3 = mat[3];
|
|
|
|
const m4 = mat[4];
|
|
|
|
const m5 = mat[5];
|
|
|
|
const m6 = mat[6];
|
|
|
|
const m7 = mat[7];
|
|
|
|
const m8 = mat[8];
|
|
|
|
const m9 = mat[9];
|
|
|
|
const m10 = mat[10];
|
|
|
|
const m11 = mat[11];
|
|
|
|
const m12 = mat[12];
|
|
|
|
const m13 = mat[13];
|
|
|
|
const m14 = mat[14];
|
|
|
|
const m15 = mat[15];
|
2017-10-03 00:03:03 +00:00
|
|
|
if (!didMatrix) {
|
2018-05-11 02:06:46 +00:00
|
|
|
initIdentity(mat); // This will be the first transform.
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const x = computeNextMatrixOperationField(anim, name, X_DIM, 0, value);
|
|
|
|
const y = computeNextMatrixOperationField(anim, name, Y_DIM, 1, value);
|
|
|
|
const z = computeNextMatrixOperationField(anim, name, Z_DIM, 2, value);
|
2017-10-03 00:03:03 +00:00
|
|
|
InterpolateMatrix[name](mat, x, y, z);
|
|
|
|
if (!didChange) {
|
2018-05-11 02:06:46 +00:00
|
|
|
didChange =
|
|
|
|
m0 !== mat[0] ||
|
|
|
|
m1 !== mat[1] ||
|
|
|
|
m2 !== mat[2] ||
|
|
|
|
m3 !== mat[3] ||
|
|
|
|
m4 !== mat[4] ||
|
|
|
|
m5 !== mat[5] ||
|
|
|
|
m6 !== mat[6] ||
|
|
|
|
m7 !== mat[7] ||
|
|
|
|
m8 !== mat[8] ||
|
|
|
|
m9 !== mat[9] ||
|
|
|
|
m10 !== mat[10] ||
|
|
|
|
m11 !== mat[11] ||
|
|
|
|
m12 !== mat[12] ||
|
|
|
|
m13 !== mat[13] ||
|
|
|
|
m14 !== mat[14] ||
|
|
|
|
m15 !== mat[15];
|
2015-03-25 02:34:12 +00:00
|
|
|
}
|
2017-10-03 00:03:03 +00:00
|
|
|
return didChange;
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} anims Animation configuration by style property name.
|
|
|
|
* @return {function} Function accepting style object, that mutates that style
|
|
|
|
* object and returns a boolean describing if any update was actually applied.
|
|
|
|
*/
|
2018-05-10 22:44:52 +00:00
|
|
|
const buildStyleInterpolator = function(anims) {
|
2017-10-03 00:03:03 +00:00
|
|
|
function styleInterpolator(result, value) {
|
2018-05-10 22:44:52 +00:00
|
|
|
let didChange = false;
|
|
|
|
let didMatrix = false;
|
|
|
|
for (const name in anims) {
|
|
|
|
const anim = anims[name];
|
2017-10-03 00:03:03 +00:00
|
|
|
if (anim.type === 'linear') {
|
|
|
|
if (name in InterpolateMatrix) {
|
2018-05-11 02:06:46 +00:00
|
|
|
didChange = computeTransform(
|
|
|
|
anim,
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
result,
|
|
|
|
didChange,
|
|
|
|
didMatrix,
|
|
|
|
);
|
2017-10-03 00:03:03 +00:00
|
|
|
didMatrix = true;
|
|
|
|
} else {
|
|
|
|
var next = computeNextValLinearScalar(anim, value);
|
|
|
|
didChange = setNextValAndDetectChange(result, name, next, didChange);
|
|
|
|
}
|
|
|
|
} else if (anim.type === 'constant') {
|
|
|
|
var next = anim.value;
|
|
|
|
didChange = setNextValAndDetectChange(result, name, next, didChange);
|
|
|
|
} else if (anim.type === 'step') {
|
|
|
|
var next = value >= anim.threshold ? anim.to : anim.from;
|
|
|
|
didChange = setNextValAndDetectChange(result, name, next, didChange);
|
|
|
|
} else if (anim.type === 'identity') {
|
|
|
|
var next = value;
|
|
|
|
didChange = setNextValAndDetectChange(result, name, next, didChange);
|
|
|
|
}
|
2015-11-23 22:17:54 +00:00
|
|
|
}
|
2017-10-03 00:03:03 +00:00
|
|
|
return didChange;
|
2015-11-23 22:17:54 +00:00
|
|
|
}
|
2017-10-03 00:03:03 +00:00
|
|
|
return styleInterpolator;
|
2015-03-25 02:34:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = buildStyleInterpolator;
|