util.js 2.2 KB

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