poly.js 11 KB

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