編寫程式

来自GeoGebra Manual
Pegasusroe讨论 | 贡献2011年12月18日 (日) 09:19的版本 →‎GGBScript
跳转至: 导航搜索
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 提示: There are commands that can be only used for scripting. You can find them in the page Scripting_Commands.


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.

GeoGebra contains its own JavaScript engine. When exported as Dynamic Worksheet one can choose whether to use this engine or the one contained in browser applets. If you edit JavaScript in a HTML page, the ggbApplet variable will not be initialized, you have to initialize it e.g. using ggbApplet=document.applets[0]; first.

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.

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