EffectLine.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. /**
  42. * Provide effect for line
  43. */
  44. import * as graphic from '../../util/graphic.js';
  45. import Line from './Line.js';
  46. import * as zrUtil from 'zrender/lib/core/util.js';
  47. import { createSymbol } from '../../util/symbol.js';
  48. import * as vec2 from 'zrender/lib/core/vector.js';
  49. import * as curveUtil from 'zrender/lib/core/curve.js';
  50. var EffectLine = /** @class */function (_super) {
  51. __extends(EffectLine, _super);
  52. function EffectLine(lineData, idx, seriesScope) {
  53. var _this = _super.call(this) || this;
  54. _this.add(_this.createLine(lineData, idx, seriesScope));
  55. _this._updateEffectSymbol(lineData, idx);
  56. return _this;
  57. }
  58. EffectLine.prototype.createLine = function (lineData, idx, seriesScope) {
  59. return new Line(lineData, idx, seriesScope);
  60. };
  61. EffectLine.prototype._updateEffectSymbol = function (lineData, idx) {
  62. var itemModel = lineData.getItemModel(idx);
  63. var effectModel = itemModel.getModel('effect');
  64. var size = effectModel.get('symbolSize');
  65. var symbolType = effectModel.get('symbol');
  66. if (!zrUtil.isArray(size)) {
  67. size = [size, size];
  68. }
  69. var lineStyle = lineData.getItemVisual(idx, 'style');
  70. var color = effectModel.get('color') || lineStyle && lineStyle.stroke;
  71. var symbol = this.childAt(1);
  72. if (this._symbolType !== symbolType) {
  73. // Remove previous
  74. this.remove(symbol);
  75. symbol = createSymbol(symbolType, -0.5, -0.5, 1, 1, color);
  76. symbol.z2 = 100;
  77. symbol.culling = true;
  78. this.add(symbol);
  79. }
  80. // Symbol may be removed if loop is false
  81. if (!symbol) {
  82. return;
  83. }
  84. // Shadow color is same with color in default
  85. symbol.setStyle('shadowColor', color);
  86. symbol.setStyle(effectModel.getItemStyle(['color']));
  87. symbol.scaleX = size[0];
  88. symbol.scaleY = size[1];
  89. symbol.setColor(color);
  90. this._symbolType = symbolType;
  91. this._symbolScale = size;
  92. this._updateEffectAnimation(lineData, effectModel, idx);
  93. };
  94. EffectLine.prototype._updateEffectAnimation = function (lineData, effectModel, idx) {
  95. var symbol = this.childAt(1);
  96. if (!symbol) {
  97. return;
  98. }
  99. var points = lineData.getItemLayout(idx);
  100. var period = effectModel.get('period') * 1000;
  101. var loop = effectModel.get('loop');
  102. var roundTrip = effectModel.get('roundTrip');
  103. var constantSpeed = effectModel.get('constantSpeed');
  104. var delayExpr = zrUtil.retrieve(effectModel.get('delay'), function (idx) {
  105. return idx / lineData.count() * period / 3;
  106. });
  107. // Ignore when updating
  108. symbol.ignore = true;
  109. this._updateAnimationPoints(symbol, points);
  110. if (constantSpeed > 0) {
  111. period = this._getLineLength(symbol) / constantSpeed * 1000;
  112. }
  113. if (period !== this._period || loop !== this._loop || roundTrip !== this._roundTrip) {
  114. symbol.stopAnimation();
  115. var delayNum = void 0;
  116. if (zrUtil.isFunction(delayExpr)) {
  117. delayNum = delayExpr(idx);
  118. } else {
  119. delayNum = delayExpr;
  120. }
  121. if (symbol.__t > 0) {
  122. delayNum = -period * symbol.__t;
  123. }
  124. this._animateSymbol(symbol, period, delayNum, loop, roundTrip);
  125. }
  126. this._period = period;
  127. this._loop = loop;
  128. this._roundTrip = roundTrip;
  129. };
  130. EffectLine.prototype._animateSymbol = function (symbol, period, delayNum, loop, roundTrip) {
  131. if (period > 0) {
  132. symbol.__t = 0;
  133. var self_1 = this;
  134. var animator = symbol.animate('', loop).when(roundTrip ? period * 2 : period, {
  135. __t: roundTrip ? 2 : 1
  136. }).delay(delayNum).during(function () {
  137. self_1._updateSymbolPosition(symbol);
  138. });
  139. if (!loop) {
  140. animator.done(function () {
  141. self_1.remove(symbol);
  142. });
  143. }
  144. animator.start();
  145. }
  146. };
  147. EffectLine.prototype._getLineLength = function (symbol) {
  148. // Not so accurate
  149. return vec2.dist(symbol.__p1, symbol.__cp1) + vec2.dist(symbol.__cp1, symbol.__p2);
  150. };
  151. EffectLine.prototype._updateAnimationPoints = function (symbol, points) {
  152. symbol.__p1 = points[0];
  153. symbol.__p2 = points[1];
  154. symbol.__cp1 = points[2] || [(points[0][0] + points[1][0]) / 2, (points[0][1] + points[1][1]) / 2];
  155. };
  156. EffectLine.prototype.updateData = function (lineData, idx, seriesScope) {
  157. this.childAt(0).updateData(lineData, idx, seriesScope);
  158. this._updateEffectSymbol(lineData, idx);
  159. };
  160. EffectLine.prototype._updateSymbolPosition = function (symbol) {
  161. var p1 = symbol.__p1;
  162. var p2 = symbol.__p2;
  163. var cp1 = symbol.__cp1;
  164. var t = symbol.__t < 1 ? symbol.__t : 2 - symbol.__t;
  165. var pos = [symbol.x, symbol.y];
  166. var lastPos = pos.slice();
  167. var quadraticAt = curveUtil.quadraticAt;
  168. var quadraticDerivativeAt = curveUtil.quadraticDerivativeAt;
  169. pos[0] = quadraticAt(p1[0], cp1[0], p2[0], t);
  170. pos[1] = quadraticAt(p1[1], cp1[1], p2[1], t);
  171. // Tangent
  172. var tx = symbol.__t < 1 ? quadraticDerivativeAt(p1[0], cp1[0], p2[0], t) : quadraticDerivativeAt(p2[0], cp1[0], p1[0], 1 - t);
  173. var ty = symbol.__t < 1 ? quadraticDerivativeAt(p1[1], cp1[1], p2[1], t) : quadraticDerivativeAt(p2[1], cp1[1], p1[1], 1 - t);
  174. symbol.rotation = -Math.atan2(ty, tx) - Math.PI / 2;
  175. // enable continuity trail for 'line', 'rect', 'roundRect' symbolType
  176. if (this._symbolType === 'line' || this._symbolType === 'rect' || this._symbolType === 'roundRect') {
  177. if (symbol.__lastT !== undefined && symbol.__lastT < symbol.__t) {
  178. symbol.scaleY = vec2.dist(lastPos, pos) * 1.05;
  179. // make sure the last segment render within endPoint
  180. if (t === 1) {
  181. pos[0] = lastPos[0] + (pos[0] - lastPos[0]) / 2;
  182. pos[1] = lastPos[1] + (pos[1] - lastPos[1]) / 2;
  183. }
  184. } else if (symbol.__lastT === 1) {
  185. // After first loop, symbol.__t does NOT start with 0, so connect p1 to pos directly.
  186. symbol.scaleY = 2 * vec2.dist(p1, pos);
  187. } else {
  188. symbol.scaleY = this._symbolScale[1];
  189. }
  190. }
  191. symbol.__lastT = symbol.__t;
  192. symbol.ignore = false;
  193. symbol.x = pos[0];
  194. symbol.y = pos[1];
  195. };
  196. EffectLine.prototype.updateLayout = function (lineData, idx) {
  197. this.childAt(0).updateLayout(lineData, idx);
  198. var effectModel = lineData.getItemModel(idx).getModel('effect');
  199. this._updateEffectAnimation(lineData, effectModel, idx);
  200. };
  201. return EffectLine;
  202. }(graphic.Group);
  203. export default EffectLine;