matrix.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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) {
  47. var aa = a[0];
  48. var ac = a[2];
  49. var atx = a[4];
  50. var ab = a[1];
  51. var ad = a[3];
  52. var aty = a[5];
  53. var st = Math.sin(rad);
  54. var ct = Math.cos(rad);
  55. out[0] = aa * ct + ab * st;
  56. out[1] = -aa * st + ab * ct;
  57. out[2] = ac * ct + ad * st;
  58. out[3] = -ac * st + ct * ad;
  59. out[4] = ct * atx + st * aty;
  60. out[5] = ct * aty - st * atx;
  61. return out;
  62. }
  63. export function scale(out, a, v) {
  64. var vx = v[0];
  65. var vy = v[1];
  66. out[0] = a[0] * vx;
  67. out[1] = a[1] * vy;
  68. out[2] = a[2] * vx;
  69. out[3] = a[3] * vy;
  70. out[4] = a[4] * vx;
  71. out[5] = a[5] * vy;
  72. return out;
  73. }
  74. export function invert(out, a) {
  75. var aa = a[0];
  76. var ac = a[2];
  77. var atx = a[4];
  78. var ab = a[1];
  79. var ad = a[3];
  80. var aty = a[5];
  81. var det = aa * ad - ab * ac;
  82. if (!det) {
  83. return null;
  84. }
  85. det = 1.0 / det;
  86. out[0] = ad * det;
  87. out[1] = -ab * det;
  88. out[2] = -ac * det;
  89. out[3] = aa * det;
  90. out[4] = (ac * aty - ad * atx) * det;
  91. out[5] = (ab * atx - aa * aty) * det;
  92. return out;
  93. }
  94. export function clone(a) {
  95. var b = create();
  96. copy(b, a);
  97. return b;
  98. }