You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

106 lines
3.0 KiB

  1. import { getBarHeightAndYAttr, getSplineCurvePointsStr } from './draw-utils';
  2. export const UNIT_ANIM_DUR = 350;
  3. export const PATH_ANIM_DUR = 350;
  4. export const MARKER_LINE_ANIM_DUR = UNIT_ANIM_DUR;
  5. export const REPLACE_ALL_NEW_DUR = 250;
  6. export const STD_EASING = 'easein';
  7. export function translate(unit, oldCoord, newCoord, duration) {
  8. let old = typeof oldCoord === 'string' ? oldCoord : oldCoord.join(', ');
  9. return [
  10. unit,
  11. {transform: newCoord.join(', ')},
  12. duration,
  13. STD_EASING,
  14. "translate",
  15. {transform: old}
  16. ];
  17. }
  18. export function translateVertLine(xLine, newX, oldX) {
  19. return translate(xLine, [oldX, 0], [newX, 0], MARKER_LINE_ANIM_DUR);
  20. }
  21. export function translateHoriLine(yLine, newY, oldY) {
  22. return translate(yLine, [0, oldY], [0, newY], MARKER_LINE_ANIM_DUR);
  23. }
  24. export function animateRegion(rectGroup, newY1, newY2, oldY2) {
  25. let newHeight = newY1 - newY2;
  26. let rect = rectGroup.childNodes[0];
  27. let width = rect.getAttribute("width");
  28. let rectAnim = [
  29. rect,
  30. { height: newHeight, 'stroke-dasharray': `${width}, ${newHeight}` },
  31. MARKER_LINE_ANIM_DUR,
  32. STD_EASING
  33. ];
  34. let groupAnim = translate(rectGroup, [0, oldY2], [0, newY2], MARKER_LINE_ANIM_DUR);
  35. return [rectAnim, groupAnim];
  36. }
  37. export function animateBar(bar, x, yTop, width, offset=0, meta={}) {
  38. let [height, y] = getBarHeightAndYAttr(yTop, meta.zeroLine);
  39. y -= offset;
  40. if(bar.nodeName !== 'rect') {
  41. let rect = bar.childNodes[0];
  42. let rectAnim = [
  43. rect,
  44. {width: width, height: height},
  45. UNIT_ANIM_DUR,
  46. STD_EASING
  47. ];
  48. let oldCoordStr = bar.getAttribute("transform").split("(")[1].slice(0, -1);
  49. let groupAnim = translate(bar, oldCoordStr, [x, y], MARKER_LINE_ANIM_DUR);
  50. return [rectAnim, groupAnim];
  51. } else {
  52. return [[bar, {width: width, height: height, x: x, y: y}, UNIT_ANIM_DUR, STD_EASING]];
  53. }
  54. // bar.animate({height: args.newHeight, y: yTop}, UNIT_ANIM_DUR, mina.easein);
  55. }
  56. export function animateDot(dot, x, y) {
  57. if(dot.nodeName !== 'circle') {
  58. let oldCoordStr = dot.getAttribute("transform").split("(")[1].slice(0, -1);
  59. let groupAnim = translate(dot, oldCoordStr, [x, y], MARKER_LINE_ANIM_DUR);
  60. return [groupAnim];
  61. } else {
  62. return [[dot, {cx: x, cy: y}, UNIT_ANIM_DUR, STD_EASING]];
  63. }
  64. // dot.animate({cy: yTop}, UNIT_ANIM_DUR, mina.easein);
  65. }
  66. export function animatePath(paths, newXList, newYList, zeroLine, spline) {
  67. let pathComponents = [];
  68. let pointsStr = newYList.map((y, i) => (newXList[i] + ',' + y)).join("L");
  69. if (spline)
  70. pointsStr = getSplineCurvePointsStr(newXList, newYList);
  71. const animPath = [paths.path, {d:"M" + pointsStr}, PATH_ANIM_DUR, STD_EASING];
  72. pathComponents.push(animPath);
  73. if(paths.region) {
  74. let regStartPt = `${newXList[0]},${zeroLine}L`;
  75. let regEndPt = `L${newXList.slice(-1)[0]}, ${zeroLine}`;
  76. const animRegion = [
  77. paths.region,
  78. {d:"M" + regStartPt + pointsStr + regEndPt},
  79. PATH_ANIM_DUR,
  80. STD_EASING
  81. ];
  82. pathComponents.push(animRegion);
  83. }
  84. return pathComponents;
  85. }
  86. export function animatePathStr(oldPath, pathStr) {
  87. return [oldPath, {d: pathStr}, UNIT_ANIM_DUR, STD_EASING];
  88. }