DocumentTools/Canvas/Script - Maple Help

DocumentTools[Canvas]

 Script
 script canvas operations

 Calling Sequence Script() Script(s, options)

Parameters

 s - (optional) string, list(string), or existing Script object mode = ... - (optional) Maple or Learn size = ... - (optional) [posint,posint] size of canvas canvas = ... - (option) existing canvas

Description

 • The Script command creates a new Script object which can be used to perform operations on a Canvas.  A "canvas" is both (1) a document template where math can be inserted and acted upon by button clicks within Maple, and (2) the document environment used by Maple Learn.
 • When used within Maple, each script method, when invoked, immediately applies that action to the current environment.  For example, calling SetMath(x^2) will cause x^2 to appear in the currently active math container.  At the same time it records the XML script action used by Maple Learn required to invoke the same operation using Maple Learn's scripting language.
 • A script can be used to populate a canvas, or it can be used to invoke changes to the canvas when a button is clicked.  In the Maple Learn environment, when a button is clicked, the current state of the canvas is sent to a Cloud service to run the related command action, which, can apply Script commands, and finally use ToString to extract the script XML and return it back to Maple Learn, where it will perform those script actions in sequence.
 • The default mode depends on which environment you are running the command in.   Use the mode=Learn option to force the operation to generate Learn script commands even though you are in Maple.
 • In Maple Learn, the canvas is effectively infinite in size, and has pixel resolution.  The canvas that is simulated in Maple is a finite grid of 12 rows and 2 columns of hidden containers that are revealed and populated by Script commands.  Use the size=[nrows,ncolumns] to specify an underlying grid with more elements.
 • When an existing script is given as the first argument to the Script constructor, it will copy the XML Maple Learn script into itself.
 • The canvas=c option can be used to link a script to an existing canvas.

List of Script Methods

 The following is a list of available methods:

 annotate math append a raw script command cursor movement script command add highlights to a math canvas element remove highlights from a math canvas element set the active canvas element set the contents of a math canvas element set canvas or canvas element properties set the contents of a text canvas element convert a script object to a XML string

Examples

Static Content in a Canvas

 • In this section we create a canvas with text, and math in columns.
 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Canvas}\right):$
 > $\mathrm{cv}≔\mathrm{NewCanvas}\left(\left["A Sample Maple Learn Document",\left["Some Text",E=M{C}^{2}\right],\left[E,M,{C}^{2}\right]\right]\right):$
 > $\mathrm{ShowCanvas}\left(\mathrm{cv}\right)$

Script to Populate a Canvas

 • In this section we create a blank canvas, then add text, and math via script commands.
 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Canvas}\right):$
 > $\mathrm{cv}≔\mathrm{NewCanvas}\left(\left[\right]\right):$
 > $\mathrm{ShowCanvas}\left(\mathrm{cv},\mathrm{size}=\left[6,3\right]\right)$
 > $s≔\mathrm{Script}\left(\right):$
 > $s:-\mathrm{SetText}\left(s,"A Sample Maple Learn Document",\mathrm{where}=\left[30,30\right],\mathrm{container}="textbox"\right)$
 > $s:-\mathrm{SetText}\left(s,"Some Text",\mathrm{where}="below"\right)$
 > $s:-\mathrm{SetMath}\left(s,E=M{C}^{2},\mathrm{where}="right"\right)$
 > $s:-\mathrm{SetActive}\left(s,"1"\right)$
 > $s:-\mathrm{SetMath}\left(s,E,\mathrm{where}="below"\right)$
 > $s:-\mathrm{SetMath}\left(s,M,\mathrm{where}="right"\right)$
 > $s:-\mathrm{SetMath}\left(s,{C}^{2},\mathrm{where}="right"\right)$
 • At this point the canvas in this section is the same as the one in the previous section. We can embed the script into a new canvas and watch the actions animate in sequence when deployed to Maple Learn.
 > $\mathrm{cv2}≔\mathrm{NewCanvas}\left(\left[\right],s\right):$
 > $\mathrm{ShareCanvas}\left(\mathrm{cv2}\right)$

Script Button

 • In this section we create a canvas with a procedure that runs a script when a button is clicked.
 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Canvas}\right):$
 > Check := proc( canvas )    local script := Script();    for local m in GetMath(canvas) do        script:-SetActive(script,m);        if m:-math = 2 then            script:-Annotate(script,"Good job, this is correct");        else            script:-Annotate(script,"This is not the answer");        end if;    end do;    script:-ToString(script); end proc:
 > $\mathrm{cv}≔\mathrm{NewCanvas}\left(\left[\mathrm{Text}\left("Calculate %1:",\mathrm{%+}\left(1,1\right)\right),\mathrm{Math}\left(""\right),\mathrm{ScriptButton}\left("Check",\mathrm{Check}\right)\right]\right):$
 > $\mathrm{ShowCanvas}\left(\mathrm{cv}\right)$

Raw Script Commands

 • In this section we generate a script for use in Maple Learn that applies raw script commands to an entered expression.
 > $\mathrm{with}\left(\mathrm{DocumentTools}:-\mathrm{Canvas}\right):$
 > 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:
 > $\mathrm{cv}≔\mathrm{NewCanvas}\left(\left["Write an expression",\mathrm{ScriptButton}\left("Test the Script",\mathrm{RunThis},\mathrm{position}=\left[500,50\right]\right)\right]\right):$
 > $\mathrm{ShareCanvas}\left(\mathrm{cv}\right)$
 insertdirect, content = "https://learn.maplesoft.com/#/?d=DRINCIBFFPMSCJDRBKNOPOCUMGNLBLFUMSAGPHIMHUIIDUFNGPFTFGGKDLERCSLNDQGKOHFGLTIUFKHFBIJTNMNKBLILOOGOOIGQ", state = "", minimal = true

Compatibility

 • The DocumentTools[Canvas][Script] command was introduced in Maple 2021.