timeline.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var util_1 = require("@antv/util");
  4. var d3Timer = require("d3-timer");
  5. var d3_interpolate_1 = require("d3-interpolate"); // 目前整体动画只需要数值和数组的差值计算
  6. var register_1 = require("./register");
  7. var PathUtil = require("../util/path");
  8. var color_1 = require("../util/color");
  9. var IDENTITY_MATRIX = [1, 0, 0, 0, 1, 0, 0, 0, 1];
  10. /**
  11. * 使用 ratio 进行插值计算来更新属性
  12. * @param {IElement} shape 元素
  13. * @param {Animation} animation 动画
  14. * @param {number} ratio 比例
  15. * @return {boolean} 动画是否执行完成
  16. */
  17. function _update(shape, animation, ratio) {
  18. var cProps = {}; // 此刻属性
  19. var fromAttrs = animation.fromAttrs, toAttrs = animation.toAttrs;
  20. if (shape.destroyed) {
  21. return;
  22. }
  23. var interf; // 差值函数
  24. for (var k in toAttrs) {
  25. if (!util_1.isEqual(fromAttrs[k], toAttrs[k])) {
  26. if (k === 'path') {
  27. var toPath = toAttrs[k];
  28. var fromPath = fromAttrs[k];
  29. if (toPath.length > fromPath.length) {
  30. toPath = PathUtil.parsePathString(toAttrs[k]); // 终点状态
  31. fromPath = PathUtil.parsePathString(fromAttrs[k]); // 起始状态
  32. fromPath = PathUtil.fillPathByDiff(fromPath, toPath);
  33. fromPath = PathUtil.formatPath(fromPath, toPath);
  34. animation.fromAttrs.path = fromPath;
  35. animation.toAttrs.path = toPath;
  36. }
  37. else if (!animation.pathFormatted) {
  38. toPath = PathUtil.parsePathString(toAttrs[k]);
  39. fromPath = PathUtil.parsePathString(fromAttrs[k]);
  40. fromPath = PathUtil.formatPath(fromPath, toPath);
  41. animation.fromAttrs.path = fromPath;
  42. animation.toAttrs.path = toPath;
  43. animation.pathFormatted = true;
  44. }
  45. cProps[k] = [];
  46. for (var i = 0; i < toPath.length; i++) {
  47. var toPathPoint = toPath[i];
  48. var fromPathPoint = fromPath[i];
  49. var cPathPoint = [];
  50. for (var j = 0; j < toPathPoint.length; j++) {
  51. if (util_1.isNumber(toPathPoint[j]) && fromPathPoint && util_1.isNumber(fromPathPoint[j])) {
  52. interf = d3_interpolate_1.interpolate(fromPathPoint[j], toPathPoint[j]);
  53. cPathPoint.push(interf(ratio));
  54. }
  55. else {
  56. cPathPoint.push(toPathPoint[j]);
  57. }
  58. }
  59. cProps[k].push(cPathPoint);
  60. }
  61. }
  62. else if (k === 'matrix') {
  63. /*
  64. 对矩阵进行插值时,需要保证矩阵不为空,为空则使用单位矩阵
  65. TODO: 二维和三维场景下单位矩阵不同,之后 WebGL 版需要做进一步处理
  66. */
  67. var matrixFn = d3_interpolate_1.interpolateArray(fromAttrs[k] || IDENTITY_MATRIX, toAttrs[k] || IDENTITY_MATRIX);
  68. var currentMatrix = matrixFn(ratio);
  69. cProps[k] = currentMatrix;
  70. }
  71. else if (color_1.isColorProp(k) && color_1.isGradientColor(toAttrs[k])) {
  72. cProps[k] = toAttrs[k];
  73. }
  74. else if (!util_1.isFunction(toAttrs[k])) {
  75. // 非函数类型的值才能做插值
  76. interf = d3_interpolate_1.interpolate(fromAttrs[k], toAttrs[k]);
  77. cProps[k] = interf(ratio);
  78. }
  79. }
  80. }
  81. shape.attr(cProps);
  82. }
  83. /**
  84. * 根据自定义帧动画函数 onFrame 来更新属性
  85. * @param {IElement} shape 元素
  86. * @param {Animation} animation 动画
  87. * @param {number} elapsed 动画执行时间(毫秒)
  88. * @return {boolean} 动画是否执行完成
  89. */
  90. function update(shape, animation, elapsed) {
  91. var startTime = animation.startTime, delay = animation.delay;
  92. // 如果还没有开始执行或暂停,先不更新
  93. if (elapsed < startTime + delay || animation._paused) {
  94. return false;
  95. }
  96. var ratio;
  97. var duration = animation.duration;
  98. var easing = animation.easing;
  99. var easeFn = register_1.getEasing(easing);
  100. // 已执行时间
  101. elapsed = elapsed - startTime - animation.delay;
  102. if (animation.repeat) {
  103. // 如果动画重复执行,则 elapsed > duration,计算 ratio 时需取模
  104. ratio = (elapsed % duration) / duration;
  105. ratio = easeFn(ratio);
  106. }
  107. else {
  108. ratio = elapsed / duration;
  109. if (ratio < 1) {
  110. // 动画未执行完
  111. ratio = easeFn(ratio);
  112. }
  113. else {
  114. // 动画已执行完
  115. if (animation.onFrame) {
  116. shape.attr(animation.onFrame(1));
  117. }
  118. else {
  119. shape.attr(animation.toAttrs);
  120. }
  121. return true;
  122. }
  123. }
  124. if (animation.onFrame) {
  125. var attrs = animation.onFrame(ratio);
  126. shape.attr(attrs);
  127. }
  128. else {
  129. _update(shape, animation, ratio);
  130. }
  131. return false;
  132. }
  133. var Timeline = /** @class */ (function () {
  134. /**
  135. * 时间轴构造函数,依赖于画布
  136. * @param {}
  137. */
  138. function Timeline(canvas) {
  139. /**
  140. * 执行动画的元素列表
  141. * @type {IElement[]}
  142. */
  143. this.animators = [];
  144. /**
  145. * 当前时间
  146. * @type {number}
  147. */
  148. this.current = 0;
  149. /**
  150. * 定时器
  151. * @type {d3Timer.Timer}
  152. */
  153. this.timer = null;
  154. this.canvas = canvas;
  155. }
  156. /**
  157. * 初始化定时器
  158. */
  159. Timeline.prototype.initTimer = function () {
  160. var _this = this;
  161. var isFinished = false;
  162. var shape;
  163. var animations;
  164. var animation;
  165. this.timer = d3Timer.timer(function (elapsed) {
  166. _this.current = elapsed;
  167. if (_this.animators.length > 0) {
  168. for (var i = _this.animators.length - 1; i >= 0; i--) {
  169. shape = _this.animators[i];
  170. if (shape.destroyed) {
  171. // 如果已经被销毁,直接移出队列
  172. _this.removeAnimator(i);
  173. continue;
  174. }
  175. if (!shape.isAnimatePaused()) {
  176. animations = shape.get('animations');
  177. for (var j = animations.length - 1; j >= 0; j--) {
  178. animation = animations[j];
  179. isFinished = update(shape, animation, elapsed);
  180. if (isFinished) {
  181. animations.splice(j, 1);
  182. isFinished = false;
  183. if (animation.callback) {
  184. animation.callback();
  185. }
  186. }
  187. }
  188. }
  189. if (animations.length === 0) {
  190. _this.removeAnimator(i);
  191. }
  192. }
  193. var autoDraw = _this.canvas.get('autoDraw');
  194. // 非自动渲染模式下,手动调用 canvas.draw() 重新渲染
  195. if (!autoDraw) {
  196. _this.canvas.draw();
  197. }
  198. }
  199. });
  200. };
  201. /**
  202. * 增加动画元素
  203. */
  204. Timeline.prototype.addAnimator = function (shape) {
  205. this.animators.push(shape);
  206. };
  207. /**
  208. * 移除动画元素
  209. */
  210. Timeline.prototype.removeAnimator = function (index) {
  211. this.animators.splice(index, 1);
  212. };
  213. /**
  214. * 是否有动画在执行
  215. */
  216. Timeline.prototype.isAnimating = function () {
  217. return !!this.animators.length;
  218. };
  219. /**
  220. * 停止定时器
  221. */
  222. Timeline.prototype.stop = function () {
  223. if (this.timer) {
  224. this.timer.stop();
  225. }
  226. };
  227. /**
  228. * 停止时间轴上所有元素的动画,并置空动画元素列表
  229. * @param {boolean} toEnd 是否到动画的最终状态,用来透传给动画元素的 stopAnimate 方法
  230. */
  231. Timeline.prototype.stopAllAnimations = function (toEnd) {
  232. if (toEnd === void 0) { toEnd = true; }
  233. this.animators.forEach(function (animator) {
  234. animator.stopAnimate(toEnd);
  235. });
  236. this.animators = [];
  237. this.canvas.draw();
  238. };
  239. /**
  240. * 获取当前时间
  241. */
  242. Timeline.prototype.getTime = function () {
  243. return this.current;
  244. };
  245. return Timeline;
  246. }());
  247. exports.default = Timeline;
  248. //# sourceMappingURL=timeline.js.map