ShapePath.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import {
  2. createSizedArray,
  3. } from '../helpers/arrays';
  4. import pointPool from '../pooling/point_pool';
  5. function ShapePath() {
  6. this.c = false;
  7. this._length = 0;
  8. this._maxLength = 8;
  9. this.v = createSizedArray(this._maxLength);
  10. this.o = createSizedArray(this._maxLength);
  11. this.i = createSizedArray(this._maxLength);
  12. }
  13. ShapePath.prototype.setPathData = function (closed, len) {
  14. this.c = closed;
  15. this.setLength(len);
  16. var i = 0;
  17. while (i < len) {
  18. this.v[i] = pointPool.newElement();
  19. this.o[i] = pointPool.newElement();
  20. this.i[i] = pointPool.newElement();
  21. i += 1;
  22. }
  23. };
  24. ShapePath.prototype.setLength = function (len) {
  25. while (this._maxLength < len) {
  26. this.doubleArrayLength();
  27. }
  28. this._length = len;
  29. };
  30. ShapePath.prototype.doubleArrayLength = function () {
  31. this.v = this.v.concat(createSizedArray(this._maxLength));
  32. this.i = this.i.concat(createSizedArray(this._maxLength));
  33. this.o = this.o.concat(createSizedArray(this._maxLength));
  34. this._maxLength *= 2;
  35. };
  36. ShapePath.prototype.setXYAt = function (x, y, type, pos, replace) {
  37. var arr;
  38. this._length = Math.max(this._length, pos + 1);
  39. if (this._length >= this._maxLength) {
  40. this.doubleArrayLength();
  41. }
  42. switch (type) {
  43. case 'v':
  44. arr = this.v;
  45. break;
  46. case 'i':
  47. arr = this.i;
  48. break;
  49. case 'o':
  50. arr = this.o;
  51. break;
  52. default:
  53. arr = [];
  54. break;
  55. }
  56. if (!arr[pos] || (arr[pos] && !replace)) {
  57. arr[pos] = pointPool.newElement();
  58. }
  59. arr[pos][0] = x;
  60. arr[pos][1] = y;
  61. };
  62. ShapePath.prototype.setTripleAt = function (vX, vY, oX, oY, iX, iY, pos, replace) {
  63. this.setXYAt(vX, vY, 'v', pos, replace);
  64. this.setXYAt(oX, oY, 'o', pos, replace);
  65. this.setXYAt(iX, iY, 'i', pos, replace);
  66. };
  67. ShapePath.prototype.reverse = function () {
  68. var newPath = new ShapePath();
  69. newPath.setPathData(this.c, this._length);
  70. var vertices = this.v;
  71. var outPoints = this.o;
  72. var inPoints = this.i;
  73. var init = 0;
  74. if (this.c) {
  75. newPath.setTripleAt(vertices[0][0], vertices[0][1], inPoints[0][0], inPoints[0][1], outPoints[0][0], outPoints[0][1], 0, false);
  76. init = 1;
  77. }
  78. var cnt = this._length - 1;
  79. var len = this._length;
  80. var i;
  81. for (i = init; i < len; i += 1) {
  82. newPath.setTripleAt(vertices[cnt][0], vertices[cnt][1], inPoints[cnt][0], inPoints[cnt][1], outPoints[cnt][0], outPoints[cnt][1], i, false);
  83. cnt -= 1;
  84. }
  85. return newPath;
  86. };
  87. ShapePath.prototype.length = function () {
  88. return this._length;
  89. };
  90. export default ShapePath;