matrix.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. export function create() {
  2. return [1, 0, 0, 1, 0, 0];
  3. }
  4. export function identity(out) {
  5. out[0] = 1;
  6. out[1] = 0;
  7. out[2] = 0;
  8. out[3] = 1;
  9. out[4] = 0;
  10. out[5] = 0;
  11. return out;
  12. }
  13. export function copy(out, m) {
  14. out[0] = m[0];
  15. out[1] = m[1];
  16. out[2] = m[2];
  17. out[3] = m[3];
  18. out[4] = m[4];
  19. out[5] = m[5];
  20. return out;
  21. }
  22. export function mul(out, m1, m2) {
  23. var out0 = m1[0] * m2[0] + m1[2] * m2[1];
  24. var out1 = m1[1] * m2[0] + m1[3] * m2[1];
  25. var out2 = m1[0] * m2[2] + m1[2] * m2[3];
  26. var out3 = m1[1] * m2[2] + m1[3] * m2[3];
  27. var out4 = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  28. var out5 = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  29. out[0] = out0;
  30. out[1] = out1;
  31. out[2] = out2;
  32. out[3] = out3;
  33. out[4] = out4;
  34. out[5] = out5;
  35. return out;
  36. }
  37. export function translate(out, a, v) {
  38. out[0] = a[0];
  39. out[1] = a[1];
  40. out[2] = a[2];
  41. out[3] = a[3];
  42. out[4] = a[4] + v[0];
  43. out[5] = a[5] + v[1];
  44. return out;
  45. }
  46. export function rotate(out, a, rad, pivot) {
  47. if (pivot === void 0) { pivot = [0, 0]; }
  48. var aa = a[0];
  49. var ac = a[2];
  50. var atx = a[4];
  51. var ab = a[1];
  52. var ad = a[3];
  53. var aty = a[5];
  54. var st = Math.sin(rad);
  55. var ct = Math.cos(rad);
  56. out[0] = aa * ct + ab * st;
  57. out[1] = -aa * st + ab * ct;
  58. out[2] = ac * ct + ad * st;
  59. out[3] = -ac * st + ct * ad;
  60. out[4] = ct * (atx - pivot[0]) + st * (aty - pivot[1]) + pivot[0];
  61. out[5] = ct * (aty - pivot[1]) - st * (atx - pivot[0]) + pivot[1];
  62. return out;
  63. }
  64. export function scale(out, a, v) {
  65. var vx = v[0];
  66. var vy = v[1];
  67. out[0] = a[0] * vx;
  68. out[1] = a[1] * vy;
  69. out[2] = a[2] * vx;
  70. out[3] = a[3] * vy;
  71. out[4] = a[4] * vx;
  72. out[5] = a[5] * vy;
  73. return out;
  74. }
  75. export function invert(out, a) {
  76. var aa = a[0];
  77. var ac = a[2];
  78. var atx = a[4];
  79. var ab = a[1];
  80. var ad = a[3];
  81. var aty = a[5];
  82. var det = aa * ad - ab * ac;
  83. if (!det) {
  84. return null;
  85. }
  86. det = 1.0 / det;
  87. out[0] = ad * det;
  88. out[1] = -ab * det;
  89. out[2] = -ac * det;
  90. out[3] = aa * det;
  91. out[4] = (ac * aty - ad * atx) * det;
  92. out[5] = (ab * atx - aa * aty) * det;
  93. return out;
  94. }
  95. export function clone(a) {
  96. var b = create();
  97. copy(b, a);
  98. return b;
  99. }