poly.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. var Path = require("zrender/lib/graphic/Path");
  20. var vec2 = require("zrender/lib/core/vector");
  21. var fixClipWithShadow = require("zrender/lib/graphic/helper/fixClipWithShadow");
  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. // Poly path support NaN point
  41. var vec2Min = vec2.min;
  42. var vec2Max = vec2.max;
  43. var scaleAndAdd = vec2.scaleAndAdd;
  44. var v2Copy = vec2.copy; // Temporary variable
  45. var v = [];
  46. var cp0 = [];
  47. var cp1 = [];
  48. function isPointNull(p) {
  49. return isNaN(p[0]) || isNaN(p[1]);
  50. }
  51. function drawSegment(ctx, points, start, segLen, allLen, dir, smoothMin, smoothMax, smooth, smoothMonotone, connectNulls) {
  52. // if (smoothMonotone == null) {
  53. // if (isMono(points, 'x')) {
  54. // return drawMono(ctx, points, start, segLen, allLen,
  55. // dir, smoothMin, smoothMax, smooth, 'x', connectNulls);
  56. // }
  57. // else if (isMono(points, 'y')) {
  58. // return drawMono(ctx, points, start, segLen, allLen,
  59. // dir, smoothMin, smoothMax, smooth, 'y', connectNulls);
  60. // }
  61. // else {
  62. // return drawNonMono.apply(this, arguments);
  63. // }
  64. // }
  65. // else if (smoothMonotone !== 'none' && isMono(points, smoothMonotone)) {
  66. // return drawMono.apply(this, arguments);
  67. // }
  68. // else {
  69. // return drawNonMono.apply(this, arguments);
  70. // }
  71. if (smoothMonotone === 'none' || !smoothMonotone) {
  72. return drawNonMono.apply(this, arguments);
  73. } else {
  74. return drawMono.apply(this, arguments);
  75. }
  76. }
  77. /**
  78. * Check if points is in monotone.
  79. *
  80. * @param {number[][]} points Array of points which is in [x, y] form
  81. * @param {string} smoothMonotone 'x', 'y', or 'none', stating for which
  82. * dimension that is checking.
  83. * If is 'none', `drawNonMono` should be
  84. * called.
  85. * If is undefined, either being monotone
  86. * in 'x' or 'y' will call `drawMono`.
  87. */
  88. // function isMono(points, smoothMonotone) {
  89. // if (points.length <= 1) {
  90. // return true;
  91. // }
  92. // var dim = smoothMonotone === 'x' ? 0 : 1;
  93. // var last = points[0][dim];
  94. // var lastDiff = 0;
  95. // for (var i = 1; i < points.length; ++i) {
  96. // var diff = points[i][dim] - last;
  97. // if (!isNaN(diff) && !isNaN(lastDiff)
  98. // && diff !== 0 && lastDiff !== 0
  99. // && ((diff >= 0) !== (lastDiff >= 0))
  100. // ) {
  101. // return false;
  102. // }
  103. // if (!isNaN(diff) && diff !== 0) {
  104. // lastDiff = diff;
  105. // last = points[i][dim];
  106. // }
  107. // }
  108. // return true;
  109. // }
  110. /**
  111. * Draw smoothed line in monotone, in which only vertical or horizontal bezier
  112. * control points will be used. This should be used when points are monotone
  113. * either in x or y dimension.
  114. */
  115. function drawMono(ctx, points, start, segLen, allLen, dir, smoothMin, smoothMax, smooth, smoothMonotone, connectNulls) {
  116. var prevIdx = 0;
  117. var idx = start;
  118. for (var k = 0; k < segLen; k++) {
  119. var p = points[idx];
  120. if (idx >= allLen || idx < 0) {
  121. break;
  122. }
  123. if (isPointNull(p)) {
  124. if (connectNulls) {
  125. idx += dir;
  126. continue;
  127. }
  128. break;
  129. }
  130. if (idx === start) {
  131. ctx[dir > 0 ? 'moveTo' : 'lineTo'](p[0], p[1]);
  132. } else {
  133. if (smooth > 0) {
  134. var prevP = points[prevIdx];
  135. var dim = smoothMonotone === 'y' ? 1 : 0; // Length of control point to p, either in x or y, but not both
  136. var ctrlLen = (p[dim] - prevP[dim]) * smooth;
  137. v2Copy(cp0, prevP);
  138. cp0[dim] = prevP[dim] + ctrlLen;
  139. v2Copy(cp1, p);
  140. cp1[dim] = p[dim] - ctrlLen;
  141. ctx.bezierCurveTo(cp0[0], cp0[1], cp1[0], cp1[1], p[0], p[1]);
  142. } else {
  143. ctx.lineTo(p[0], p[1]);
  144. }
  145. }
  146. prevIdx = idx;
  147. idx += dir;
  148. }
  149. return k;
  150. }
  151. /**
  152. * Draw smoothed line in non-monotone, in may cause undesired curve in extreme
  153. * situations. This should be used when points are non-monotone neither in x or
  154. * y dimension.
  155. */
  156. function drawNonMono(ctx, points, start, segLen, allLen, dir, smoothMin, smoothMax, smooth, smoothMonotone, connectNulls) {
  157. var prevIdx = 0;
  158. var idx = start;
  159. for (var k = 0; k < segLen; k++) {
  160. var p = points[idx];
  161. if (idx >= allLen || idx < 0) {
  162. break;
  163. }
  164. if (isPointNull(p)) {
  165. if (connectNulls) {
  166. idx += dir;
  167. continue;
  168. }
  169. break;
  170. }
  171. if (idx === start) {
  172. ctx[dir > 0 ? 'moveTo' : 'lineTo'](p[0], p[1]);
  173. v2Copy(cp0, p);
  174. } else {
  175. if (smooth > 0) {
  176. var nextIdx = idx + dir;
  177. var nextP = points[nextIdx];
  178. if (connectNulls) {
  179. // Find next point not null
  180. while (nextP && isPointNull(points[nextIdx])) {
  181. nextIdx += dir;
  182. nextP = points[nextIdx];
  183. }
  184. }
  185. var ratioNextSeg = 0.5;
  186. var prevP = points[prevIdx];
  187. var nextP = points[nextIdx]; // Last point
  188. if (!nextP || isPointNull(nextP)) {
  189. v2Copy(cp1, p);
  190. } else {
  191. // If next data is null in not connect case
  192. if (isPointNull(nextP) && !connectNulls) {
  193. nextP = p;
  194. }
  195. vec2.sub(v, nextP, prevP);
  196. var lenPrevSeg;
  197. var lenNextSeg;
  198. if (smoothMonotone === 'x' || smoothMonotone === 'y') {
  199. var dim = smoothMonotone === 'x' ? 0 : 1;
  200. lenPrevSeg = Math.abs(p[dim] - prevP[dim]);
  201. lenNextSeg = Math.abs(p[dim] - nextP[dim]);
  202. } else {
  203. lenPrevSeg = vec2.dist(p, prevP);
  204. lenNextSeg = vec2.dist(p, nextP);
  205. } // Use ratio of seg length
  206. ratioNextSeg = lenNextSeg / (lenNextSeg + lenPrevSeg);
  207. scaleAndAdd(cp1, p, v, -smooth * (1 - ratioNextSeg));
  208. } // Smooth constraint
  209. vec2Min(cp0, cp0, smoothMax);
  210. vec2Max(cp0, cp0, smoothMin);
  211. vec2Min(cp1, cp1, smoothMax);
  212. vec2Max(cp1, cp1, smoothMin);
  213. ctx.bezierCurveTo(cp0[0], cp0[1], cp1[0], cp1[1], p[0], p[1]); // cp0 of next segment
  214. scaleAndAdd(cp0, p, v, smooth * ratioNextSeg);
  215. } else {
  216. ctx.lineTo(p[0], p[1]);
  217. }
  218. }
  219. prevIdx = idx;
  220. idx += dir;
  221. }
  222. return k;
  223. }
  224. function getBoundingBox(points, smoothConstraint) {
  225. var ptMin = [Infinity, Infinity];
  226. var ptMax = [-Infinity, -Infinity];
  227. if (smoothConstraint) {
  228. for (var i = 0; i < points.length; i++) {
  229. var pt = points[i];
  230. if (pt[0] < ptMin[0]) {
  231. ptMin[0] = pt[0];
  232. }
  233. if (pt[1] < ptMin[1]) {
  234. ptMin[1] = pt[1];
  235. }
  236. if (pt[0] > ptMax[0]) {
  237. ptMax[0] = pt[0];
  238. }
  239. if (pt[1] > ptMax[1]) {
  240. ptMax[1] = pt[1];
  241. }
  242. }
  243. }
  244. return {
  245. min: smoothConstraint ? ptMin : ptMax,
  246. max: smoothConstraint ? ptMax : ptMin
  247. };
  248. }
  249. var Polyline = Path.extend({
  250. type: 'ec-polyline',
  251. shape: {
  252. points: [],
  253. smooth: 0,
  254. smoothConstraint: true,
  255. smoothMonotone: null,
  256. connectNulls: false
  257. },
  258. style: {
  259. fill: null,
  260. stroke: '#000'
  261. },
  262. brush: fixClipWithShadow(Path.prototype.brush),
  263. buildPath: function (ctx, shape) {
  264. var points = shape.points;
  265. var i = 0;
  266. var len = points.length;
  267. var result = getBoundingBox(points, shape.smoothConstraint);
  268. if (shape.connectNulls) {
  269. // Must remove first and last null values avoid draw error in polygon
  270. for (; len > 0; len--) {
  271. if (!isPointNull(points[len - 1])) {
  272. break;
  273. }
  274. }
  275. for (; i < len; i++) {
  276. if (!isPointNull(points[i])) {
  277. break;
  278. }
  279. }
  280. }
  281. while (i < len) {
  282. i += drawSegment(ctx, points, i, len, len, 1, result.min, result.max, shape.smooth, shape.smoothMonotone, shape.connectNulls) + 1;
  283. }
  284. }
  285. });
  286. var Polygon = Path.extend({
  287. type: 'ec-polygon',
  288. shape: {
  289. points: [],
  290. // Offset between stacked base points and points
  291. stackedOnPoints: [],
  292. smooth: 0,
  293. stackedOnSmooth: 0,
  294. smoothConstraint: true,
  295. smoothMonotone: null,
  296. connectNulls: false
  297. },
  298. brush: fixClipWithShadow(Path.prototype.brush),
  299. buildPath: function (ctx, shape) {
  300. var points = shape.points;
  301. var stackedOnPoints = shape.stackedOnPoints;
  302. var i = 0;
  303. var len = points.length;
  304. var smoothMonotone = shape.smoothMonotone;
  305. var bbox = getBoundingBox(points, shape.smoothConstraint);
  306. var stackedOnBBox = getBoundingBox(stackedOnPoints, shape.smoothConstraint);
  307. if (shape.connectNulls) {
  308. // Must remove first and last null values avoid draw error in polygon
  309. for (; len > 0; len--) {
  310. if (!isPointNull(points[len - 1])) {
  311. break;
  312. }
  313. }
  314. for (; i < len; i++) {
  315. if (!isPointNull(points[i])) {
  316. break;
  317. }
  318. }
  319. }
  320. while (i < len) {
  321. var k = drawSegment(ctx, points, i, len, len, 1, bbox.min, bbox.max, shape.smooth, smoothMonotone, shape.connectNulls);
  322. drawSegment(ctx, stackedOnPoints, i + k - 1, k, len, -1, stackedOnBBox.min, stackedOnBBox.max, shape.stackedOnSmooth, smoothMonotone, shape.connectNulls);
  323. i += k + 1;
  324. ctx.closePath();
  325. }
  326. }
  327. });
  328. exports.Polyline = Polyline;
  329. exports.Polygon = Polygon;