transformPath.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import PathProxy from '../core/PathProxy.js';
  2. import { applyTransform as v2ApplyTransform } from '../core/vector.js';
  3. var CMD = PathProxy.CMD;
  4. var points = [[], [], []];
  5. var mathSqrt = Math.sqrt;
  6. var mathAtan2 = Math.atan2;
  7. export default function transformPath(path, m) {
  8. if (!m) {
  9. return;
  10. }
  11. var data = path.data;
  12. var len = path.len();
  13. var cmd;
  14. var nPoint;
  15. var i;
  16. var j;
  17. var k;
  18. var p;
  19. var M = CMD.M;
  20. var C = CMD.C;
  21. var L = CMD.L;
  22. var R = CMD.R;
  23. var A = CMD.A;
  24. var Q = CMD.Q;
  25. for (i = 0, j = 0; i < len;) {
  26. cmd = data[i++];
  27. j = i;
  28. nPoint = 0;
  29. switch (cmd) {
  30. case M:
  31. nPoint = 1;
  32. break;
  33. case L:
  34. nPoint = 1;
  35. break;
  36. case C:
  37. nPoint = 3;
  38. break;
  39. case Q:
  40. nPoint = 2;
  41. break;
  42. case A:
  43. var x = m[4];
  44. var y = m[5];
  45. var sx = mathSqrt(m[0] * m[0] + m[1] * m[1]);
  46. var sy = mathSqrt(m[2] * m[2] + m[3] * m[3]);
  47. var angle = mathAtan2(-m[1] / sy, m[0] / sx);
  48. data[i] *= sx;
  49. data[i++] += x;
  50. data[i] *= sy;
  51. data[i++] += y;
  52. data[i++] *= sx;
  53. data[i++] *= sy;
  54. data[i++] += angle;
  55. data[i++] += angle;
  56. i += 2;
  57. j = i;
  58. break;
  59. case R:
  60. p[0] = data[i++];
  61. p[1] = data[i++];
  62. v2ApplyTransform(p, p, m);
  63. data[j++] = p[0];
  64. data[j++] = p[1];
  65. p[0] += data[i++];
  66. p[1] += data[i++];
  67. v2ApplyTransform(p, p, m);
  68. data[j++] = p[0];
  69. data[j++] = p[1];
  70. }
  71. for (k = 0; k < nPoint; k++) {
  72. var p_1 = points[k];
  73. p_1[0] = data[i++];
  74. p_1[1] = data[i++];
  75. v2ApplyTransform(p_1, p_1, m);
  76. data[j++] = p_1[0];
  77. data[j++] = p_1[1];
  78. }
  79. }
  80. path.increaseVersion();
  81. }