PathProxy.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. import * as vec2 from './vector.js';
  2. import BoundingRect from './BoundingRect.js';
  3. import { devicePixelRatio as dpr } from '../config.js';
  4. import { fromLine, fromCubic, fromQuadratic, fromArc } from './bbox.js';
  5. import { cubicLength, cubicSubdivide, quadraticLength, quadraticSubdivide } from './curve.js';
  6. var CMD = {
  7. M: 1,
  8. L: 2,
  9. C: 3,
  10. Q: 4,
  11. A: 5,
  12. Z: 6,
  13. R: 7
  14. };
  15. var tmpOutX = [];
  16. var tmpOutY = [];
  17. var min = [];
  18. var max = [];
  19. var min2 = [];
  20. var max2 = [];
  21. var mathMin = Math.min;
  22. var mathMax = Math.max;
  23. var mathCos = Math.cos;
  24. var mathSin = Math.sin;
  25. var mathAbs = Math.abs;
  26. var PI = Math.PI;
  27. var PI2 = PI * 2;
  28. var hasTypedArray = typeof Float32Array !== 'undefined';
  29. var tmpAngles = [];
  30. function modPI2(radian) {
  31. var n = Math.round(radian / PI * 1e8) / 1e8;
  32. return (n % 2) * PI;
  33. }
  34. export function normalizeArcAngles(angles, anticlockwise) {
  35. var newStartAngle = modPI2(angles[0]);
  36. if (newStartAngle < 0) {
  37. newStartAngle += PI2;
  38. }
  39. var delta = newStartAngle - angles[0];
  40. var newEndAngle = angles[1];
  41. newEndAngle += delta;
  42. if (!anticlockwise && newEndAngle - newStartAngle >= PI2) {
  43. newEndAngle = newStartAngle + PI2;
  44. }
  45. else if (anticlockwise && newStartAngle - newEndAngle >= PI2) {
  46. newEndAngle = newStartAngle - PI2;
  47. }
  48. else if (!anticlockwise && newStartAngle > newEndAngle) {
  49. newEndAngle = newStartAngle + (PI2 - modPI2(newStartAngle - newEndAngle));
  50. }
  51. else if (anticlockwise && newStartAngle < newEndAngle) {
  52. newEndAngle = newStartAngle - (PI2 - modPI2(newEndAngle - newStartAngle));
  53. }
  54. angles[0] = newStartAngle;
  55. angles[1] = newEndAngle;
  56. }
  57. var PathProxy = (function () {
  58. function PathProxy(notSaveData) {
  59. this.dpr = 1;
  60. this._xi = 0;
  61. this._yi = 0;
  62. this._x0 = 0;
  63. this._y0 = 0;
  64. this._len = 0;
  65. if (notSaveData) {
  66. this._saveData = false;
  67. }
  68. if (this._saveData) {
  69. this.data = [];
  70. }
  71. }
  72. PathProxy.prototype.increaseVersion = function () {
  73. this._version++;
  74. };
  75. PathProxy.prototype.getVersion = function () {
  76. return this._version;
  77. };
  78. PathProxy.prototype.setScale = function (sx, sy, segmentIgnoreThreshold) {
  79. segmentIgnoreThreshold = segmentIgnoreThreshold || 0;
  80. if (segmentIgnoreThreshold > 0) {
  81. this._ux = mathAbs(segmentIgnoreThreshold / dpr / sx) || 0;
  82. this._uy = mathAbs(segmentIgnoreThreshold / dpr / sy) || 0;
  83. }
  84. };
  85. PathProxy.prototype.setDPR = function (dpr) {
  86. this.dpr = dpr;
  87. };
  88. PathProxy.prototype.setContext = function (ctx) {
  89. this._ctx = ctx;
  90. };
  91. PathProxy.prototype.getContext = function () {
  92. return this._ctx;
  93. };
  94. PathProxy.prototype.beginPath = function () {
  95. this._ctx && this._ctx.beginPath();
  96. this.reset();
  97. return this;
  98. };
  99. PathProxy.prototype.reset = function () {
  100. if (this._saveData) {
  101. this._len = 0;
  102. }
  103. if (this._pathSegLen) {
  104. this._pathSegLen = null;
  105. this._pathLen = 0;
  106. }
  107. this._version++;
  108. };
  109. PathProxy.prototype.moveTo = function (x, y) {
  110. this._drawPendingPt();
  111. this.addData(CMD.M, x, y);
  112. this._ctx && this._ctx.moveTo(x, y);
  113. this._x0 = x;
  114. this._y0 = y;
  115. this._xi = x;
  116. this._yi = y;
  117. return this;
  118. };
  119. PathProxy.prototype.lineTo = function (x, y) {
  120. var dx = mathAbs(x - this._xi);
  121. var dy = mathAbs(y - this._yi);
  122. var exceedUnit = dx > this._ux || dy > this._uy;
  123. this.addData(CMD.L, x, y);
  124. if (this._ctx && exceedUnit) {
  125. this._ctx.lineTo(x, y);
  126. }
  127. if (exceedUnit) {
  128. this._xi = x;
  129. this._yi = y;
  130. this._pendingPtDist = 0;
  131. }
  132. else {
  133. var d2 = dx * dx + dy * dy;
  134. if (d2 > this._pendingPtDist) {
  135. this._pendingPtX = x;
  136. this._pendingPtY = y;
  137. this._pendingPtDist = d2;
  138. }
  139. }
  140. return this;
  141. };
  142. PathProxy.prototype.bezierCurveTo = function (x1, y1, x2, y2, x3, y3) {
  143. this._drawPendingPt();
  144. this.addData(CMD.C, x1, y1, x2, y2, x3, y3);
  145. if (this._ctx) {
  146. this._ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
  147. }
  148. this._xi = x3;
  149. this._yi = y3;
  150. return this;
  151. };
  152. PathProxy.prototype.quadraticCurveTo = function (x1, y1, x2, y2) {
  153. this._drawPendingPt();
  154. this.addData(CMD.Q, x1, y1, x2, y2);
  155. if (this._ctx) {
  156. this._ctx.quadraticCurveTo(x1, y1, x2, y2);
  157. }
  158. this._xi = x2;
  159. this._yi = y2;
  160. return this;
  161. };
  162. PathProxy.prototype.arc = function (cx, cy, r, startAngle, endAngle, anticlockwise) {
  163. this._drawPendingPt();
  164. tmpAngles[0] = startAngle;
  165. tmpAngles[1] = endAngle;
  166. normalizeArcAngles(tmpAngles, anticlockwise);
  167. startAngle = tmpAngles[0];
  168. endAngle = tmpAngles[1];
  169. var delta = endAngle - startAngle;
  170. this.addData(CMD.A, cx, cy, r, r, startAngle, delta, 0, anticlockwise ? 0 : 1);
  171. this._ctx && this._ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
  172. this._xi = mathCos(endAngle) * r + cx;
  173. this._yi = mathSin(endAngle) * r + cy;
  174. return this;
  175. };
  176. PathProxy.prototype.arcTo = function (x1, y1, x2, y2, radius) {
  177. this._drawPendingPt();
  178. if (this._ctx) {
  179. this._ctx.arcTo(x1, y1, x2, y2, radius);
  180. }
  181. return this;
  182. };
  183. PathProxy.prototype.rect = function (x, y, w, h) {
  184. this._drawPendingPt();
  185. this._ctx && this._ctx.rect(x, y, w, h);
  186. this.addData(CMD.R, x, y, w, h);
  187. return this;
  188. };
  189. PathProxy.prototype.closePath = function () {
  190. this._drawPendingPt();
  191. this.addData(CMD.Z);
  192. var ctx = this._ctx;
  193. var x0 = this._x0;
  194. var y0 = this._y0;
  195. if (ctx) {
  196. ctx.closePath();
  197. }
  198. this._xi = x0;
  199. this._yi = y0;
  200. return this;
  201. };
  202. PathProxy.prototype.fill = function (ctx) {
  203. ctx && ctx.fill();
  204. this.toStatic();
  205. };
  206. PathProxy.prototype.stroke = function (ctx) {
  207. ctx && ctx.stroke();
  208. this.toStatic();
  209. };
  210. PathProxy.prototype.len = function () {
  211. return this._len;
  212. };
  213. PathProxy.prototype.setData = function (data) {
  214. var len = data.length;
  215. if (!(this.data && this.data.length === len) && hasTypedArray) {
  216. this.data = new Float32Array(len);
  217. }
  218. for (var i = 0; i < len; i++) {
  219. this.data[i] = data[i];
  220. }
  221. this._len = len;
  222. };
  223. PathProxy.prototype.appendPath = function (path) {
  224. if (!(path instanceof Array)) {
  225. path = [path];
  226. }
  227. var len = path.length;
  228. var appendSize = 0;
  229. var offset = this._len;
  230. for (var i = 0; i < len; i++) {
  231. appendSize += path[i].len();
  232. }
  233. if (hasTypedArray && (this.data instanceof Float32Array)) {
  234. this.data = new Float32Array(offset + appendSize);
  235. }
  236. for (var i = 0; i < len; i++) {
  237. var appendPathData = path[i].data;
  238. for (var k = 0; k < appendPathData.length; k++) {
  239. this.data[offset++] = appendPathData[k];
  240. }
  241. }
  242. this._len = offset;
  243. };
  244. PathProxy.prototype.addData = function (cmd, a, b, c, d, e, f, g, h) {
  245. if (!this._saveData) {
  246. return;
  247. }
  248. var data = this.data;
  249. if (this._len + arguments.length > data.length) {
  250. this._expandData();
  251. data = this.data;
  252. }
  253. for (var i = 0; i < arguments.length; i++) {
  254. data[this._len++] = arguments[i];
  255. }
  256. };
  257. PathProxy.prototype._drawPendingPt = function () {
  258. if (this._pendingPtDist > 0) {
  259. this._ctx && this._ctx.lineTo(this._pendingPtX, this._pendingPtY);
  260. this._pendingPtDist = 0;
  261. }
  262. };
  263. PathProxy.prototype._expandData = function () {
  264. if (!(this.data instanceof Array)) {
  265. var newData = [];
  266. for (var i = 0; i < this._len; i++) {
  267. newData[i] = this.data[i];
  268. }
  269. this.data = newData;
  270. }
  271. };
  272. PathProxy.prototype.toStatic = function () {
  273. if (!this._saveData) {
  274. return;
  275. }
  276. this._drawPendingPt();
  277. var data = this.data;
  278. if (data instanceof Array) {
  279. data.length = this._len;
  280. if (hasTypedArray && this._len > 11) {
  281. this.data = new Float32Array(data);
  282. }
  283. }
  284. };
  285. PathProxy.prototype.getBoundingRect = function () {
  286. min[0] = min[1] = min2[0] = min2[1] = Number.MAX_VALUE;
  287. max[0] = max[1] = max2[0] = max2[1] = -Number.MAX_VALUE;
  288. var data = this.data;
  289. var xi = 0;
  290. var yi = 0;
  291. var x0 = 0;
  292. var y0 = 0;
  293. var i;
  294. for (i = 0; i < this._len;) {
  295. var cmd = data[i++];
  296. var isFirst = i === 1;
  297. if (isFirst) {
  298. xi = data[i];
  299. yi = data[i + 1];
  300. x0 = xi;
  301. y0 = yi;
  302. }
  303. switch (cmd) {
  304. case CMD.M:
  305. xi = x0 = data[i++];
  306. yi = y0 = data[i++];
  307. min2[0] = x0;
  308. min2[1] = y0;
  309. max2[0] = x0;
  310. max2[1] = y0;
  311. break;
  312. case CMD.L:
  313. fromLine(xi, yi, data[i], data[i + 1], min2, max2);
  314. xi = data[i++];
  315. yi = data[i++];
  316. break;
  317. case CMD.C:
  318. fromCubic(xi, yi, data[i++], data[i++], data[i++], data[i++], data[i], data[i + 1], min2, max2);
  319. xi = data[i++];
  320. yi = data[i++];
  321. break;
  322. case CMD.Q:
  323. fromQuadratic(xi, yi, data[i++], data[i++], data[i], data[i + 1], min2, max2);
  324. xi = data[i++];
  325. yi = data[i++];
  326. break;
  327. case CMD.A:
  328. var cx = data[i++];
  329. var cy = data[i++];
  330. var rx = data[i++];
  331. var ry = data[i++];
  332. var startAngle = data[i++];
  333. var endAngle = data[i++] + startAngle;
  334. i += 1;
  335. var anticlockwise = !data[i++];
  336. if (isFirst) {
  337. x0 = mathCos(startAngle) * rx + cx;
  338. y0 = mathSin(startAngle) * ry + cy;
  339. }
  340. fromArc(cx, cy, rx, ry, startAngle, endAngle, anticlockwise, min2, max2);
  341. xi = mathCos(endAngle) * rx + cx;
  342. yi = mathSin(endAngle) * ry + cy;
  343. break;
  344. case CMD.R:
  345. x0 = xi = data[i++];
  346. y0 = yi = data[i++];
  347. var width = data[i++];
  348. var height = data[i++];
  349. fromLine(x0, y0, x0 + width, y0 + height, min2, max2);
  350. break;
  351. case CMD.Z:
  352. xi = x0;
  353. yi = y0;
  354. break;
  355. }
  356. vec2.min(min, min, min2);
  357. vec2.max(max, max, max2);
  358. }
  359. if (i === 0) {
  360. min[0] = min[1] = max[0] = max[1] = 0;
  361. }
  362. return new BoundingRect(min[0], min[1], max[0] - min[0], max[1] - min[1]);
  363. };
  364. PathProxy.prototype._calculateLength = function () {
  365. var data = this.data;
  366. var len = this._len;
  367. var ux = this._ux;
  368. var uy = this._uy;
  369. var xi = 0;
  370. var yi = 0;
  371. var x0 = 0;
  372. var y0 = 0;
  373. if (!this._pathSegLen) {
  374. this._pathSegLen = [];
  375. }
  376. var pathSegLen = this._pathSegLen;
  377. var pathTotalLen = 0;
  378. var segCount = 0;
  379. for (var i = 0; i < len;) {
  380. var cmd = data[i++];
  381. var isFirst = i === 1;
  382. if (isFirst) {
  383. xi = data[i];
  384. yi = data[i + 1];
  385. x0 = xi;
  386. y0 = yi;
  387. }
  388. var l = -1;
  389. switch (cmd) {
  390. case CMD.M:
  391. xi = x0 = data[i++];
  392. yi = y0 = data[i++];
  393. break;
  394. case CMD.L: {
  395. var x2 = data[i++];
  396. var y2 = data[i++];
  397. var dx = x2 - xi;
  398. var dy = y2 - yi;
  399. if (mathAbs(dx) > ux || mathAbs(dy) > uy || i === len - 1) {
  400. l = Math.sqrt(dx * dx + dy * dy);
  401. xi = x2;
  402. yi = y2;
  403. }
  404. break;
  405. }
  406. case CMD.C: {
  407. var x1 = data[i++];
  408. var y1 = data[i++];
  409. var x2 = data[i++];
  410. var y2 = data[i++];
  411. var x3 = data[i++];
  412. var y3 = data[i++];
  413. l = cubicLength(xi, yi, x1, y1, x2, y2, x3, y3, 10);
  414. xi = x3;
  415. yi = y3;
  416. break;
  417. }
  418. case CMD.Q: {
  419. var x1 = data[i++];
  420. var y1 = data[i++];
  421. var x2 = data[i++];
  422. var y2 = data[i++];
  423. l = quadraticLength(xi, yi, x1, y1, x2, y2, 10);
  424. xi = x2;
  425. yi = y2;
  426. break;
  427. }
  428. case CMD.A:
  429. var cx = data[i++];
  430. var cy = data[i++];
  431. var rx = data[i++];
  432. var ry = data[i++];
  433. var startAngle = data[i++];
  434. var delta = data[i++];
  435. var endAngle = delta + startAngle;
  436. i += 1;
  437. var anticlockwise = !data[i++];
  438. if (isFirst) {
  439. x0 = mathCos(startAngle) * rx + cx;
  440. y0 = mathSin(startAngle) * ry + cy;
  441. }
  442. l = mathMax(rx, ry) * mathMin(PI2, Math.abs(delta));
  443. xi = mathCos(endAngle) * rx + cx;
  444. yi = mathSin(endAngle) * ry + cy;
  445. break;
  446. case CMD.R: {
  447. x0 = xi = data[i++];
  448. y0 = yi = data[i++];
  449. var width = data[i++];
  450. var height = data[i++];
  451. l = width * 2 + height * 2;
  452. break;
  453. }
  454. case CMD.Z: {
  455. var dx = x0 - xi;
  456. var dy = y0 - yi;
  457. l = Math.sqrt(dx * dx + dy * dy);
  458. xi = x0;
  459. yi = y0;
  460. break;
  461. }
  462. }
  463. if (l >= 0) {
  464. pathSegLen[segCount++] = l;
  465. pathTotalLen += l;
  466. }
  467. }
  468. this._pathLen = pathTotalLen;
  469. return pathTotalLen;
  470. };
  471. PathProxy.prototype.rebuildPath = function (ctx, percent) {
  472. var d = this.data;
  473. var ux = this._ux;
  474. var uy = this._uy;
  475. var len = this._len;
  476. var x0;
  477. var y0;
  478. var xi;
  479. var yi;
  480. var x;
  481. var y;
  482. var drawPart = percent < 1;
  483. var pathSegLen;
  484. var pathTotalLen;
  485. var accumLength = 0;
  486. var segCount = 0;
  487. var displayedLength;
  488. var pendingPtDist = 0;
  489. var pendingPtX;
  490. var pendingPtY;
  491. if (drawPart) {
  492. if (!this._pathSegLen) {
  493. this._calculateLength();
  494. }
  495. pathSegLen = this._pathSegLen;
  496. pathTotalLen = this._pathLen;
  497. displayedLength = percent * pathTotalLen;
  498. if (!displayedLength) {
  499. return;
  500. }
  501. }
  502. lo: for (var i = 0; i < len;) {
  503. var cmd = d[i++];
  504. var isFirst = i === 1;
  505. if (isFirst) {
  506. xi = d[i];
  507. yi = d[i + 1];
  508. x0 = xi;
  509. y0 = yi;
  510. }
  511. if (cmd !== CMD.L && pendingPtDist > 0) {
  512. ctx.lineTo(pendingPtX, pendingPtY);
  513. pendingPtDist = 0;
  514. }
  515. switch (cmd) {
  516. case CMD.M:
  517. x0 = xi = d[i++];
  518. y0 = yi = d[i++];
  519. ctx.moveTo(xi, yi);
  520. break;
  521. case CMD.L: {
  522. x = d[i++];
  523. y = d[i++];
  524. var dx = mathAbs(x - xi);
  525. var dy = mathAbs(y - yi);
  526. if (dx > ux || dy > uy) {
  527. if (drawPart) {
  528. var l = pathSegLen[segCount++];
  529. if (accumLength + l > displayedLength) {
  530. var t = (displayedLength - accumLength) / l;
  531. ctx.lineTo(xi * (1 - t) + x * t, yi * (1 - t) + y * t);
  532. break lo;
  533. }
  534. accumLength += l;
  535. }
  536. ctx.lineTo(x, y);
  537. xi = x;
  538. yi = y;
  539. pendingPtDist = 0;
  540. }
  541. else {
  542. var d2 = dx * dx + dy * dy;
  543. if (d2 > pendingPtDist) {
  544. pendingPtX = x;
  545. pendingPtY = y;
  546. pendingPtDist = d2;
  547. }
  548. }
  549. break;
  550. }
  551. case CMD.C: {
  552. var x1 = d[i++];
  553. var y1 = d[i++];
  554. var x2 = d[i++];
  555. var y2 = d[i++];
  556. var x3 = d[i++];
  557. var y3 = d[i++];
  558. if (drawPart) {
  559. var l = pathSegLen[segCount++];
  560. if (accumLength + l > displayedLength) {
  561. var t = (displayedLength - accumLength) / l;
  562. cubicSubdivide(xi, x1, x2, x3, t, tmpOutX);
  563. cubicSubdivide(yi, y1, y2, y3, t, tmpOutY);
  564. ctx.bezierCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2], tmpOutX[3], tmpOutY[3]);
  565. break lo;
  566. }
  567. accumLength += l;
  568. }
  569. ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
  570. xi = x3;
  571. yi = y3;
  572. break;
  573. }
  574. case CMD.Q: {
  575. var x1 = d[i++];
  576. var y1 = d[i++];
  577. var x2 = d[i++];
  578. var y2 = d[i++];
  579. if (drawPart) {
  580. var l = pathSegLen[segCount++];
  581. if (accumLength + l > displayedLength) {
  582. var t = (displayedLength - accumLength) / l;
  583. quadraticSubdivide(xi, x1, x2, t, tmpOutX);
  584. quadraticSubdivide(yi, y1, y2, t, tmpOutY);
  585. ctx.quadraticCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2]);
  586. break lo;
  587. }
  588. accumLength += l;
  589. }
  590. ctx.quadraticCurveTo(x1, y1, x2, y2);
  591. xi = x2;
  592. yi = y2;
  593. break;
  594. }
  595. case CMD.A:
  596. var cx = d[i++];
  597. var cy = d[i++];
  598. var rx = d[i++];
  599. var ry = d[i++];
  600. var startAngle = d[i++];
  601. var delta = d[i++];
  602. var psi = d[i++];
  603. var anticlockwise = !d[i++];
  604. var r = (rx > ry) ? rx : ry;
  605. var isEllipse = mathAbs(rx - ry) > 1e-3;
  606. var endAngle = startAngle + delta;
  607. var breakBuild = false;
  608. if (drawPart) {
  609. var l = pathSegLen[segCount++];
  610. if (accumLength + l > displayedLength) {
  611. endAngle = startAngle + delta * (displayedLength - accumLength) / l;
  612. breakBuild = true;
  613. }
  614. accumLength += l;
  615. }
  616. if (isEllipse && ctx.ellipse) {
  617. ctx.ellipse(cx, cy, rx, ry, psi, startAngle, endAngle, anticlockwise);
  618. }
  619. else {
  620. ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
  621. }
  622. if (breakBuild) {
  623. break lo;
  624. }
  625. if (isFirst) {
  626. x0 = mathCos(startAngle) * rx + cx;
  627. y0 = mathSin(startAngle) * ry + cy;
  628. }
  629. xi = mathCos(endAngle) * rx + cx;
  630. yi = mathSin(endAngle) * ry + cy;
  631. break;
  632. case CMD.R:
  633. x0 = xi = d[i];
  634. y0 = yi = d[i + 1];
  635. x = d[i++];
  636. y = d[i++];
  637. var width = d[i++];
  638. var height = d[i++];
  639. if (drawPart) {
  640. var l = pathSegLen[segCount++];
  641. if (accumLength + l > displayedLength) {
  642. var d_1 = displayedLength - accumLength;
  643. ctx.moveTo(x, y);
  644. ctx.lineTo(x + mathMin(d_1, width), y);
  645. d_1 -= width;
  646. if (d_1 > 0) {
  647. ctx.lineTo(x + width, y + mathMin(d_1, height));
  648. }
  649. d_1 -= height;
  650. if (d_1 > 0) {
  651. ctx.lineTo(x + mathMax(width - d_1, 0), y + height);
  652. }
  653. d_1 -= width;
  654. if (d_1 > 0) {
  655. ctx.lineTo(x, y + mathMax(height - d_1, 0));
  656. }
  657. break lo;
  658. }
  659. accumLength += l;
  660. }
  661. ctx.rect(x, y, width, height);
  662. break;
  663. case CMD.Z:
  664. if (drawPart) {
  665. var l = pathSegLen[segCount++];
  666. if (accumLength + l > displayedLength) {
  667. var t = (displayedLength - accumLength) / l;
  668. ctx.lineTo(xi * (1 - t) + x0 * t, yi * (1 - t) + y0 * t);
  669. break lo;
  670. }
  671. accumLength += l;
  672. }
  673. ctx.closePath();
  674. xi = x0;
  675. yi = y0;
  676. }
  677. }
  678. };
  679. PathProxy.prototype.clone = function () {
  680. var newProxy = new PathProxy();
  681. var data = this.data;
  682. newProxy.data = data.slice ? data.slice()
  683. : Array.prototype.slice.call(data);
  684. newProxy._len = this._len;
  685. return newProxy;
  686. };
  687. PathProxy.CMD = CMD;
  688. PathProxy.initDefaultProps = (function () {
  689. var proto = PathProxy.prototype;
  690. proto._saveData = true;
  691. proto._ux = 0;
  692. proto._uy = 0;
  693. proto._pendingPtDist = 0;
  694. proto._version = 0;
  695. })();
  696. return PathProxy;
  697. }());
  698. export default PathProxy;