EffectPolyline.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var Polyline = require("./Polyline");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var EffectLine = require("./EffectLine");
  22. var vec2 = require("zrender/lib/core/vector");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * Provide effect for line
  43. * @module echarts/chart/helper/EffectLine
  44. */
  45. /**
  46. * @constructor
  47. * @extends {module:echarts/chart/helper/EffectLine}
  48. * @alias {module:echarts/chart/helper/Polyline}
  49. */
  50. function EffectPolyline(lineData, idx, seriesScope) {
  51. EffectLine.call(this, lineData, idx, seriesScope);
  52. this._lastFrame = 0;
  53. this._lastFramePercent = 0;
  54. }
  55. var effectPolylineProto = EffectPolyline.prototype; // Overwrite
  56. effectPolylineProto.createLine = function (lineData, idx, seriesScope) {
  57. return new Polyline(lineData, idx, seriesScope);
  58. }; // Overwrite
  59. effectPolylineProto.updateAnimationPoints = function (symbol, points) {
  60. this._points = points;
  61. var accLenArr = [0];
  62. var len = 0;
  63. for (var i = 1; i < points.length; i++) {
  64. var p1 = points[i - 1];
  65. var p2 = points[i];
  66. len += vec2.dist(p1, p2);
  67. accLenArr.push(len);
  68. }
  69. if (len === 0) {
  70. return;
  71. }
  72. for (var i = 0; i < accLenArr.length; i++) {
  73. accLenArr[i] /= len;
  74. }
  75. this._offsets = accLenArr;
  76. this._length = len;
  77. }; // Overwrite
  78. effectPolylineProto.getLineLength = function (symbol) {
  79. return this._length;
  80. }; // Overwrite
  81. effectPolylineProto.updateSymbolPosition = function (symbol) {
  82. var t = symbol.__t;
  83. var points = this._points;
  84. var offsets = this._offsets;
  85. var len = points.length;
  86. if (!offsets) {
  87. // Has length 0
  88. return;
  89. }
  90. var lastFrame = this._lastFrame;
  91. var frame;
  92. if (t < this._lastFramePercent) {
  93. // Start from the next frame
  94. // PENDING start from lastFrame ?
  95. var start = Math.min(lastFrame + 1, len - 1);
  96. for (frame = start; frame >= 0; frame--) {
  97. if (offsets[frame] <= t) {
  98. break;
  99. }
  100. } // PENDING really need to do this ?
  101. frame = Math.min(frame, len - 2);
  102. } else {
  103. for (var frame = lastFrame; frame < len; frame++) {
  104. if (offsets[frame] > t) {
  105. break;
  106. }
  107. }
  108. frame = Math.min(frame - 1, len - 2);
  109. }
  110. vec2.lerp(symbol.position, points[frame], points[frame + 1], (t - offsets[frame]) / (offsets[frame + 1] - offsets[frame]));
  111. var tx = points[frame + 1][0] - points[frame][0];
  112. var ty = points[frame + 1][1] - points[frame][1];
  113. symbol.rotation = -Math.atan2(ty, tx) - Math.PI / 2;
  114. this._lastFrame = frame;
  115. this._lastFramePercent = t;
  116. symbol.ignore = false;
  117. };
  118. zrUtil.inherits(EffectPolyline, EffectLine);
  119. var _default = EffectPolyline;
  120. module.exports = _default;