CanvasScripting - Maple Help

Workflow Diagram

Consider the "FindPrimes" example, where all math on a canvas is checked to see if it is prime.  The workflow is as follows:

Step 1:  Create/Edit/Modify the canvas -- this is where the user enters some new math.

Step 2: When the button is pressed the contents of the canvas will be serialized into an XML structure.  This includes all text, math, the button, and all of the Maple code behind the button.  The "canvas" will be passed as an argument to the FindPrimes() procedure, which can use GetMath and GetElements if it wants to inspect any values.

Step 3: A script is created.  The script is generated by creating a Script object, and then calling any of the Script object methods such as SetActive, Annotate, and SetMath.  This script is finally broken down into a series of XML instructions by calling ToString.

Step 4: In the Maple environment Script commands produce results immediately as they are invoked, but in the Maple Learn environment, no action is taken until the complete script is written and sent back to the Maple Learn interface, at which point the individual script commands are processed.  This happens because the FindPrimes button action procedure is processed by a Maple engine in the cloud.  This is separate from the returned XML instructions that will be processed in the client, no longer having a connection to Maple.



Raw Script

Here is an example that uses raw script commands to invoke actions that are only understood by the Maple Learn environment.  The Command method processes these raw commands into the right format.

 > with(DocumentTools:-Canvas):

 > RunThis := proc( canvas )     local script := Script();     for local m in GetMath(canvas) do         script:-SetActive(script,m);         script:-Command(script,["COPYDOWN","Left","Left","STOREPOS","Right","MARK","SELECTMARK","PAUSE","BS"]);     end do;     script:-ToString(script); end proc:
 > cv := NewCanvas(["Write an expression", ScriptButton("Test the Script", RunThis, position = [500, 50])]):
 > ShareCanvas(cv);

$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}$

Auto-Play Script

A canvas can be created with a script attached so that when the canvas is opened in Maple Learn, the script automatically plays itself.

 > sc := Script(mode='Learn'): SetText(sc,"SubTitle"): SetMath(sc,exp(x),where="below"): SetText(sc,"etc.",where="below"):
 > cv := NewCanvas([ "Main Title", more^2 ], sc ):
 > ShareCanvas(cv);

Next Steps