poly.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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"; // Poly path support NaN point
  41. import Path from 'zrender/lib/graphic/Path.js';
  42. import PathProxy from 'zrender/lib/core/PathProxy.js';
  43. import { cubicRootAt, cubicAt } from 'zrender/lib/core/curve.js';
  44. var mathMin = Math.min;
  45. var mathMax = Math.max;
  46. function isPointNull(x, y) {
  47. return isNaN(x) || isNaN(y);
  48. }
  49. /**
  50. * Draw smoothed line in non-monotone, in may cause undesired curve in extreme
  51. * situations. This should be used when points are non-monotone neither in x or
  52. * y dimension.
  53. */
  54. function drawSegment(ctx, points, start, segLen, allLen, dir, smooth, smoothMonotone, connectNulls) {
  55. var prevX;
  56. var prevY;
  57. var cpx0;
  58. var cpy0;
  59. var cpx1;
  60. var cpy1;
  61. var idx = start;
  62. var k = 0;
  63. for (; k < segLen; k++) {
  64. var x = points[idx * 2];
  65. var y = points[idx * 2 + 1];
  66. if (idx >= allLen || idx < 0) {
  67. break;
  68. }
  69. if (isPointNull(x, y)) {
  70. if (connectNulls) {
  71. idx += dir;
  72. continue;
  73. }
  74. break;
  75. }
  76. if (idx === start) {
  77. ctx[dir > 0 ? 'moveTo' : 'lineTo'](x, y);
  78. cpx0 = x;
  79. cpy0 = y;
  80. } else {
  81. var dx = x - prevX;
  82. var dy = y - prevY; // Ignore tiny segment.
  83. if (dx * dx + dy * dy < 0.5) {
  84. idx += dir;
  85. continue;
  86. }
  87. if (smooth > 0) {
  88. var nextIdx = idx + dir;
  89. var nextX = points[nextIdx * 2];
  90. var nextY = points[nextIdx * 2 + 1]; // Ignore duplicate point
  91. while (nextX === x && nextY === y && k < segLen) {
  92. k++;
  93. nextIdx += dir;
  94. idx += dir;
  95. nextX = points[nextIdx * 2];
  96. nextY = points[nextIdx * 2 + 1];
  97. x = points[idx * 2];
  98. y = points[idx * 2 + 1];
  99. dx = x - prevX;
  100. dy = y - prevY;
  101. }
  102. var tmpK = k + 1;
  103. if (connectNulls) {
  104. // Find next point not null
  105. while (isPointNull(nextX, nextY) && tmpK < segLen) {
  106. tmpK++;
  107. nextIdx += dir;
  108. nextX = points[nextIdx * 2];
  109. nextY = points[nextIdx * 2 + 1];
  110. }
  111. }
  112. var ratioNextSeg = 0.5;
  113. var vx = 0;
  114. var vy = 0;
  115. var nextCpx0 = void 0;
  116. var nextCpy0 = void 0; // Is last point
  117. if (tmpK >= segLen || isPointNull(nextX, nextY)) {
  118. cpx1 = x;
  119. cpy1 = y;
  120. } else {
  121. vx = nextX - prevX;
  122. vy = nextY - prevY;
  123. var dx0 = x - prevX;
  124. var dx1 = nextX - x;
  125. var dy0 = y - prevY;
  126. var dy1 = nextY - y;
  127. var lenPrevSeg = void 0;
  128. var lenNextSeg = void 0;
  129. if (smoothMonotone === 'x') {
  130. lenPrevSeg = Math.abs(dx0);
  131. lenNextSeg = Math.abs(dx1);
  132. var dir_1 = vx > 0 ? 1 : -1;
  133. cpx1 = x - dir_1 * lenPrevSeg * smooth;
  134. cpy1 = y;
  135. nextCpx0 = x + dir_1 * lenNextSeg * smooth;
  136. nextCpy0 = y;
  137. } else if (smoothMonotone === 'y') {
  138. lenPrevSeg = Math.abs(dy0);
  139. lenNextSeg = Math.abs(dy1);
  140. var dir_2 = vy > 0 ? 1 : -1;
  141. cpx1 = x;
  142. cpy1 = y - dir_2 * lenPrevSeg * smooth;
  143. nextCpx0 = x;
  144. nextCpy0 = y + dir_2 * lenNextSeg * smooth;
  145. } else {
  146. lenPrevSeg = Math.sqrt(dx0 * dx0 + dy0 * dy0);
  147. lenNextSeg = Math.sqrt(dx1 * dx1 + dy1 * dy1); // Use ratio of seg length
  148. ratioNextSeg = lenNextSeg / (lenNextSeg + lenPrevSeg);
  149. cpx1 = x - vx * smooth * (1 - ratioNextSeg);
  150. cpy1 = y - vy * smooth * (1 - ratioNextSeg); // cp0 of next segment
  151. nextCpx0 = x + vx * smooth * ratioNextSeg;
  152. nextCpy0 = y + vy * smooth * ratioNextSeg; // Smooth constraint between point and next point.
  153. // Avoid exceeding extreme after smoothing.
  154. nextCpx0 = mathMin(nextCpx0, mathMax(nextX, x));
  155. nextCpy0 = mathMin(nextCpy0, mathMax(nextY, y));
  156. nextCpx0 = mathMax(nextCpx0, mathMin(nextX, x));
  157. nextCpy0 = mathMax(nextCpy0, mathMin(nextY, y)); // Reclaculate cp1 based on the adjusted cp0 of next seg.
  158. vx = nextCpx0 - x;
  159. vy = nextCpy0 - y;
  160. cpx1 = x - vx * lenPrevSeg / lenNextSeg;
  161. cpy1 = y - vy * lenPrevSeg / lenNextSeg; // Smooth constraint between point and prev point.
  162. // Avoid exceeding extreme after smoothing.
  163. cpx1 = mathMin(cpx1, mathMax(prevX, x));
  164. cpy1 = mathMin(cpy1, mathMax(prevY, y));
  165. cpx1 = mathMax(cpx1, mathMin(prevX, x));
  166. cpy1 = mathMax(cpy1, mathMin(prevY, y)); // Adjust next cp0 again.
  167. vx = x - cpx1;
  168. vy = y - cpy1;
  169. nextCpx0 = x + vx * lenNextSeg / lenPrevSeg;
  170. nextCpy0 = y + vy * lenNextSeg / lenPrevSeg;
  171. }
  172. }
  173. ctx.bezierCurveTo(cpx0, cpy0, cpx1, cpy1, x, y);
  174. cpx0 = nextCpx0;
  175. cpy0 = nextCpy0;
  176. } else {
  177. ctx.lineTo(x, y);
  178. }
  179. }
  180. prevX = x;
  181. prevY = y;
  182. idx += dir;
  183. }
  184. return k;
  185. }
  186. var ECPolylineShape =
  187. /** @class */
  188. function () {
  189. function ECPolylineShape() {
  190. this.smooth = 0;
  191. this.smoothConstraint = true;
  192. }
  193. return ECPolylineShape;
  194. }();
  195. var ECPolyline =
  196. /** @class */
  197. function (_super) {
  198. __extends(ECPolyline, _super);
  199. function ECPolyline(opts) {
  200. var _this = _super.call(this, opts) || this;
  201. _this.type = 'ec-polyline';
  202. return _this;
  203. }
  204. ECPolyline.prototype.getDefaultStyle = function () {
  205. return {
  206. stroke: '#000',
  207. fill: null
  208. };
  209. };
  210. ECPolyline.prototype.getDefaultShape = function () {
  211. return new ECPolylineShape();
  212. };
  213. ECPolyline.prototype.buildPath = function (ctx, shape) {
  214. var points = shape.points;
  215. var i = 0;
  216. var len = points.length / 2; // const result = getBoundingBox(points, shape.smoothConstraint);
  217. if (shape.connectNulls) {
  218. // Must remove first and last null values avoid draw error in polygon
  219. for (; len > 0; len--) {
  220. if (!isPointNull(points[len * 2 - 2], points[len * 2 - 1])) {
  221. break;
  222. }
  223. }
  224. for (; i < len; i++) {
  225. if (!isPointNull(points[i * 2], points[i * 2 + 1])) {
  226. break;
  227. }
  228. }
  229. }
  230. while (i < len) {
  231. i += drawSegment(ctx, points, i, len, len, 1, shape.smooth, shape.smoothMonotone, shape.connectNulls) + 1;
  232. }
  233. };
  234. ECPolyline.prototype.getPointOn = function (xOrY, dim) {
  235. if (!this.path) {
  236. this.createPathProxy();
  237. this.buildPath(this.path, this.shape);
  238. }
  239. var path = this.path;
  240. var data = path.data;
  241. var CMD = PathProxy.CMD;
  242. var x0;
  243. var y0;
  244. var isDimX = dim === 'x';
  245. var roots = [];
  246. for (var i = 0; i < data.length;) {
  247. var cmd = data[i++];
  248. var x = void 0;
  249. var y = void 0;
  250. var x2 = void 0;
  251. var y2 = void 0;
  252. var x3 = void 0;
  253. var y3 = void 0;
  254. var t = void 0;
  255. switch (cmd) {
  256. case CMD.M:
  257. x0 = data[i++];
  258. y0 = data[i++];
  259. break;
  260. case CMD.L:
  261. x = data[i++];
  262. y = data[i++];
  263. t = isDimX ? (xOrY - x0) / (x - x0) : (xOrY - y0) / (y - y0);
  264. if (t <= 1 && t >= 0) {
  265. var val = isDimX ? (y - y0) * t + y0 : (x - x0) * t + x0;
  266. return isDimX ? [xOrY, val] : [val, xOrY];
  267. }
  268. x0 = x;
  269. y0 = y;
  270. break;
  271. case CMD.C:
  272. x = data[i++];
  273. y = data[i++];
  274. x2 = data[i++];
  275. y2 = data[i++];
  276. x3 = data[i++];
  277. y3 = data[i++];
  278. var nRoot = isDimX ? cubicRootAt(x0, x, x2, x3, xOrY, roots) : cubicRootAt(y0, y, y2, y3, xOrY, roots);
  279. if (nRoot > 0) {
  280. for (var i_1 = 0; i_1 < nRoot; i_1++) {
  281. var t_1 = roots[i_1];
  282. if (t_1 <= 1 && t_1 >= 0) {
  283. var val = isDimX ? cubicAt(y0, y, y2, y3, t_1) : cubicAt(x0, x, x2, x3, t_1);
  284. return isDimX ? [xOrY, val] : [val, xOrY];
  285. }
  286. }
  287. }
  288. x0 = x3;
  289. y0 = y3;
  290. break;
  291. }
  292. }
  293. };
  294. return ECPolyline;
  295. }(Path);
  296. export { ECPolyline };
  297. var ECPolygonShape =
  298. /** @class */
  299. function (_super) {
  300. __extends(ECPolygonShape, _super);
  301. function ECPolygonShape() {
  302. return _super !== null && _super.apply(this, arguments) || this;
  303. }
  304. return ECPolygonShape;
  305. }(ECPolylineShape);
  306. var ECPolygon =
  307. /** @class */
  308. function (_super) {
  309. __extends(ECPolygon, _super);
  310. function ECPolygon(opts) {
  311. var _this = _super.call(this, opts) || this;
  312. _this.type = 'ec-polygon';
  313. return _this;
  314. }
  315. ECPolygon.prototype.getDefaultShape = function () {
  316. return new ECPolygonShape();
  317. };
  318. ECPolygon.prototype.buildPath = function (ctx, shape) {
  319. var points = shape.points;
  320. var stackedOnPoints = shape.stackedOnPoints;
  321. var i = 0;
  322. var len = points.length / 2;
  323. var smoothMonotone = shape.smoothMonotone;
  324. if (shape.connectNulls) {
  325. // Must remove first and last null values avoid draw error in polygon
  326. for (; len > 0; len--) {
  327. if (!isPointNull(points[len * 2 - 2], points[len * 2 - 1])) {
  328. break;
  329. }
  330. }
  331. for (; i < len; i++) {
  332. if (!isPointNull(points[i * 2], points[i * 2 + 1])) {
  333. break;
  334. }
  335. }
  336. }
  337. while (i < len) {
  338. var k = drawSegment(ctx, points, i, len, len, 1, shape.smooth, smoothMonotone, shape.connectNulls);
  339. drawSegment(ctx, stackedOnPoints, i + k - 1, k, len, -1, shape.stackedOnSmooth, smoothMonotone, shape.connectNulls);
  340. i += k + 1;
  341. ctx.closePath();
  342. }
  343. };
  344. return ECPolygon;
  345. }(Path);
  346. export { ECPolygon };