KeyframeTrack.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { KeyframeTrackPrototype } from './KeyframeTrackPrototype.js';
  2. import { StringKeyframeTrack } from './tracks/StringKeyframeTrack.js';
  3. import { BooleanKeyframeTrack } from './tracks/BooleanKeyframeTrack.js';
  4. import { QuaternionKeyframeTrack } from './tracks/QuaternionKeyframeTrack.js';
  5. import { ColorKeyframeTrack } from './tracks/ColorKeyframeTrack.js';
  6. import { VectorKeyframeTrack } from './tracks/VectorKeyframeTrack.js';
  7. import { NumberKeyframeTrack } from './tracks/NumberKeyframeTrack.js';
  8. import { AnimationUtils } from './AnimationUtils.js';
  9. import { KeyframeTrackConstructor } from './KeyframeTrackConstructor.js';
  10. /**
  11. *
  12. * A timed sequence of keyframes for a specific property.
  13. *
  14. *
  15. * @author Ben Houston / http://clara.io/
  16. * @author David Sarno / http://lighthaus.us/
  17. * @author tschw
  18. */
  19. function KeyframeTrack( name, times, values, interpolation ) {
  20. KeyframeTrackConstructor.apply( this, arguments );
  21. }
  22. KeyframeTrack.prototype = KeyframeTrackPrototype;
  23. KeyframeTrackPrototype.constructor = KeyframeTrack;
  24. // Static methods:
  25. Object.assign( KeyframeTrack, {
  26. // Serialization (in static context, because of constructor invocation
  27. // and automatic invocation of .toJSON):
  28. parse: function( json ) {
  29. if( json.type === undefined ) {
  30. throw new Error( "track type undefined, can not parse" );
  31. }
  32. var trackType = KeyframeTrack._getTrackTypeForValueTypeName( json.type );
  33. if ( json.times === undefined ) {
  34. var times = [], values = [];
  35. AnimationUtils.flattenJSON( json.keys, times, values, 'value' );
  36. json.times = times;
  37. json.values = values;
  38. }
  39. // derived classes can define a static parse method
  40. if ( trackType.parse !== undefined ) {
  41. return trackType.parse( json );
  42. } else {
  43. // by default, we assume a constructor compatible with the base
  44. return new trackType(
  45. json.name, json.times, json.values, json.interpolation );
  46. }
  47. },
  48. toJSON: function( track ) {
  49. var trackType = track.constructor;
  50. var json;
  51. // derived classes can define a static toJSON method
  52. if ( trackType.toJSON !== undefined ) {
  53. json = trackType.toJSON( track );
  54. } else {
  55. // by default, we assume the data can be serialized as-is
  56. json = {
  57. 'name': track.name,
  58. 'times': AnimationUtils.convertArray( track.times, Array ),
  59. 'values': AnimationUtils.convertArray( track.values, Array )
  60. };
  61. var interpolation = track.getInterpolation();
  62. if ( interpolation !== track.DefaultInterpolation ) {
  63. json.interpolation = interpolation;
  64. }
  65. }
  66. json.type = track.ValueTypeName; // mandatory
  67. return json;
  68. },
  69. _getTrackTypeForValueTypeName: function( typeName ) {
  70. switch( typeName.toLowerCase() ) {
  71. case "scalar":
  72. case "double":
  73. case "float":
  74. case "number":
  75. case "integer":
  76. return NumberKeyframeTrack;
  77. case "vector":
  78. case "vector2":
  79. case "vector3":
  80. case "vector4":
  81. return VectorKeyframeTrack;
  82. case "color":
  83. return ColorKeyframeTrack;
  84. case "quaternion":
  85. return QuaternionKeyframeTrack;
  86. case "bool":
  87. case "boolean":
  88. return BooleanKeyframeTrack;
  89. case "string":
  90. return StringKeyframeTrack;
  91. }
  92. throw new Error( "Unsupported typeName: " + typeName );
  93. }
  94. } );
  95. export { KeyframeTrack };
粤ICP备19079148号