poly.js 11 KB

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