Download Max Tutorial Manual - Electronic Art and Design
Transcript
Designing User Interfaces
in JavaScript
Tutorial 51
{
glcolor(vfrgb[0],vfrgb[1],vfrgb[2],vfrgb[3]);
}
else // set 'off' color (midway between vbrgb and vfrgb)
{
glcolor(vmrgb[0],vmrgb[1],vmrgb[2],vmrgb[3]);
}
circle(0.7/Math.max(nrows,ncols)); // draw the circle
}
}
}
}
The graphics commands (everything beginning with ‘gl’, as well as the circle()
command) are all methods and properties of the sketch object, which encapsulates most
of the OpenGL API, much as the Math object encapsulates most common math
functions.
Note: The JavaScript with() statement allows us to use properties and
methods belonging to an object (in this case the sketch object that provides
us with our OpenGL functionality) without having to reference ‘sketch’ in
every command. Without the with(), we would have to write
sketch.glcolor() instead of glcolor(), sketch.circle()
instead of circle(), etc. This useful trick could also be implemented in
functions that rely heavily on other objects (e.g. Task or Patcher).
Our draw() function sets some default drawing behaviors and clears the window with
the color defined by the vbrgb array. It then iterates through our state array based on how
many columns (ncols) and rows (nrows) we’ve defined for our object. If a particular state
element is 0 (off), it draws a circle in the color defined by vmrgb. If an element is 1 (on),
the circle is drawn in the vfrgb color. The position of the circles is determined by the
number of rows and columns, and is based on boundaries of our OpenGL world, which
are set to be between –1.0 and 1.0 on both axes (i.e. the middle of our jsui window in
OpenGL coordinates is 0, 0).
Note: The size of the world is not limited to coordinates in the range of
–1.0 to 1.0; our default viewport merely sets us at the center of the scene
whose y range is –1.0 to 1.0 and whose x range is scaled based on the
aspect ration of the object. Because our object box happens to be square,
our ranges are the same on both axes. Changing the viewport (by
manipulating the position of our virtual “camera”, for example) will
change what coordinates are visible in the jsui object’s box.
335