timeline.js 8.6 KB

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