Comments:参考:JavaScript
GGB 档中使用 JavaScript
如果在物件的“程式”页面中要使用 JavaScript 来控制 GGB 的话,可以使用 ggbApplet 这个关键字,然后用下面介绍的各种 JavaScript 指令。
var a = ggbApplet; a.evalCommand('n = 3'); a.evalCommand('s = Sequence[k^2, k, 1, n]');
var a = ggbApplet; 不能使用: var a = document.ggbApplet; 否则会产生“document 物件不存在”的错误!
网页中使用 JavaScript
<input type="button" value="重设" onclick="document.applets[0].reset();">
<input type="button" value="隐藏 A 点" onclick="document.applets[0].setVisible('A', false);"> <input type="button" value="显示 A 点" onclick="document.applets[0].setVisible('A', true);">
<applet name="ggbApplet" ... > </applet> ...... <script type="text/javascript"> function drawLineAB() { var applet = document.ggbApplet; applet.evalCommand("A = (1,1)"); applet.evalCommand("B = (3,2)"); applet.evalCommand("s = Line[A, B]"); } </script> ...... <input type="button" value="Do construction" onclick="drawLineAB();">
通用指令
回传:若执行成功则传回 true,若失败则传回 false。
范例:{{{4}}}
说明:此指令会执行“命令字串”中所指定的 GGB 指令,此与直接在“命令列”中下该指令有相同的效果。从 3.2 版起,我们可以在“命令字串”中加入特殊的换行字串“\n”,这样一来,我们就可以同时下许多个指令。
回传:无。
范例:{{{4}}}
说明:此指令可用于设定由 evalCommand()
所产生的动作的回复点。
设定物件属性
函数 | 版本 | 说明 |
---|---|---|
void deleteObject(String objName) | 2.7 | Deletes the object with the given name. |
void setValue(String objName, double value) | 3.2 | Sets the double value of the object with the given name. Note: if the specified object is boolean, use a value of 1 to set it to true and any other value to set it to false. For any other object type, nothing is done. |
void setCoords(String objName, double x, double y) | 3.0 | Sets the coordinates of the object with the given name. Note: if the specified object is not a point or a vector, nothing is done. |
void setColor(物件名, 红, 绿, 蓝) | 2.7 | 设定物件的颜色,红绿蓝 (RGB) 的颜色值为 0 ~ 255。 例如: document.ggbApplet.setColor("lineAB", 255, 0, 0) |
void setVisible(String objName, boolean visible) | 2.7 | Shows or hides the object with the given name in the graphics window. |
void setLabelVisible(String objName, boolean visible) | 3.0 | Shows or hides the label of the object with the given name in the graphics window. |
void setLabelStyle(String objName, int style) | 3.0 | Sets the label style of the object with the given name in the graphics window. Possible label styles are NAME = 0, NAME_VALUE = 1, VALUE = 2 and (from GeoGebra 3.2) CAPTION = 3 |
void setFixed(String objName, boolean flag) | 3.0 | Sets the fixed state of the object with the given name. Note: fixed objects cannot be changed. |
void setTrace(String objName, boolean flag) | 3.0 | Turns the trace of the object with the given name on or off. |
boolean renameObject(String oldObjName, String newObjName) | 3.2 | Renames oldObjName to newObjName. Returns whether the rename was successful |
void setLayer(String objName, int layer) | 3.2 | Sets the layer of the object |
void setLayerVisible(int layer, boolean visible) | 3.2 | Shows or hides the all objects in the given layer |
void setLineStyle(物件名, 样式编号) | 3.2 | 设定线条样式:0 - 实线,1 - 长虚线,2 - 短虚线,3 - 点虚线,4 - 长点虚线。 |
void setLineThickness(物件名, 厚度) | 3.2 | 设定物件(线、圆等)的厚度,值从 1 ~ 13。 |
void setPointStyle(String objName, int style) | 3.2 | Sets the style of points (-1 default, 0 filled circle, 1 circle, 2 cross, 3 plus, 4 filled diamond, 5 unfilled diamond, 6 triangle (north), 7 triangle (south), 8 triangle (east), 9 triangle (west)) |
void setPointSize(String objName, int size) | 3.2 | Sets the size of a point (from 1 to 9) |
void setFilling(String objName, double filling) | 3.2 | Sets the filling of an object (from 0 to 1) |
String getPNGBase64(double exportScale, boolean transparent, double DPI) | 4.0 | Returns Graphics View 1 as a base64-encoded String eg var str = ggbApplet.getPNGBase64(1, true, 72); |
boolean writePNGtoFile(String filename, double exportScale, boolean transparent, double DPI) | 4.0 | Exports Graphics View 1 to a .PNG file (signed applets only) eg var success = ggbApplet.writePNGtoFile("c:\\test.png", 1, false, 300); |
boolean isIndependent(String objName) | 4.0 | checks if objName is independent |
boolean isMoveable(String objName) | 4.0 | checks if objName is is moveable |
String getBase64() | 4.0 | Gets the current construction as a base64-encoded .ggb file |
void setBase64(String) | 4.0 | Sets the current construction from a base64-encoded .ggb file |
设定动态效果
函数 | 版本 | 说明 | ||
---|---|---|---|---|
setAnimating | 3.2 | 格式:setAnimating( 物件名, 启动动画? ) 备注: 设定物件要不要启动动画效果。 范例: setAnimating("A", true)
|
||
setAnimationSpeed | 3.2 | 格式:setAnimationSpeed(物件名, 速度) 备注: 设定物件的动画速度。 |
||
startAnimation | 3.2 | 格式:startAnimation() 备注: 正式启动动画功能。注意:只有用过 setAnimating() 设定过的物件才会动喔! |
||
void stopAnimation() | 3.2 | Stops animation for all objects with the animating flag set, see setAnimating() | ||
boolean isAnimationRunning() | 3.2 | Returns whether automatic animation is currently running. |
取得物件属性
回传:物件的 x 座标。若物件不是点或向量,则传回 0。
范例:{{{4}}}
回传:物件的 y 座标。若物件不是点或向量,则传回 0。
范例:{{{4}}}
回传:物件的数值、长度、面积等。若物件为“真假值”,则 true 传回 1,false 传回 0。
范例:{{{4}}}
Method Signature | Since | Description |
---|---|---|
String getColor(String objName) | 2.7 | Returns the color of the object with the given name as a hex string, e.g. "#FF0000" for red. Note that the hex string always starts with # and contains no lower case letters. |
boolean getVisible(String objName) | 3.2 | Returns true or false depending on whether the object is visible in the Graphics View. Returns false if the object does not exist. |
boolean getVisible(String objName, int view) | 4.2 | Returns true or false depending on whether the object is visible in Graphics View 'view' (1 or 2). Returns false if the object does not exist. |
String getValueString(String objName) | 2.7 | Returns the value of the object with the given name as a string. |
String getDefinitionString(String objName) | 2.7 | Returns the definition of the object with the given name as a string. |
String getCommandString(String objName) | 2.7 | Returns the command of the object with the given name as a string. |
String getObjectType(String objName) | 2.7 | Returns the type of the given object as a string (like "point", "line", "circle", etc.). |
boolean exists(String objName) | 2.7 | Returns whether an object with the given name exists in the construction. |
boolean isDefined(String objName) | 2.7 | Returns whether the given object's value is valid at the moment. |
String [] getAllObjectNames() Deprecated since 3.0 |
2.7 | Returns an array with all object names in the construction. Note: using arrays in JavaScript causes problems with some browsers. Use getObjectNumber() and getObjectName() instead. |
int getObjectNumber() | 3.0 | Returns the number of objects in the construction. |
String getObjectName(int i) | 3.0 | Returns the name of the n-th object of the construction. |
String getLayer(String objName) | 3.2 | Returns the layer of the object. |
int getLineStyle(String objName) | 3.2 | Gets the line style for the object (0 to 4) |
int getLineThickness(String objName) | 3.2 | Gets the thickness of the line (1 to 13) |
int getPointStyle(String objName) | 3.2 | Gets the style of points (-1 default, 0 filled circle, 1 circle, 2 cross, 3 plus, 4 filled diamond, 5 unfilled diamond, 6 triangle (north), 7 triangle (south), 8 triangle (east), 9 triangle (west)) |
int getPointSize(String objName) | 3.2 | Gets the size of a point (from 1 to 9) |
double getFilling(String objName) | 3.2 | Gets the filling of an object (from 0 to 1) |
绘图区指令
函数 | 版本 | 说明 |
---|---|---|
void setMode( int 工具编号 ) | 2.7 | 设定绘图区要使用的工具 请参阅:工具编号 与 applet 参数中的“showToolBar”、“customToolBar”参数 |
void openFile(String strURL) | 2.7 | Opens a construction from a file (given as absolute or relative URL string) |
void reset() | 2.7 | Reloads the initial construction (given in filename parameter) of this applet. |
void refreshViews() | 2.7 | Refreshs all views. Note: this clears all traces in the graphics window. |
void setOnTheFlyPointCreationActive(boolean flag) |
3.2 | Turns on the fly creation of points in graphics view on (true) or off (false). Note: this is useful if you don't want tools to have the side effect of creating points. For example, when this flag is set to false, the tool "line through two points" will not create points on the fly when you click on the background of the graphics view. |
void hideCursorWhenDragging(boolean flag) |
3.2 | Hides (true) or shows (false) the mouse cursor (pointer) when dragging an object to change the construction. |
void setRepaintingActive(boolean flag) |
2.7 | Turns the repainting of this applet on (true) or off (false). Note: use this method for efficient repainting when you invoke several methods. |
void setErrorDialogsActive(boolean flag) | 3.0 | Turns showing of error dialogs on (true) or off (false). Note: this is especially useful together with evalCommand(). |
void setCoordSystem(double xmin, double xmax, double ymin, double ymax) | 3.0 | Sets the Cartesian coordinate system of the graphics window. |
void setAxesVisible(boolean xAxis, boolean yAxis) | 3.0 | Shows or hides the x- and y-axis of the coordinate system in the graphics window. |
void setGridVisible(boolean flag) | 3.0 | Shows or hides the coordinate grid in the graphics window. |
由 Applet 下达 JavaScript 指令
下表的指令让我们可以从 Applet 中的物件所产生的“事件”,下达一般的 JavaScript 指令。
当使用这些指令时,请记得一定要在<applet>
标签的最后面加入 MAYSCRIPT 这个属性才行。例如:
<applet name="ggbApplet" code="geogebra.GeoGebraApplet" codebase="." archive="geogebra.jar" width="500" height="250" MAYSCRIPT>
反过来说,如果我们只是要使用 document.ggbApplet
的物件方法去操控 Applet 中的物件时,这个 MAYSCRIPT 属性就不需要设定了。
For example, these methods can be used to:
- monitor user actions (see java2javascript example)
- communicate between two GeoGebra applets (see java2java example)
函数 | 版本 | 说明 |
---|---|---|
void registerAddListener(String JSFunctionName) | 3.0 | Registers a JavaScript function as an add listener for the applet's construction. Whenever a new object is created in the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the name of the newly created object as its single argument. Example: First, register a listening JavaScript function: ggbApplet.registerAddListener("myAddListenerFunction");
When an object "A" is created, the GeoGebra Applet will call the Javascript function myAddListenerFunction("A");
|
void unregisterAddListener(String objName) | 3.0 | Removes a previously registered add listener, see registerAddListener() |
void registerRemoveListener(String JSFunctionName) | 3.0 | Registers a JavaScript function as a remove listener for the applet's construction. Whenever an object is deleted from the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the name of the deleted object as its single argument. Note: when a construction is cleared, remove is not called for every single object, see registerClearListener(). Example: First, register a listening JavaScript function: ggbApplet.registerRemoveListener("myRemoveListenerFunction");
When the object "A" is deleted, the GeoGebra Applet will call the Javascript function myRemoveListenerFunction("A");
|
void unregisterRemoveListener(String objName) | 3.0 | Removes a previously registered remove listener, see registerRemoveListener() |
void registerUpdateListener(String JSFunctionName) | 3.0 | Registers a JavaScript function as a update listener for the applet's construction. Whenever any object is updated in the GeoGebraApplet's construction, the JavaScript function JSFunctionNameis called using the name of the updated object as its single argument. Note: when you only want to listen for the updates of a single object use registerObjectUpdateListener() instead. Example: First, register a listening JavaScript function: ggbApplet.registerUpdateListener("myUpdateListenerFunction");
When the object "A" is updated, the GeoGebra Applet will call the Javascript function myUpdateListenerFunction("A");
|
void unregisterUpdateListener(String objName) | 3.0 | Removes a previously registered update listener, see registerUpdateListener() |
void registerObjectUpdateListener(物件名, 函数名) | 3.0 | 设定当某物件的属性有所变更时的处理程序。例如:如果设定
则当物件 A 有所变更时,便会执行 |
void unregisterObjectUpdateListener(String objName) | 3.0 | Removes a previously registered object update listener of the object with the given name, see registerObjectUpdateListener() |
void registerRenameListener(String JSFunctionName) | 3.0 | Registers a JavaScript function as a rename listener for the applet's construction. Whenever an object is renamed in the GeoGebraApplet's construction, the JavaScript function JSFunctionName is called using the old name and the new name of the renamed object as its two arguments. Example: First, register a listening JavaScript function: ggbApplet.registerRenameListener("myRenameListenerFunction");
When an object "A" is renamed to "B", the GeoGebra Applet will call the Javascript function myRenameListenerFunction("A", "B");
|
void unregisterRenameListener(String objName) | 3.0 | Removes a previously registered rename listener, see registerRenameListener() |
void registerClearListener(String JSFunctionName) | 3.0 | Registers a JavaScript function as a clear listener for the applet's construction. Whenever the construction in the GeoGebraApplet is cleared (i.e. all objects are removed), the JavaScript function JSFunctionName is called using no arguments. Note: all update listeners are unregistered when a construction is cleared. See registerUpdateListener() and registerRemoveListener(). Example: First, register a listening JavaScript function: ggbApplet.registerClearListener("myClearListenerFunction");
When the construction is cleared (i.e. after reseting a construction or opening a new construction file), the GeoGebra Applet will call the Javascript function myClearListenerFunction();
|
void unregisterClearListener(String JSFunctionName) | 3.0 | Removes a previously registered clear listener, see registerClearListener() |
GeoGebra 的 XML 码
GeoGebra 档案 (*.ggb, *.ggt) 内部是以 XML 的格式储存的,利用下列的指令可以让我们任意更改 GGB 档内部的物件属性。因此,如果上面所提到的所有指令都不能符合你的需求的话,这时就可以考虑利用下列的指令,但前提是你必须有点了解 XML 格式才行。
回传:无
范例:{{{4}}}
说明:解析“XML 字串”,并产生或变更相对应的物件。注意:此指令不会清除已有的物件。
回传:无
范例:{{{4}}}
说明:解析“XML 字串”,并产生产生新的构图。注意:此指令会清除已有的物件。
回传:整个构图案本的 XML 字串
范例:{{{4}}}
说明:此指令可用来储存现有的构图。
回传:该物件的 <ELEMENT> 标签
范例:{{{4}}}
回传:若为自变物件,则传回“空字串”。若为应变物件,则传回其算法与相关输出物件之 XML。
范例:{{{4}}}
Miscellaneous
Method Signature | Since | Description |
---|---|---|
String evalMathPiper(String string) | 3.2 | Passes the string to MathPiper and returns the result as a String. Removed in GeoGebra 4.0 and will replaced with evalGeoGebraCAS() in GeoGebra 4.2 |
String getIPAddress() | 3.2 | Returns the IP address of the local computer as a String eg "192.168.0.4" |
String getHostname() | 3.2 | Returns the hostname of the local computer as a String |
void debug(String string) | 3.2 | Prints the string to the Java Console |