EffectLine.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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';
  45. import Line from './Line';
  46. import * as zrUtil from 'zrender/lib/core/util';
  47. import { createSymbol } from '../../util/symbol';
  48. import * as vec2 from 'zrender/lib/core/vector';
  49. import * as curveUtil from 'zrender/lib/core/curve';
  50. var EffectLine =
  51. /** @class */
  52. function (_super) {
  53. __extends(EffectLine, _super);
  54. function EffectLine(lineData, idx, seriesScope) {
  55. var _this = _super.call(this) || this;
  56. _this.add(_this.createLine(lineData, idx, seriesScope));
  57. _this._updateEffectSymbol(lineData, idx);
  58. return _this;
  59. }
  60. EffectLine.prototype.createLine = function (lineData, idx, seriesScope) {
  61. return new Line(lineData, idx, seriesScope);
  62. };
  63. EffectLine.prototype._updateEffectSymbol = function (lineData, idx) {
  64. var itemModel = lineData.getItemModel(idx);
  65. var effectModel = itemModel.getModel('effect');
  66. var size = effectModel.get('symbolSize');
  67. var symbolType = effectModel.get('symbol');
  68. if (!zrUtil.isArray(size)) {
  69. size = [size, size];
  70. }
  71. var lineStyle = lineData.getItemVisual(idx, 'style');
  72. var color = effectModel.get('color') || lineStyle && lineStyle.stroke;
  73. var symbol = this.childAt(1);
  74. if (this._symbolType !== symbolType) {
  75. // Remove previous
  76. this.remove(symbol);
  77. symbol = createSymbol(symbolType, -0.5, -0.5, 1, 1, color);
  78. symbol.z2 = 100;
  79. symbol.culling = true;
  80. this.add(symbol);
  81. } // Symbol may be removed if loop is false
  82. if (!symbol) {
  83. return;
  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 self = this;
  100. var points = lineData.getItemLayout(idx);
  101. var period = effectModel.get('period') * 1000;
  102. var loop = effectModel.get('loop');
  103. var constantSpeed = effectModel.get('constantSpeed');
  104. var delayExpr = zrUtil.retrieve(effectModel.get('delay'), function (idx) {
  105. return idx / lineData.count() * period / 3;
  106. }); // Ignore when updating
  107. symbol.ignore = true;
  108. this._updateAnimationPoints(symbol, points);
  109. if (constantSpeed > 0) {
  110. period = this._getLineLength(symbol) / constantSpeed * 1000;
  111. }
  112. if (period !== this._period || loop !== this._loop) {
  113. symbol.stopAnimation();
  114. if (period > 0) {
  115. var delayNum = void 0;
  116. if (typeof delayExpr === 'function') {
  117. delayNum = delayExpr(idx);
  118. } else {
  119. delayNum = delayExpr;
  120. }
  121. if (symbol.__t > 0) {
  122. delayNum = -period * symbol.__t;
  123. }
  124. symbol.__t = 0;
  125. var animator = symbol.animate('', loop).when(period, {
  126. __t: 1
  127. }).delay(delayNum).during(function () {
  128. self._updateSymbolPosition(symbol);
  129. });
  130. if (!loop) {
  131. animator.done(function () {
  132. self.remove(symbol);
  133. });
  134. }
  135. animator.start();
  136. }
  137. }
  138. this._period = period;
  139. this._loop = loop;
  140. };
  141. EffectLine.prototype._getLineLength = function (symbol) {
  142. // Not so accurate
  143. return vec2.dist(symbol.__p1, symbol.__cp1) + vec2.dist(symbol.__cp1, symbol.__p2);
  144. };
  145. EffectLine.prototype._updateAnimationPoints = function (symbol, points) {
  146. symbol.__p1 = points[0];
  147. symbol.__p2 = points[1];
  148. symbol.__cp1 = points[2] || [(points[0][0] + points[1][0]) / 2, (points[0][1] + points[1][1]) / 2];
  149. };
  150. EffectLine.prototype.updateData = function (lineData, idx, seriesScope) {
  151. this.childAt(0).updateData(lineData, idx, seriesScope);
  152. this._updateEffectSymbol(lineData, idx);
  153. };
  154. EffectLine.prototype._updateSymbolPosition = function (symbol) {
  155. var p1 = symbol.__p1;
  156. var p2 = symbol.__p2;
  157. var cp1 = symbol.__cp1;
  158. var t = symbol.__t;
  159. var pos = [symbol.x, symbol.y];
  160. var lastPos = pos.slice();
  161. var quadraticAt = curveUtil.quadraticAt;
  162. var quadraticDerivativeAt = curveUtil.quadraticDerivativeAt;
  163. pos[0] = quadraticAt(p1[0], cp1[0], p2[0], t);
  164. pos[1] = quadraticAt(p1[1], cp1[1], p2[1], t); // Tangent
  165. var tx = quadraticDerivativeAt(p1[0], cp1[0], p2[0], t);
  166. var ty = quadraticDerivativeAt(p1[1], cp1[1], p2[1], t);
  167. symbol.rotation = -Math.atan2(ty, tx) - Math.PI / 2; // enable continuity trail for 'line', 'rect', 'roundRect' symbolType
  168. if (this._symbolType === 'line' || this._symbolType === 'rect' || this._symbolType === 'roundRect') {
  169. if (symbol.__lastT !== undefined && symbol.__lastT < symbol.__t) {
  170. symbol.scaleY = vec2.dist(lastPos, pos) * 1.05; // make sure the last segment render within endPoint
  171. if (t === 1) {
  172. pos[0] = lastPos[0] + (pos[0] - lastPos[0]) / 2;
  173. pos[1] = lastPos[1] + (pos[1] - lastPos[1]) / 2;
  174. }
  175. } else if (symbol.__lastT === 1) {
  176. // After first loop, symbol.__t does NOT start with 0, so connect p1 to pos directly.
  177. symbol.scaleY = 2 * vec2.dist(p1, pos);
  178. } else {
  179. symbol.scaleY = this._symbolScale[1];
  180. }
  181. }
  182. symbol.__lastT = symbol.__t;
  183. symbol.ignore = false;
  184. symbol.x = pos[0];
  185. symbol.y = pos[1];
  186. };
  187. EffectLine.prototype.updateLayout = function (lineData, idx) {
  188. this.childAt(0).updateLayout(lineData, idx);
  189. var effectModel = lineData.getItemModel(idx).getModel('effect');
  190. this._updateEffectAnimation(lineData, effectModel, idx);
  191. };
  192. return EffectLine;
  193. }(graphic.Group);
  194. export default EffectLine;