编写程式

来自GeoGebra Manual
跳转至: 导航搜索
Accessories dictionary.png
本页为官方文件,一般使用者无法修改,若有任何误谬,请与官方联络。如欲编辑,请至本页的开放版


GeoGebra 支援两种程式语言 - GGBScript 和 Javascript。我们可以在下列的事件中编写相对的触发程序:

  • 用滑鼠点选某物件时 (On Click)
  • 某物件数值或属性变更时 (On Update, ...)
  • 开启 GeoGebra 档案时 (ggbOnInit)
  • Javascript 触发程序 (listener),详情请参考 JavaScript

我们可以透过物件属性视窗中的“程式”页面来编写程式码。

属性视窗的“程式”页

GGBScript

GGBScript 主要是用 GeoGebra 本身的指令来编写。

范例:
  • 假设 a 是一个范围从 1 到 3 的整数
  • 在命令列中输入以下的集合
    colors = {"red", "green", "blue"}
  • 进入 a属性中的“程式”页面,然后在“On Update”页面中输入:
    SetColor[a, Element[colors, a]]
  • 最后,调整 a 值就可以改变它的颜色
Note 提示: 有些指令只能用于 GGBScript 中,详情请参阅:GGBScript 指令

JavaScript

JavaScript 是一种常用于网页中的程式语言。不像 GGBScript 只能依序执行一些 GeoGebra 指令而己,JavaScript 可以使用 ifwhilefor 等关键字来控制程式的流程。若想进一步了解 JavaScript 的语法,请参阅 developer.mozilla.org。我们也可以使用一些特别的 JavaScript 函数来作图,这些函数都是 ggbApplet 的物件方法,也就是说我们必须使用:

ggbApplet.method(a, b, ...)

这样的语法来呼叫这些函数才行。详细的函数说明,请参阅 ggbApplet 物件方法列表。

范例:

for(var i =0;i<10;i++) ggbApplet.evalCommand("A_"+i+"=(random()*10,random()*10)");

这段程式会画出 10 个点:A0A9,其座标均由乱数产生。

GeoGebra 拥有自己的 JavaScript 编译程式。当我们要将 GeoGebra 汇出网页时,我们可以选择要使用 GeoGebra 自己的编译程式还是浏览器的编译程式。如果你直接在 HTML 网页码中编辑 JavaScript 的话,记得要先执行:ggbApplet = document.applets[0]; 否则 ggbApplet 物件无法自行启动。

Global JavaScript

In the Global JavaScript part of Scripting tab in Properties Dialog you may define some functions or do some assignments that will be done before the construction is loaded. 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. Complete list is available in Reference:JavaScript.

备注: Using any ggbApplet methods in Global JavaScript outside of ggbOnInit will not work as intended since they will be called before the construction is loaded.


范例: 当 GeoGebra 中有物件新增、变更、或删除时,将其状态显示于其他网页元件上。

以下为此网页的 JavaScript 程式部份:

<script type="text/javascript">

function ggbOnInit() {
  // 指定「新增物件、刪除物件、變更名稱、清除繪圖區、物件更新」等事件的觸發程序。
  var applet = document.ggbApplet;
  applet.registerAddListener("addListener");
  applet.registerRemoveListener("removeListener");
  applet.registerRenameListener("renameListener");
  applet.registerClearListener("clearListener");
  applet.registerUpdateListener("updateListener");
}

var strLength = 150;
function addListener(objName) { 
  document.listenerForm.textarea1.value = "add: " + objName + "\n" + document.listenerForm.textarea1.value.substring(0, strLength );	
  printConstructionState();
}

function removeListener(objName) {
  document.listenerForm.textarea1.value = "remove: " + objName + "\n" + document.listenerForm.textarea1.value.substring(0, strLength );	
  printConstructionState();
}

function renameListener(oldObjName, newObjName) {
  document.listenerForm.textarea1.value = "rename: " + objName + "\n" + document.listenerForm.textarea1.value.substring(0, strLength );	
  printConstructionState();
}

function updateListener(objName) {
  strVal = document.ggbApplet.getValueString(objName);
  document.listenerForm.textarea2.value = strVal + "\n" + document.listenerForm.textarea2.value.substring(0, strLength );
}

function clearListener() {
  document.listenerForm.textarea1.value = "";
  document.listenerForm.textarea2.value = "";
  document.listenerForm.textarea1.value = "construction cleared";
  document.listenerForm.consState.value = "";
}

function printConstructionState() {
  var applet = document.ggbApplet;
  var objNumber = applet.getObjectNumber();
  var strState = "Number of objects: " + objNumber;

  for (i=0; i < objNumber; i++) {
	strName = applet.getObjectName(i);
	strType = applet.getObjectType(strName);
	strCommand = applet.getCommandString(strName);
	strState += "\n" + strType + " " + strName + ", " + strCommand;
  }
  document.listenerForm.consState.value = strState;
}
</script>

USB Data Logging (From GeoGebra 4.2)

For logging data from some Vernier USB Data Loggers, eg Go!Motion and Go!Temp one can define a logger listener using the registerLoggerListener method. Such listener can look like this:

function logger(value) {
   var d = value * 1;
   ggbApplet.evalCommand("(CopyFreeObject[a],"+d+")");
   ggbApplet.evalCommand("SetValue[a,a+1]");
}

This script assumes that there is a free number a in the construction. Each time number d is logged, point (a,d) is constructed and a is increased.

© 2024 International GeoGebra Institute