Advertisement
bac1lla

Untitled

May 26th, 2022
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. ymaps.modules.define('AnimatedLine', [
  2. 'util.defineClass',
  3. 'Polyline',
  4. 'vow'
  5. ], function(provide, defineClass, Polyline, vow) {
  6. /**
  7. * @fileOverview Анимированная линия.
  8. */
  9. /**
  10. * Создает экземпляр анимированной линии.
  11. * @class AnimatedLine. Представляет собой геообъект с геометрией geometry.LineString.
  12. * @param {Boolean} [options.animationTime = 4000] Длительность анимации.
  13. **/
  14. function AnimatedLine(geometry, properties, options) {
  15. AnimatedLine.superclass.constructor.call(this, geometry, properties, options);
  16. this._loopTime = 50;
  17. this._animationTime = this.options.get('animationTime', 300);
  18. // Вычислим длину переданной линии.
  19. var distance = 0;
  20. var previousElem = geometry[0];
  21. this.geometry.getCoordinates().forEach(function(elem) {
  22. distance += getDistance(elem, previousElem);
  23. // console.log(distance)
  24. previousElem = elem;
  25. });
  26. // Вычислим минимальный интервал отрисовки.
  27. this._animationInterval = distance / this._animationTime * this._loopTime;
  28. // Создадим массив с более частым расположением промежуточных точек.
  29. this._smoothCoords = generateSmoothCoords(geometry, this._animationInterval);
  30. // console.log(generateSmoothCoords(geometry, this._animationInterval))
  31. }
  32. defineClass(AnimatedLine, Polyline, {
  33. // Анимировать линию.
  34. start: function() {
  35. var value = 0;
  36. var coords = this._smoothCoords;
  37. var line = this;
  38. var loopTime = this._loopTime;
  39. // Будем добавлять по одной точке каждые 50 мс.
  40. function loop(value, currentTime, previousTime) {
  41. if (value < coords.length) {
  42. if (!currentTime || (currentTime - previousTime) > loopTime) {
  43. line.geometry.set(value, coords[value]);
  44. value++;
  45. previousTime = currentTime;
  46. }
  47. requestAnimationFrame(function(time) {
  48. loop(value, time, previousTime || time)
  49. });
  50. } else {
  51. // Бросаем событие окончания отрисовки линии.
  52. line.events.fire('animationfinished');
  53. }
  54. }
  55.  
  56. loop(value);
  57. },
  58. // Убрать отрисованную линию.
  59. reset: function() {
  60. this.geometry.setCoordinates([]);
  61. },
  62. // Запустить полный цикл анимации.
  63. animate: function() {
  64. this.reset();
  65. this.start();
  66. var deferred = vow.defer();
  67. this.events.once('animationfinished', function() {
  68. deferred.resolve();
  69. });
  70. return deferred.promise();
  71. }
  72.  
  73. });
  74. // Функция генерации частых координат по заданной линии.
  75. function generateSmoothCoords(coords, interval) {
  76. var smoothCoords = [];
  77. smoothCoords.push(coords[0]);
  78. for (var i = 1; i < coords.length; i++) {
  79. var difference = [coords[i][0] - coords[i - 1][0], coords[i][1] - coords[i - 1][1]];
  80. var maxAmount = Math.max(Math.abs(difference[0] / interval), Math.abs(difference[1] / interval));
  81. var minDifference = [difference[0] / maxAmount, difference[1] / maxAmount];
  82. var lastCoord = coords[i - 1];
  83. while (maxAmount > 1) {
  84. lastCoord = [lastCoord[0] + minDifference[0], lastCoord[1] + minDifference[1]];
  85. smoothCoords.push(lastCoord);
  86. maxAmount--;
  87. }
  88. smoothCoords.push(coords[i])
  89. }
  90. return smoothCoords;
  91. }
  92. // Функция нахождения расстояния между двумя точками на плоскости.
  93. function getDistance(point1, point2) {
  94. return Math.sqrt(
  95. Math.pow((point2[0] - point1[0]), 2) +
  96. Math.pow((point2[1] - point1[1]), 2)
  97. );
  98. }
  99. provide(AnimatedLine);
  100. });
  101.  
  102.  
  103.  
  104.  
  105.  
  106. var firstAnimatedLine = new ymaps.AnimatedLine([
  107. [55.761223661714205, 37.57854299428123],
  108. [55.76129474190374, 37.57836060406823],
  109. [55.76149285834102, 37.57855640532632],
  110. [55.76173267134118, 37.57864573959325],
  111. [55.761782872763874, 37.578559582240004],
  112. [55.7622647306412, 37.57857741008619],
  113. [55.76247342821094, 37.57840038429122],
  114. [55.762818964832924, 37.57765342764373],
  115. [55.76292179998886, 37.57748713068481],
  116. [55.762890042102114, 37.577167947812036],
  117. [55.76292179998886, 37.576878269238435],
  118. [55.763076052212064, 37.57669587902541],
  119. [55.76309672830313, 37.57723949881904]
  120. ], {}, {
  121. // Задаем цвет.
  122. strokeColor: "#ED4543",
  123. // Задаем ширину линии.
  124. strokeWidth: 5,
  125. // Задаем длительность анимации.
  126. animationTime: 10000
  127. });
  128.  
  129.  
  130.  
  131.  
  132. // Добавляем линии на карту.
  133. myMap.geoObjects.add(firstAnimatedLine);
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement