util.js 1.9 KB

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