util.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. /**
  3. * Utility
  4. * @author sima.zhang1990@gmail.com
  5. */
  6. var _require = require('../graphic/index'),
  7. Matrix = _require.Matrix;
  8. var Util = require('../util/common');
  9. var Helpers = {
  10. getCoordInfo: function getCoordInfo(coord) {
  11. var start = coord.start;
  12. var end = coord.end;
  13. return {
  14. start: start,
  15. end: end,
  16. width: end.x - start.x,
  17. height: Math.abs(end.y - start.y)
  18. };
  19. },
  20. getScaledMatrix: function getScaledMatrix(shape, v, direct) {
  21. var scaledMatrix;
  22. shape.apply(v);
  23. var x = v[0];
  24. var y = v[1];
  25. if (direct === 'x') {
  26. shape.transform([['t', x, y], ['s', 0.01, 1], ['t', -x, -y]]);
  27. var matrix = shape.getMatrix();
  28. scaledMatrix = Matrix.transform(matrix, [['t', x, y], ['s', 100, 1], ['t', -x, -y]]);
  29. } else if (direct === 'y') {
  30. shape.transform([['t', x, y], ['s', 1, 0.01], ['t', -x, -y]]);
  31. var _matrix = shape.getMatrix();
  32. scaledMatrix = Matrix.transform(_matrix, [['t', x, y], ['s', 1, 100], ['t', -x, -y]]);
  33. } else if (direct === 'xy') {
  34. shape.transform([['t', x, y], ['s', 0.01, 0.01], ['t', -x, -y]]);
  35. var _matrix2 = shape.getMatrix();
  36. scaledMatrix = Matrix.transform(_matrix2, [['t', x, y], ['s', 100, 100], ['t', -x, -y]]);
  37. }
  38. return scaledMatrix;
  39. },
  40. getAnimateParam: function getAnimateParam(animateCfg, index, id) {
  41. var result = {};
  42. if (animateCfg.delay) {
  43. result.delay = Util.isFunction(animateCfg.delay) ? animateCfg.delay(index, id) : animateCfg.delay;
  44. }
  45. result.easing = animateCfg.easing;
  46. result.duration = animateCfg.duration;
  47. result.delay = animateCfg.delay;
  48. return result;
  49. },
  50. doAnimation: function doAnimation(shape, endState, animateCfg, callback) {
  51. var id = shape._id;
  52. var index = shape.get('index');
  53. var _Helpers$getAnimatePa = Helpers.getAnimateParam(animateCfg, index, id),
  54. easing = _Helpers$getAnimatePa.easing,
  55. delay = _Helpers$getAnimatePa.delay,
  56. duration = _Helpers$getAnimatePa.duration;
  57. var anim = shape.animate().to({
  58. attrs: endState,
  59. duration: duration,
  60. delay: delay,
  61. easing: easing
  62. });
  63. if (callback) {
  64. anim.onEnd(function () {
  65. callback();
  66. });
  67. }
  68. }
  69. };
  70. module.exports = Helpers;