This document is intended for those who may wish to write their GlowScript programs directly in RapydScript rather than using the VPython definitions.
The capabilities and syntax of GlowScript are based on those of VPython. For those familiar with VPython, this is a summary of the differences between the two environments. For those familiar with Python, it offers a summary of the main differences between the programming languages Python and RapydScript.
VPython uses the OpenGL 3D graphics library; it does not run in a browser. GlowScript uses the closely related WebGL 3D graphics library that is included in major web browsers. See the first page of the GlowScript Help for browser details.
GlowScript uses the ACE text editor. Here is a list of keyboard shortcuts for find, replace, etc.
In Python, when a for-loop increment is a fraction, you must use the arange() function, like this: "for x in arange(0, 10, 0.1)". In RapydScript you simply say "for x in range(0, 10, 0.1)".
In VPython you can say b.pos = (1,2,0), and the (1,2,0) is converted to vector(1,2,0). This is not possible in GlowScript, where you must be specific about vector quantitities. Here are the possibilities:
b.pos = vec(1,2,0) # okay
b.pos = (1,2,0) # error
A related issue is that in VPython b.x is a shorthand for b.pos.x. The shorthand b.x is not available in GlowScript; you must refer to b.pos.x. Because GlowScript requires that all vectors be explicitly stated to be vectors, the name "vector" has been shortened to "vec".
In VPython, pos, axis, and up are vectors, but size and color are not. In GlowScript size and color are also vector quantities. Here is how to create a non-cubic box with a special color:
box( pos=vec(2,1,0), size=vec(4,2,0.5), color=vec(1,0.6,0)} )
An interesting capability that results from treating color as a vector is that you can blend two colors c1 and c2 as (a*c1 + b*c2)/(a+b).
In VPython axis and length (size.x) are tied to each other. When you change axis of a VPython object, length is set to the magnitude of axis. Similarly, when you set length, you also set the magnitude of axis.
In GlowScript axis and size are not tied to each other. You can set axis without affecting size, and you can set size without affecting axis. Only in the case of the arrow object is there a special attribute, axis_and_length, for changing both direction and length at the same time.
All basic GlowScript objects (box, cylinder, etc., but not arrow) have size attributes, which is not the case in VPython, and these objects fit into a bounding box given by size. By default, size = vec(1,1,1) except for the ring object where size is vec(0.1,1,1) to give a thickness of 0.1. This means that you can change a box to a sphere or vice versa and they'll fit into the same cube of 1 unit on a side (so the default diameter of a sphere is 1 rather than the default radius being 1). Similarly, you can interchange cones and pyramids and cylinders and helices.
There is no radius or length or height or width attribute for these objects. Rather, you specify the bounding box with size. A convenient way to specify the diameter D of a sphere is by setting size to D*vec(1,1,1). Note that you can make an ellipsoid from a sphere, or make a cylinder with an elliptical cross section, by making size.y different from size.z.
A GlowScript curve is an array of point objects, each of which has attributes of pos, color, and radius.
See the curve documentation for ways to manipulate the list of points in a curve.
Unlike VPython, you can vary the radius of the cross section along the curve by setting the radius attribute of the point objects. Also, the curve object itself has its own pos, size, axis, and up attributes, so that you can move or rotate or resize the entire curve object quickly and easily.
VPython animation loops must contain a rate statement to make the animation run at an appropriate rate. In a web browser environment, one must occasionally pause to permit the screen to be redrawn, and programs must be written to run for short amounts of time and then exit, having specified a time in the future when they want to run again. This structure is enabled in GlowScript by you marking in your program where such pauses can occur in your program. This special mark is the word "wait":
ate(100) |
|
There is also a statement sleep(0.01), wait for 1/100th of a second. The statement update() gives the browser the opportunity to update the page display.
Currently, all VPython objects have been implemented except for faces (replaced by vertex, triangle, and quad), text, extrusion, frame, and points (which you can implement simply by displaying small spheres). Instead of frame there is a "compound" object, which among other things makes possible very fast displays of objects consisting of several combined objects. There is also a "clone" capability to make copies of existing objects.
A display region was called a "display" in VPython and is a "canvas" in GlowScript, corresponding to the name by which drawing regions are known on web pages.
You can wait for a mouse event with these statements:
scene.waitfor('click') # wait for mouse click
scene.waitfor('mousedown') # wait for mouse button to be depressed
scene.waitfor('mouseup') # wait for mouse to be moved (with button depressed)
scene.waitfor('mousemove') # wait for mouse button to be released
As in VPython, scene.mouse.pos gives the current position of the mouse.
The statement scene.waitfor('redraw') will wait for the beginning of the next screen update time, which lets you synchronize your updates with the screen. There is a similar statement scene.waitfor('draw_complete') which waits until the next screen update has been completed.
Making graphs in GlowScript is quite similar to making graphs in VPython. See the description in the Help for details. One difference is that each line-graph, scatter-graph, or bar-graph object has its own label, which appears in a legend on the graph. Also, if you don't specify a color, a color will be assigned automatically by the flot graphing library, http://code.google.com/p/flot.
The math functions sqrt, sin, cos, tan, asin, acos, atan, atan2, exp, log, pow, abs, and the constant pi are all available in GlowScript. The function Math.random() provides a random number in the range 0 to 1. For a full discussion, including radian/degree conversion and getting time information, see the description in the Help.
See the description of GlowScript vector functions for some differences from VPython.
Here is a name that is different in VPython and GlowScript:
# VPython scene.mouse.camera |
# GlowScript scene.camera.pos |
In a Python function you can specify that a variable "x" is global rather than local to the function. In RapydScript, as in Python 3, you say "nonlocal x" instead of "global x".
Full RapydScript documentation
Full documentation of RapydScript is available at http://rapydscript.pyjeon.com.