Get a matrix from a patcher to javascript?

Dg's icon

Hello,

Could Anyone explain how to get a matrix from a patcher and process it inside javascript.
What kind of function do I need in JS?

Tutorials explain how to send a processed matrix from javascript out of the [js] object with the outlet() method, to the patcher but not how to retrieve a matrix inside the [js] FROM the patcher.

Thanks.

Wesley Smith's icon

Here's how to get a matrix object in JS with the same data as the input matrix name.

function jit_matrix(name) {
    // create matrix with the same name as the input matrix
var mat = new JitterObject("jit.matrix", name);
var cell = mat.getcell(0, 0);
    post("res: "+cell+"n");
}

Dg's icon

Okay, thank you, nice, but how do I process the matrix now?
Does it need to be inside the function itself. After several attempt this not works:

////////////////////////////////////////////////////
var mydimmap = new JitterObject("jit.dimmap");
mydimmap.invert = [1, 1];

var mtrx2 = new JitterMatrix(4, "char", 320, 240 );
mtrx2.adapt = 1;

function jit_matrix(ext)
    {
        var mtrx = new JitterObject("jit.matrix", ext);
        mtrx2.frommatrix(mtrx);
        mtrx2.matrixcalc(mtrx, mydimmap);
        outlet(0, "jit_matrix", mydimmap.name);
    }

////////////////////////////////////////////////////

>>> Javascript TypeError: mtrx2.matrixcalc is not a function, line 15

I can't achieve to process the matrix inside another function anymore.
This lacks in the JS tutorials.

Dg's icon

Okay, after a good night I go this way:

/////////////////////
process&output2.js

autowatch = 1;

var mtrx = new JitterMatrix(4, "char", 320, 240);
var opfloat;

function recupop(v)
{
    opfloat = v;
}

function bang()
{
    mtrx.op("*", opfloat);
}

function jit_matrix(ext)
{
    {
    mtrx.name = ext;
    }

Max Patch
Copy patch and select New From Clipboard in Max.

    {
     bang();
    }
outlet(0, "jit_matrix", mtrx.name);
}
//////////////////////////

Dg's icon

I am aware that this code is elementary for programmers, but I'm not, I'm only an artist and I think Max is really interesting for its accessibility and documentation. Such basic code should be in the doc because it would allow to gain some days ... yes. I think we need needs more JS basic tutorials ...

Anyway thanks for the example, Wes, on how to retrieve the matrix in JS.
(more, more, more ;-)