path2d.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /**
  2. * svg路径绘制。
  3. * 作者:tmzdy
  4. * url:https://jx2d.cn
  5. */
  6. function parsePathData(data) {
  7. if (!data) {
  8. return [];
  9. }
  10. var cs = data;
  11. var cc = [
  12. 'm',
  13. 'M',
  14. 'l',
  15. 'L',
  16. 'v',
  17. 'V',
  18. 'h',
  19. 'H',
  20. 'z',
  21. 'Z',
  22. 'c',
  23. 'C',
  24. 'q',
  25. 'Q',
  26. 't',
  27. 'T',
  28. 's',
  29. 'S',
  30. 'a',
  31. 'A',
  32. ];
  33. cs = cs.replace(new RegExp(' ', 'g'), ',');
  34. for (var n = 0; n < cc.length; n++) {
  35. cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
  36. }
  37. var arr = cs.split('|');
  38. var ca = [];
  39. var coords = [];
  40. var cpx = 0;
  41. var cpy = 0;
  42. var re = /([-+]?((\d+\.\d+)|((\d+)|(\.\d+)))(?:e[-+]?\d+)?)/gi;
  43. var match;
  44. for (n = 1; n < arr.length; n++) {
  45. var str = arr[n];
  46. var c = str.charAt(0);
  47. str = str.slice(1);
  48. coords.length = 0;
  49. while ((match = re.exec(str))) {
  50. coords.push(match[0]);
  51. }
  52. var p = [];
  53. for (var j = 0, jlen = coords.length; j < jlen; j++) {
  54. if (coords[j] === '00') {
  55. p.push(0, 0);
  56. continue;
  57. }
  58. var parsed = parseFloat(coords[j]);
  59. if (!isNaN(parsed)) {
  60. p.push(parsed);
  61. } else {
  62. p.push(0);
  63. }
  64. }
  65. while (p.length > 0) {
  66. if (isNaN(p[0])) {
  67. break;
  68. }
  69. var cmd = null;
  70. var points = [];
  71. var startX = cpx,
  72. startY = cpy;
  73. var prevCmd, ctlPtx, ctlPty;
  74. var rx, ry, psi, fa, fs, x1, y1;
  75. switch (c) {
  76. case 'l':
  77. cpx += p.shift();
  78. cpy += p.shift();
  79. cmd = 'L';
  80. points.push(cpx, cpy);
  81. break;
  82. case 'L':
  83. cpx = p.shift();
  84. cpy = p.shift();
  85. points.push(cpx, cpy);
  86. break;
  87. case 'm':
  88. var dx = p.shift();
  89. var dy = p.shift();
  90. cpx += dx;
  91. cpy += dy;
  92. cmd = 'M';
  93. if (ca.length > 2 && ca[ca.length - 1].command === 'z') {
  94. for (var idx = ca.length - 2; idx >= 0; idx--) {
  95. if (ca[idx].command === 'M') {
  96. cpx = ca[idx].points[0] + dx;
  97. cpy = ca[idx].points[1] + dy;
  98. break;
  99. }
  100. }
  101. }
  102. points.push(cpx, cpy);
  103. c = 'l';
  104. break;
  105. case 'M':
  106. cpx = p.shift();
  107. cpy = p.shift();
  108. cmd = 'M';
  109. points.push(cpx, cpy);
  110. c = 'L';
  111. break;
  112. case 'h':
  113. cpx += p.shift();
  114. cmd = 'L';
  115. points.push(cpx, cpy);
  116. break;
  117. case 'H':
  118. cpx = p.shift();
  119. cmd = 'L';
  120. points.push(cpx, cpy);
  121. break;
  122. case 'v':
  123. cpy += p.shift();
  124. cmd = 'L';
  125. points.push(cpx, cpy);
  126. break;
  127. case 'V':
  128. cpy = p.shift();
  129. cmd = 'L';
  130. points.push(cpx, cpy);
  131. break;
  132. case 'C':
  133. points.push(p.shift(), p.shift(), p.shift(), p.shift());
  134. cpx = p.shift();
  135. cpy = p.shift();
  136. points.push(cpx, cpy);
  137. break;
  138. case 'c':
  139. points.push(cpx + p.shift(), cpy + p.shift(), cpx + p.shift(), cpy + p.shift());
  140. cpx += p.shift();
  141. cpy += p.shift();
  142. cmd = 'C';
  143. points.push(cpx, cpy);
  144. break;
  145. case 'S':
  146. ctlPtx = cpx;
  147. ctlPty = cpy;
  148. prevCmd = ca[ca.length - 1];
  149. if (prevCmd.command === 'C') {
  150. ctlPtx = cpx + (cpx - prevCmd.points[2]);
  151. ctlPty = cpy + (cpy - prevCmd.points[3]);
  152. }
  153. points.push(ctlPtx, ctlPty, p.shift(), p.shift());
  154. cpx = p.shift();
  155. cpy = p.shift();
  156. cmd = 'C';
  157. points.push(cpx, cpy);
  158. break;
  159. case 's':
  160. ctlPtx = cpx;
  161. ctlPty = cpy;
  162. prevCmd = ca[ca.length - 1];
  163. if (prevCmd.command === 'C') {
  164. ctlPtx = cpx + (cpx - prevCmd.points[2]);
  165. ctlPty = cpy + (cpy - prevCmd.points[3]);
  166. }
  167. points.push(ctlPtx, ctlPty, cpx + p.shift(), cpy + p.shift());
  168. cpx += p.shift();
  169. cpy += p.shift();
  170. cmd = 'C';
  171. points.push(cpx, cpy);
  172. break;
  173. case 'Q':
  174. points.push(p.shift(), p.shift());
  175. cpx = p.shift();
  176. cpy = p.shift();
  177. points.push(cpx, cpy);
  178. break;
  179. case 'q':
  180. points.push(cpx + p.shift(), cpy + p.shift());
  181. cpx += p.shift();
  182. cpy += p.shift();
  183. cmd = 'Q';
  184. points.push(cpx, cpy);
  185. break;
  186. case 'T':
  187. ctlPtx = cpx;
  188. ctlPty = cpy;
  189. prevCmd = ca[ca.length - 1];
  190. if (prevCmd.command === 'Q') {
  191. ctlPtx = cpx + (cpx - prevCmd.points[0]);
  192. ctlPty = cpy + (cpy - prevCmd.points[1]);
  193. }
  194. cpx = p.shift();
  195. cpy = p.shift();
  196. cmd = 'Q';
  197. points.push(ctlPtx, ctlPty, cpx, cpy);
  198. break;
  199. case 't':
  200. ctlPtx = cpx;
  201. ctlPty = cpy;
  202. prevCmd = ca[ca.length - 1];
  203. if (prevCmd.command === 'Q') {
  204. ctlPtx = cpx + (cpx - prevCmd.points[0]);
  205. ctlPty = cpy + (cpy - prevCmd.points[1]);
  206. }
  207. cpx += p.shift();
  208. cpy += p.shift();
  209. cmd = 'Q';
  210. points.push(ctlPtx, ctlPty, cpx, cpy);
  211. break;
  212. case 'A':
  213. rx = p.shift();
  214. ry = p.shift();
  215. psi = p.shift();
  216. fa = p.shift();
  217. fs = p.shift();
  218. x1 = cpx;
  219. y1 = cpy;
  220. cpx = p.shift();
  221. cpy = p.shift();
  222. cmd = 'A';
  223. points = convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
  224. break;
  225. case 'a':
  226. rx = p.shift();
  227. ry = p.shift();
  228. psi = p.shift();
  229. fa = p.shift();
  230. fs = p.shift();
  231. x1 = cpx;
  232. y1 = cpy;
  233. cpx += p.shift();
  234. cpy += p.shift();
  235. cmd = 'A';
  236. points = convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
  237. break;
  238. }
  239. ca.push({
  240. command: cmd || c,
  241. points: points,
  242. start: {
  243. x: startX,
  244. y: startY,
  245. },
  246. pathLength: calcLength(startX, startY, cmd || c, points),
  247. });
  248. }
  249. if (c === 'z' || c === 'Z') {
  250. ca.push({
  251. command: 'z',
  252. points: [],
  253. start: undefined,
  254. pathLength: 0,
  255. });
  256. }
  257. }
  258. return ca;
  259. }
  260. function calcLength(x, y, cmd, points) {
  261. var len, p1, p2, t;
  262. switch (cmd) {
  263. case 'L':
  264. return getLineLength(x, y, points[0], points[1]);
  265. case 'C':
  266. len = 0.0;
  267. p1 = getPointOnCubicBezier(0, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
  268. for (t = 0.01; t <= 1; t += 0.01) {
  269. p2 = getPointOnCubicBezier(t, x, y, points[0], points[1], points[2], points[3], points[4], points[
  270. 5]);
  271. len += getLineLength(p1.x, p1.y, p2.x, p2.y);
  272. p1 = p2;
  273. }
  274. return len;
  275. case 'Q':
  276. len = 0.0;
  277. p1 = getPointOnQuadraticBezier(0, x, y, points[0], points[1], points[2], points[3]);
  278. for (t = 0.01; t <= 1; t += 0.01) {
  279. p2 = getPointOnQuadraticBezier(t, x, y, points[0], points[1], points[2], points[3]);
  280. len += getLineLength(p1.x, p1.y, p2.x, p2.y);
  281. p1 = p2;
  282. }
  283. return len;
  284. case 'A':
  285. len = 0.0;
  286. var start = points[4];
  287. var dTheta = points[5];
  288. var end = points[4] + dTheta;
  289. var inc = Math.PI / 180.0;
  290. if (Math.abs(start - end) < inc) {
  291. inc = Math.abs(start - end);
  292. }
  293. p1 = getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
  294. if (dTheta < 0) {
  295. for (t = start - inc; t > end; t -= inc) {
  296. p2 = getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
  297. len += getLineLength(p1.x, p1.y, p2.x, p2.y);
  298. p1 = p2;
  299. }
  300. } else {
  301. for (t = start + inc; t < end; t += inc) {
  302. p2 = getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
  303. len += getLineLength(p1.x, p1.y, p2.x, p2.y);
  304. p1 = p2;
  305. }
  306. }
  307. p2 = getPointOnEllipticalArc(points[0], points[1], points[2], points[3], end, 0);
  308. len += getLineLength(p1.x, p1.y, p2.x, p2.y);
  309. return len;
  310. }
  311. return 0;
  312. }
  313. function convertEndpointToCenterParameterization(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
  314. var psi = psiDeg * (Math.PI / 180.0);
  315. var xp = (Math.cos(psi) * (x1 - x2)) / 2.0 + (Math.sin(psi) * (y1 - y2)) / 2.0;
  316. var yp = (-1 * Math.sin(psi) * (x1 - x2)) / 2.0 +
  317. (Math.cos(psi) * (y1 - y2)) / 2.0;
  318. var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
  319. if (lambda > 1) {
  320. rx *= Math.sqrt(lambda);
  321. ry *= Math.sqrt(lambda);
  322. }
  323. var f = Math.sqrt((rx * rx * (ry * ry) - rx * rx * (yp * yp) - ry * ry * (xp * xp)) /
  324. (rx * rx * (yp * yp) + ry * ry * (xp * xp)));
  325. if (fa === fs) {
  326. f *= -1;
  327. }
  328. if (isNaN(f)) {
  329. f = 0;
  330. }
  331. var cxp = (f * rx * yp) / ry;
  332. var cyp = (f * -ry * xp) / rx;
  333. var cx = (x1 + x2) / 2.0 + Math.cos(psi) * cxp - Math.sin(psi) * cyp;
  334. var cy = (y1 + y2) / 2.0 + Math.sin(psi) * cxp + Math.cos(psi) * cyp;
  335. var vMag = function(v) {
  336. return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
  337. };
  338. var vRatio = function(u, v) {
  339. return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
  340. };
  341. var vAngle = function(u, v) {
  342. return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
  343. };
  344. var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
  345. var u = [(xp - cxp) / rx, (yp - cyp) / ry];
  346. var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
  347. var dTheta = vAngle(u, v);
  348. if (vRatio(u, v) <= -1) {
  349. dTheta = Math.PI;
  350. }
  351. if (vRatio(u, v) >= 1) {
  352. dTheta = 0;
  353. }
  354. if (fs === 0 && dTheta > 0) {
  355. dTheta = dTheta - 2 * Math.PI;
  356. }
  357. if (fs === 1 && dTheta < 0) {
  358. dTheta = dTheta + 2 * Math.PI;
  359. }
  360. return [cx, cy, rx, ry, theta, dTheta, psi, fs];
  361. }
  362. function getSelfRect() {
  363. var points = [];
  364. this.dataArray.forEach(function(data) {
  365. if (data.command === 'A') {
  366. var start = data.points[4];
  367. var dTheta = data.points[5];
  368. var end = data.points[4] + dTheta;
  369. var inc = Math.PI / 180.0;
  370. if (Math.abs(start - end) < inc) {
  371. inc = Math.abs(start - end);
  372. }
  373. if (dTheta < 0) {
  374. for (let t = start - inc; t > end; t -= inc) {
  375. const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2],
  376. data.points[3], t, 0);
  377. points.push(point.x, point.y);
  378. }
  379. } else {
  380. for (let t = start + inc; t < end; t += inc) {
  381. const point = Path.getPointOnEllipticalArc(data.points[0], data.points[1], data.points[2],
  382. data.points[3], t, 0);
  383. points.push(point.x, point.y);
  384. }
  385. }
  386. } else if (data.command === 'C') {
  387. for (let t = 0.0; t <= 1; t += 0.01) {
  388. const point = Path.getPointOnCubicBezier(t, data.start.x, data.start.y, data.points[0], data
  389. .points[1], data.points[2], data.points[3], data.points[4], data.points[5]);
  390. points.push(point.x, point.y);
  391. }
  392. } else {
  393. points = points.concat(data.points);
  394. }
  395. });
  396. var minX = points[0];
  397. var maxX = points[0];
  398. var minY = points[1];
  399. var maxY = points[1];
  400. var x, y;
  401. for (var i = 0; i < points.length / 2; i++) {
  402. x = points[i * 2];
  403. y = points[i * 2 + 1];
  404. if (!isNaN(x)) {
  405. minX = Math.min(minX, x);
  406. maxX = Math.max(maxX, x);
  407. }
  408. if (!isNaN(y)) {
  409. minY = Math.min(minY, y);
  410. maxY = Math.max(maxY, y);
  411. }
  412. }
  413. return {
  414. x: Math.round(minX),
  415. y: Math.round(minY),
  416. width: Math.round(maxX - minX),
  417. height: Math.round(maxY - minY),
  418. };
  419. }
  420. function getPointAtLength(length) {
  421. var point, i = 0,
  422. ii = this.dataArray.length;
  423. if (!ii) {
  424. return null;
  425. }
  426. while (i < ii && length > this.dataArray[i].pathLength) {
  427. length -= this.dataArray[i].pathLength;
  428. ++i;
  429. }
  430. if (i === ii) {
  431. point = this.dataArray[i - 1].points.slice(-2);
  432. return {
  433. x: point[0],
  434. y: point[1],
  435. };
  436. }
  437. if (length < 0.01) {
  438. point = this.dataArray[i].points.slice(0, 2);
  439. return {
  440. x: point[0],
  441. y: point[1],
  442. };
  443. }
  444. var cp = this.dataArray[i];
  445. var p = cp.points;
  446. switch (cp.command) {
  447. case 'L':
  448. return Path.getPointOnLine(length, cp.start.x, cp.start.y, p[0], p[1]);
  449. case 'C':
  450. return Path.getPointOnCubicBezier(length / cp.pathLength, cp.start.x, cp.start.y, p[0], p[1], p[2], p[3], p[
  451. 4], p[5]);
  452. case 'Q':
  453. return Path.getPointOnQuadraticBezier(length / cp.pathLength, cp.start.x, cp.start.y, p[0], p[1], p[2], p[
  454. 3]);
  455. case 'A':
  456. var cx = p[0],
  457. cy = p[1],
  458. rx = p[2],
  459. ry = p[3],
  460. theta = p[4],
  461. dTheta = p[5],
  462. psi = p[6];
  463. theta += (dTheta * length) / cp.pathLength;
  464. return Path.getPointOnEllipticalArc(cx, cy, rx, ry, theta, psi);
  465. }
  466. return null;
  467. }
  468. function getLineLength(x1, y1, x2, y2) {
  469. return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  470. }
  471. function getPointOnLine(dist, P1x, P1y, P2x, P2y, fromX, fromY) {
  472. if (fromX === undefined) {
  473. fromX = P1x;
  474. }
  475. if (fromY === undefined) {
  476. fromY = P1y;
  477. }
  478. var m = (P2y - P1y) / (P2x - P1x + 0.00000001);
  479. var run = Math.sqrt((dist * dist) / (1 + m * m));
  480. if (P2x < P1x) {
  481. run *= -1;
  482. }
  483. var rise = m * run;
  484. var pt;
  485. if (P2x === P1x) {
  486. pt = {
  487. x: fromX,
  488. y: fromY + rise,
  489. };
  490. } else if ((fromY - P1y) / (fromX - P1x + 0.00000001) === m) {
  491. pt = {
  492. x: fromX + run,
  493. y: fromY + rise,
  494. };
  495. } else {
  496. var ix, iy;
  497. var len = this.getLineLength(P1x, P1y, P2x, P2y);
  498. var u = (fromX - P1x) * (P2x - P1x) + (fromY - P1y) * (P2y - P1y);
  499. u = u / (len * len);
  500. ix = P1x + u * (P2x - P1x);
  501. iy = P1y + u * (P2y - P1y);
  502. var pRise = this.getLineLength(fromX, fromY, ix, iy);
  503. var pRun = Math.sqrt(dist * dist - pRise * pRise);
  504. run = Math.sqrt((pRun * pRun) / (1 + m * m));
  505. if (P2x < P1x) {
  506. run *= -1;
  507. }
  508. rise = m * run;
  509. pt = {
  510. x: ix + run,
  511. y: iy + rise,
  512. };
  513. }
  514. return pt;
  515. }
  516. function getPointOnCubicBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) {
  517. function CB1(t) {
  518. return t * t * t;
  519. }
  520. function CB2(t) {
  521. return 3 * t * t * (1 - t);
  522. }
  523. function CB3(t) {
  524. return 3 * t * (1 - t) * (1 - t);
  525. }
  526. function CB4(t) {
  527. return (1 - t) * (1 - t) * (1 - t);
  528. }
  529. var x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct);
  530. var y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct);
  531. return {
  532. x: x,
  533. y: y,
  534. };
  535. }
  536. function getPointOnQuadraticBezier(pct, P1x, P1y, P2x, P2y, P3x, P3y) {
  537. function QB1(t) {
  538. return t * t;
  539. }
  540. function QB2(t) {
  541. return 2 * t * (1 - t);
  542. }
  543. function QB3(t) {
  544. return (1 - t) * (1 - t);
  545. }
  546. var x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct);
  547. var y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct);
  548. return {
  549. x: x,
  550. y: y,
  551. };
  552. }
  553. function getPointOnEllipticalArc(cx, cy, rx, ry, theta, psi) {
  554. var cosPsi = Math.cos(psi),
  555. sinPsi = Math.sin(psi);
  556. var pt = {
  557. x: rx * Math.cos(theta),
  558. y: ry * Math.sin(theta),
  559. };
  560. return {
  561. x: cx + (pt.x * cosPsi - pt.y * sinPsi),
  562. y: cy + (pt.x * sinPsi + pt.y * cosPsi),
  563. };
  564. }
  565. let path2d = function(render, config = {}) {
  566. const [w, h] = render.area;
  567. let cfg = {
  568. name: 'path',
  569. animationCurve: 'easeOutBack',
  570. hover: true,
  571. drag: true,
  572. shape: {
  573. path: '',
  574. close:true,
  575. points:[]
  576. },
  577. style: {
  578. stroke: '#000',
  579. fill:'#000',
  580. lineWidth: 1,
  581. hoverCursor: 'pointer',
  582. },
  583. ...config,
  584. draw({ ctx }, { shape, style: { lineWidth } }){
  585. let { points, close,x,y ,path} = shape
  586. var ca=[]
  587. if(this.shape['points'].length>0&&this.shape['points']){
  588. ca = this.shape['svg']
  589. }else{
  590. ca = parsePathData(path);
  591. // ca = ca.map(el=>{
  592. // if(el.points.length){
  593. // return {...el,points:[el.points[0]+x,el.points[1]+y]}
  594. // }
  595. // return el
  596. // })
  597. let ar = ca.map(el=> el.points)
  598. this.shape['points'] = ar.filter((el)=>el.length==2)
  599. this.shape['svg'] = ca;
  600. }
  601. const context=ctx
  602. context.beginPath();
  603. for (var n = 0; n < ca.length; n++) {
  604. var c = ca[n].command;
  605. var p = ca[n].points;
  606. switch (c) {
  607. case 'L':
  608. context.lineTo(p[0], p[1]);
  609. break;
  610. case 'M':
  611. context.moveTo(p[0], p[1]);
  612. break;
  613. case 'C':
  614. context.bezierCurveTo(p[0], p[1], p[2], p[3], p[4], p[5]);
  615. break;
  616. case 'Q':
  617. context.quadraticCurveTo(p[0], p[1], p[2], p[3]);
  618. break;
  619. case 'A':
  620. var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6], fs = p[7];
  621. var r = rx > ry ? rx : ry;
  622. var scaleX = rx > ry ? 1 : rx / ry;
  623. var scaleY = rx > ry ? ry / rx : 1;
  624. context.translate(cx, cy);
  625. context.rotate(psi);
  626. context.scale(scaleX, scaleY);
  627. context.arc(0, 0, r, theta, theta + dTheta, 1 - fs);
  628. context.scale(1 / scaleX, 1 / scaleY);
  629. context.rotate(-psi);
  630. context.translate(-cx, -cy);
  631. break;
  632. case 'z':
  633. close = true;
  634. context.closePath();
  635. break;
  636. }
  637. }
  638. if (close) {
  639. ctx.closePath()
  640. ctx.fill()
  641. ctx.stroke()
  642. } else {
  643. ctx.stroke()
  644. }
  645. },
  646. };
  647. return cfg;
  648. }
  649. export default path2d;