expressionHelpers.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. createTypedArray,
  3. } from '../helpers/arrays';
  4. import ExpressionManager from './ExpressionManager';
  5. const expressionHelpers = (function () {
  6. function searchExpressions(elem, data, prop) {
  7. if (data.x) {
  8. prop.k = true;
  9. prop.x = true;
  10. prop.initiateExpression = ExpressionManager.initiateExpression;
  11. prop.effectsSequence.push(prop.initiateExpression(elem, data, prop).bind(prop));
  12. }
  13. }
  14. function getValueAtTime(frameNum) {
  15. frameNum *= this.elem.globalData.frameRate;
  16. frameNum -= this.offsetTime;
  17. if (frameNum !== this._cachingAtTime.lastFrame) {
  18. this._cachingAtTime.lastIndex = this._cachingAtTime.lastFrame < frameNum ? this._cachingAtTime.lastIndex : 0;
  19. this._cachingAtTime.value = this.interpolateValue(frameNum, this._cachingAtTime);
  20. this._cachingAtTime.lastFrame = frameNum;
  21. }
  22. return this._cachingAtTime.value;
  23. }
  24. function getSpeedAtTime(frameNum) {
  25. var delta = -0.01;
  26. var v1 = this.getValueAtTime(frameNum);
  27. var v2 = this.getValueAtTime(frameNum + delta);
  28. var speed = 0;
  29. if (v1.length) {
  30. var i;
  31. for (i = 0; i < v1.length; i += 1) {
  32. speed += Math.pow(v2[i] - v1[i], 2);
  33. }
  34. speed = Math.sqrt(speed) * 100;
  35. } else {
  36. speed = 0;
  37. }
  38. return speed;
  39. }
  40. function getVelocityAtTime(frameNum) {
  41. if (this.vel !== undefined) {
  42. return this.vel;
  43. }
  44. var delta = -0.001;
  45. // frameNum += this.elem.data.st;
  46. var v1 = this.getValueAtTime(frameNum);
  47. var v2 = this.getValueAtTime(frameNum + delta);
  48. var velocity;
  49. if (v1.length) {
  50. velocity = createTypedArray('float32', v1.length);
  51. var i;
  52. for (i = 0; i < v1.length; i += 1) {
  53. // removing frameRate
  54. // if needed, don't add it here
  55. // velocity[i] = this.elem.globalData.frameRate*((v2[i] - v1[i])/delta);
  56. velocity[i] = (v2[i] - v1[i]) / delta;
  57. }
  58. } else {
  59. velocity = (v2 - v1) / delta;
  60. }
  61. return velocity;
  62. }
  63. function getStaticValueAtTime() {
  64. return this.pv;
  65. }
  66. function setGroupProperty(propertyGroup) {
  67. this.propertyGroup = propertyGroup;
  68. }
  69. return {
  70. searchExpressions: searchExpressions,
  71. getSpeedAtTime: getSpeedAtTime,
  72. getVelocityAtTime: getVelocityAtTime,
  73. getValueAtTime: getValueAtTime,
  74. getStaticValueAtTime: getStaticValueAtTime,
  75. setGroupProperty: setGroupProperty,
  76. };
  77. }());
  78. export default expressionHelpers;