transformPath.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. var PathProxy = require("../core/PathProxy");
  2. var _vector = require("../core/vector");
  3. var v2ApplyTransform = _vector.applyTransform;
  4. var CMD = PathProxy.CMD;
  5. var points = [[], [], []];
  6. var mathSqrt = Math.sqrt;
  7. var mathAtan2 = Math.atan2;
  8. function _default(path, m) {
  9. var data = path.data;
  10. var cmd;
  11. var nPoint;
  12. var i;
  13. var j;
  14. var k;
  15. var p;
  16. var M = CMD.M;
  17. var C = CMD.C;
  18. var L = CMD.L;
  19. var R = CMD.R;
  20. var A = CMD.A;
  21. var Q = CMD.Q;
  22. for (i = 0, j = 0; i < data.length;) {
  23. cmd = data[i++];
  24. j = i;
  25. nPoint = 0;
  26. switch (cmd) {
  27. case M:
  28. nPoint = 1;
  29. break;
  30. case L:
  31. nPoint = 1;
  32. break;
  33. case C:
  34. nPoint = 3;
  35. break;
  36. case Q:
  37. nPoint = 2;
  38. break;
  39. case A:
  40. var x = m[4];
  41. var y = m[5];
  42. var sx = mathSqrt(m[0] * m[0] + m[1] * m[1]);
  43. var sy = mathSqrt(m[2] * m[2] + m[3] * m[3]);
  44. var angle = mathAtan2(-m[1] / sy, m[0] / sx); // cx
  45. data[i] *= sx;
  46. data[i++] += x; // cy
  47. data[i] *= sy;
  48. data[i++] += y; // Scale rx and ry
  49. // FIXME Assume psi is 0 here
  50. data[i++] *= sx;
  51. data[i++] *= sy; // Start angle
  52. data[i++] += angle; // end angle
  53. data[i++] += angle; // FIXME psi
  54. i += 2;
  55. j = i;
  56. break;
  57. case R:
  58. // x0, y0
  59. p[0] = data[i++];
  60. p[1] = data[i++];
  61. v2ApplyTransform(p, p, m);
  62. data[j++] = p[0];
  63. data[j++] = p[1]; // x1, y1
  64. p[0] += data[i++];
  65. p[1] += data[i++];
  66. v2ApplyTransform(p, p, m);
  67. data[j++] = p[0];
  68. data[j++] = p[1];
  69. }
  70. for (k = 0; k < nPoint; k++) {
  71. var p = points[k];
  72. p[0] = data[i++];
  73. p[1] = data[i++];
  74. v2ApplyTransform(p, p, m); // Write back
  75. data[j++] = p[0];
  76. data[j++] = p[1];
  77. }
  78. }
  79. }
  80. module.exports = _default;