Difference between revisions of "Scripting"
(remove old info for Java applets) |
Noel Lambert (talk | contribs) |
||
Line 14: | Line 14: | ||
* ''a'' is an integer-valued [[Slider Tool|slider]] ranging from 1 to 3 (therefore Increment equals 1) | * ''a'' is an integer-valued [[Slider Tool|slider]] ranging from 1 to 3 (therefore Increment equals 1) | ||
* type in: <code><nowiki>list1 = {"red", "green", "blue"}</nowiki></code> | * type in: <code><nowiki>list1 = {"red", "green", "blue"}</nowiki></code> | ||
− | * in properties of ''a'', set "On Update" script to <code>SetColor | + | * in properties of ''a'', set "On Update" script to <code>SetColor(a, Element(list1, a))</code> |
* by moving the slider you change its color</div>}} | * by moving the slider you change its color</div>}} | ||
Revision as of 11:35, 12 October 2017
Script is a sequence of commands, that are executed one after each other. GeoGebra supports two scripting languages - GGBScript and Javascript. The execution can be triggered by :
- clicking a particular object
- updating a particular object (when value or properties of the object are changed)
- loading the file (in case of JavaScript)
- Javascript listeners (see Reference:JavaScript)
You can set this script via the Tab Scripting in the Properties Dialog.
GGBScript
You can create scripts consisting of GeoGebra commands, like you can use them in the Input Bar. After triggering the script, every command is executed one after each other.
- a is an integer-valued slider ranging from 1 to 3 (therefore Increment equals 1)
- type in:
list1 = {"red", "green", "blue"}
- in properties of a, set "On Update" script to
SetColor(a, Element(list1, a))
- by moving the slider you change its color
Explanation: Every time the slider is moved, there an update occurs. So, for every move the script is called and the value of a is used to get one color from the list and change the color of the slider a.
#
to start a comment
JavaScript
JavaScript is a programming language used by many Internet technologies. Unlike GeoGebra Script, in Javascript the commands don't have to be executed as a simple sequence, but a control flow (if
, while
, for
) can be used. For generic JavaScript you can find a nice tutorial on developer.mozilla.org.
In GeoGebra, you can use special JavaScript methods which allow you to change the construction. These methods belong to ggbApplet object, which means that you call them as ggbApplet.method_name(parameter,..,parameter)
. For complete list of these methods see Reference:JavaScript.
for(var i =0;i<10;i++) ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");This script creates 10 points A0 to A9 at random coordinates.
Global JavaScript
In the Global JavaScript part of the Scripting tab in the Properties Dialog you may define functions (not variables) which will be available from the other scripts. You can also define function ggbOnInit(), which is called automatically once the construction is loaded. The ggbOnInit function can be used for registering some listeners, as shown below.
function onAdd(name){ alert("Object "+name+" was added."); } function ggbOnInit(){ ggbApplet.registerAddListener("onAdd"); }First we defined function onAdd that given a string shows a message depending on that string. After that, using the ggbOnInit function, we told GeoGebra to call this function whenever a new object is added. Once we reload our construction, function ggbOnInit will be called and since then, when user adds a point named e.g. A, message "Object A was added" will appear.
You can also use listeners for actions like rename, delete and clear construction. A complete list is available in Reference:JavaScript.