Search code examples
javascriptgl-matrix

How can I use GLMatrix to do matrix multiplications?


I'm just beginning to try to find/use a matrix operations library for JS. I want to do simple translation, scale, and rotation operations on 2D matrices. The mozilla developer website recommends GLMatrix for its "focus on speed and performance."

I'm just wanting to do some simple 2D transformations on matrices, e.g. using mat2d. I want to do something like this:

var a = mat2d.fromValues(2,0,2,0,0,0);
var b = mat2d.fromValues(1,3,3,1,-1,-2);
var ans = mat2d();

mat2d.multiply(ans,a,b);
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.3.2/gl-matrix-min.js"></script>

but I must not have the syntax right because I get an Uncaught TypeError: mat2d is not a function (JSFiddle). I am using GL-matrix-min.js found here.

How can I fix my matrix multiplication code snippet above?


Solution

  • Use

    var ans = mat2d.create();
    

    to create a new (identity) matrix. See the docs or the source.