PathProxy.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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. if (isFirst) {
  438. x0 = mathCos(startAngle) * rx + cx;
  439. y0 = mathSin(startAngle) * ry + cy;
  440. }
  441. l = mathMax(rx, ry) * mathMin(PI2, Math.abs(delta));
  442. xi = mathCos(endAngle) * rx + cx;
  443. yi = mathSin(endAngle) * ry + cy;
  444. break;
  445. case CMD.R: {
  446. x0 = xi = data[i++];
  447. y0 = yi = data[i++];
  448. var width = data[i++];
  449. var height = data[i++];
  450. l = width * 2 + height * 2;
  451. break;
  452. }
  453. case CMD.Z: {
  454. var dx = x0 - xi;
  455. var dy = y0 - yi;
  456. l = Math.sqrt(dx * dx + dy * dy);
  457. xi = x0;
  458. yi = y0;
  459. break;
  460. }
  461. }
  462. if (l >= 0) {
  463. pathSegLen[segCount++] = l;
  464. pathTotalLen += l;
  465. }
  466. }
  467. this._pathLen = pathTotalLen;
  468. return pathTotalLen;
  469. };
  470. PathProxy.prototype.rebuildPath = function (ctx, percent) {
  471. var d = this.data;
  472. var ux = this._ux;
  473. var uy = this._uy;
  474. var len = this._len;
  475. var x0;
  476. var y0;
  477. var xi;
  478. var yi;
  479. var x;
  480. var y;
  481. var drawPart = percent < 1;
  482. var pathSegLen;
  483. var pathTotalLen;
  484. var accumLength = 0;
  485. var segCount = 0;
  486. var displayedLength;
  487. var pendingPtDist = 0;
  488. var pendingPtX;
  489. var pendingPtY;
  490. if (drawPart) {
  491. if (!this._pathSegLen) {
  492. this._calculateLength();
  493. }
  494. pathSegLen = this._pathSegLen;
  495. pathTotalLen = this._pathLen;
  496. displayedLength = percent * pathTotalLen;
  497. if (!displayedLength) {
  498. return;
  499. }
  500. }
  501. lo: for (var i = 0; i < len;) {
  502. var cmd = d[i++];
  503. var isFirst = i === 1;
  504. if (isFirst) {
  505. xi = d[i];
  506. yi = d[i + 1];
  507. x0 = xi;
  508. y0 = yi;
  509. }
  510. if (cmd !== CMD.L && pendingPtDist > 0) {
  511. ctx.lineTo(pendingPtX, pendingPtY);
  512. pendingPtDist = 0;
  513. }
  514. switch (cmd) {
  515. case CMD.M:
  516. x0 = xi = d[i++];
  517. y0 = yi = d[i++];
  518. ctx.moveTo(xi, yi);
  519. break;
  520. case CMD.L: {
  521. x = d[i++];
  522. y = d[i++];
  523. var dx = mathAbs(x - xi);
  524. var dy = mathAbs(y - yi);
  525. if (dx > ux || dy > uy) {
  526. if (drawPart) {
  527. var l = pathSegLen[segCount++];
  528. if (accumLength + l > displayedLength) {
  529. var t = (displayedLength - accumLength) / l;
  530. ctx.lineTo(xi * (1 - t) + x * t, yi * (1 - t) + y * t);
  531. break lo;
  532. }
  533. accumLength += l;
  534. }
  535. ctx.lineTo(x, y);
  536. xi = x;
  537. yi = y;
  538. pendingPtDist = 0;
  539. }
  540. else {
  541. var d2 = dx * dx + dy * dy;
  542. if (d2 > pendingPtDist) {
  543. pendingPtX = x;
  544. pendingPtY = y;
  545. pendingPtDist = d2;
  546. }
  547. }
  548. break;
  549. }
  550. case CMD.C: {
  551. var x1 = d[i++];
  552. var y1 = d[i++];
  553. var x2 = d[i++];
  554. var y2 = d[i++];
  555. var x3 = d[i++];
  556. var y3 = d[i++];
  557. if (drawPart) {
  558. var l = pathSegLen[segCount++];
  559. if (accumLength + l > displayedLength) {
  560. var t = (displayedLength - accumLength) / l;
  561. cubicSubdivide(xi, x1, x2, x3, t, tmpOutX);
  562. cubicSubdivide(yi, y1, y2, y3, t, tmpOutY);
  563. ctx.bezierCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2], tmpOutX[3], tmpOutY[3]);
  564. break lo;
  565. }
  566. accumLength += l;
  567. }
  568. ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
  569. xi = x3;
  570. yi = y3;
  571. break;
  572. }
  573. case CMD.Q: {
  574. var x1 = d[i++];
  575. var y1 = d[i++];
  576. var x2 = d[i++];
  577. var y2 = d[i++];
  578. if (drawPart) {
  579. var l = pathSegLen[segCount++];
  580. if (accumLength + l > displayedLength) {
  581. var t = (displayedLength - accumLength) / l;
  582. quadraticSubdivide(xi, x1, x2, t, tmpOutX);
  583. quadraticSubdivide(yi, y1, y2, t, tmpOutY);
  584. ctx.quadraticCurveTo(tmpOutX[1], tmpOutY[1], tmpOutX[2], tmpOutY[2]);
  585. break lo;
  586. }
  587. accumLength += l;
  588. }
  589. ctx.quadraticCurveTo(x1, y1, x2, y2);
  590. xi = x2;
  591. yi = y2;
  592. break;
  593. }
  594. case CMD.A:
  595. var cx = d[i++];
  596. var cy = d[i++];
  597. var rx = d[i++];
  598. var ry = d[i++];
  599. var startAngle = d[i++];
  600. var delta = d[i++];
  601. var psi = d[i++];
  602. var anticlockwise = !d[i++];
  603. var r = (rx > ry) ? rx : ry;
  604. var isEllipse = mathAbs(rx - ry) > 1e-3;
  605. var endAngle = startAngle + delta;
  606. var breakBuild = false;
  607. if (drawPart) {
  608. var l = pathSegLen[segCount++];
  609. if (accumLength + l > displayedLength) {
  610. endAngle = startAngle + delta * (displayedLength - accumLength) / l;
  611. breakBuild = true;
  612. }
  613. accumLength += l;
  614. }
  615. if (isEllipse && ctx.ellipse) {
  616. ctx.ellipse(cx, cy, rx, ry, psi, startAngle, endAngle, anticlockwise);
  617. }
  618. else {
  619. ctx.arc(cx, cy, r, startAngle, endAngle, anticlockwise);
  620. }
  621. if (breakBuild) {
  622. break lo;
  623. }
  624. if (isFirst) {
  625. x0 = mathCos(startAngle) * rx + cx;
  626. y0 = mathSin(startAngle) * ry + cy;
  627. }
  628. xi = mathCos(endAngle) * rx + cx;
  629. yi = mathSin(endAngle) * ry + cy;
  630. break;
  631. case CMD.R:
  632. x0 = xi = d[i];
  633. y0 = yi = d[i + 1];
  634. x = d[i++];
  635. y = d[i++];
  636. var width = d[i++];
  637. var height = d[i++];
  638. if (drawPart) {
  639. var l = pathSegLen[segCount++];
  640. if (accumLength + l > displayedLength) {
  641. var d_1 = displayedLength - accumLength;
  642. ctx.moveTo(x, y);
  643. ctx.lineTo(x + mathMin(d_1, width), y);
  644. d_1 -= width;
  645. if (d_1 > 0) {
  646. ctx.lineTo(x + width, y + mathMin(d_1, height));
  647. }
  648. d_1 -= height;
  649. if (d_1 > 0) {
  650. ctx.lineTo(x + mathMax(width - d_1, 0), y + height);
  651. }
  652. d_1 -= width;
  653. if (d_1 > 0) {
  654. ctx.lineTo(x, y + mathMax(height - d_1, 0));
  655. }
  656. break lo;
  657. }
  658. accumLength += l;
  659. }
  660. ctx.rect(x, y, width, height);
  661. break;
  662. case CMD.Z:
  663. if (drawPart) {
  664. var l = pathSegLen[segCount++];
  665. if (accumLength + l > displayedLength) {
  666. var t = (displayedLength - accumLength) / l;
  667. ctx.lineTo(xi * (1 - t) + x0 * t, yi * (1 - t) + y0 * t);
  668. break lo;
  669. }
  670. accumLength += l;
  671. }
  672. ctx.closePath();
  673. xi = x0;
  674. yi = y0;
  675. }
  676. }
  677. };
  678. PathProxy.prototype.clone = function () {
  679. var newProxy = new PathProxy();
  680. var data = this.data;
  681. newProxy.data = data.slice ? data.slice()
  682. : Array.prototype.slice.call(data);
  683. newProxy._len = this._len;
  684. return newProxy;
  685. };
  686. PathProxy.CMD = CMD;
  687. PathProxy.initDefaultProps = (function () {
  688. var proto = PathProxy.prototype;
  689. proto._saveData = true;
  690. proto._ux = 0;
  691. proto._uy = 0;
  692. proto._pendingPtDist = 0;
  693. proto._version = 0;
  694. })();
  695. return PathProxy;
  696. }());
  697. export default PathProxy;