JavaScript

From QuartzCompositions.com the central source for Quartz Composer :: wiki

This page is about the JavaScript patch available in QC2. For information on the QC3 JavaScript patch, see JavaScript in QC3

This patch executes a JavaScript with an arbitrary number of input / output parameters.

Note that only the core JavaScript classes are supported: Boolean, Number, String, Math, Object, Array, Date and RegExp. Use the "Log()" function to send a text message to the Quartz Composer log: for example, "Log("bonjour")".

Patch class: QCJavaScript

Table of contents

Input Parameters

Values passed to the inputs of the patch are available in JavaScript through the global array variable "inputs"; for example, use "inputs[2]" to get the value on the input #2 inlet. Only a restricted set of Quartz Composer variable types are supported: Boolean, Index, Number, String or Structure. Structures are converted to generic JavaScript objects whose properties are the structure members.

Output Parameters

Values can be sent to the outputs of the patch from JavaScript using the global array variable "outputs". The same restrictions apply regarding the value types as for the inputs.

Parameter Notes

Initially all of the inputs and outputs are undefined. If you attempt to use any of these array members while they are still undefined your calculations will result in a value which is NaN (Not a Number). In order to avoid this scenario you can use temporary variables which you set explicitly while the array members are undefined. As an example...

inputs0 = inputs[0];
if (inputs0 == undefined)
    inputs0 = 0;

outputs[0] = inputs0;

Or, simply:

outputs[0] = inputs[0]||0;


Using temporary variables which are initialized in this way will ensure that your outputs are always defined and avoid the NaN scenario.

Persistent Memory

Variables created with the var token are local to the patch, and are destroyed after each iteration (i.e. you cannot retrieve the value from a previous frame). You can use the Math or the Object objects to get persistent memory between iterations, however. Just add variables as parameters to the object. Be sure to initialize the variable before first using it.

if(Object.x == undefined) { //initialize only once
   Object.x = new Array(2); //create array variable 
   Object.x[0] = 0; 
   Object.x[1] = 0; 
   } 

Object.name = "My Name" //re-initialized with each iteration
Object.x[0] += inputs[0]; 

with(Object) { 
   outputs[0] = x[0]; //running total of input 
   outputs[1] = name; 
   }

Note that the persistent memory is local to that patch and cannot be used to communicate to other JavaScript patches. Do not use the this object within a with() {} clause or Quartz Composer will crash.

Settings