Canvas/ButtonIntro - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

Home : Support : Online Help : Canvas/ButtonIntro

Introducing the Script Button

 

Prime Number Example

Two Buttons

Next Steps

Prime Number Example

In this example, we define (1) a procedure that will be invoked when a button is clicked, and (2) a canvas that contains a title and a description.

When the Button is clicked, the entire canvas is serialized into an XML structure and sent to the FindPrimes procedure.  This procedure loops through all of the math elements in the canvas (extracting them by calling GetMath).  For each element it decides whether the expression is a prime number or not.  To set an annotation, a script is used.  First, you need to set the active element by calling SetActive.  Then the Annotate command is used to add a message next to the existing expression.  When you intend to use this in Maple Learn, the final call must always be to turn the script object into a string of commands that Maple Learn will process.  This is done by returning ToString.

with(DocumentTools:-Canvas):

FindPrimes := proc( canvas )
    local script := Script();
    for local m in GetMath(canvas) do
        script:-SetActive(script,m);
        if m:-math::integer and isprime(m:-math) then
            script:-Annotate(script,"Good job, this is prime");
        else
            script:-Annotate(script,"This is not prime");
        end if;
    end do;
    script:-ToString(script);
end proc:

cv := NewCanvas(["Write Some Prime Numbers Anywhere",
             ScriptButton("Check Your Page", FindPrimes, position = [500, 50])]):

ShowCanvas(cv);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


GetMath and GetElements

In Maple we can explore these interactions by issuing commands directly.  Normally you wouldn't do this, but it may be helpful here to explain what's going on.

First, go ahead and click the "Check Your Page" button above.  All of the code is behind that button, so it will reinitialize this example.

Next, we will execute the GetMath command.  This will return an array of 3 records.  Each record has many fields.  The main one you will want to know about is the :-math field, which contains the value.

with(DocumentTools:-Canvas):

M := GetMath():

numelems(M);

(1.1)

seq(elem:-math,elem=M);

(1.2)

We could extract more than the math elements if we wanted:

A := GetElements():

seq(elem:-type,elem=A);

(1.3)

Later we'll show how to find specific elements using the custom field.


Script

The other part of this example is that of creating a script.  We can create another one here manually, and watch what happens in the above example when we execute each command.  We will use M as computed above.  

sc := Script():   #new script

SetActive(sc,M[1]);   #set the first element as the active one

Annotate(sc,"Here");   #new annotation

SetMath(sc,97);     #replace the entered value

Two Buttons

In this example we will use two different buttons.  We will also put names on the math boxes so we can fetch their values more easily.

with(Grading): with(DocumentTools:-Canvas):

tryAnother := proc( canvas )
    local s := Script();
    SetActive(s,"Q");
    SetMath( s, RandomTools:-GenerateSimilar( [1,-2,3] ) );
    ToString(s);
end proc:

checkAnswer := proc( canvas )
    local a := GetMath('custom'="ANS");
    local q := GetMath('custom'="Q");
    local s := Script();
    SetActive(s,a[1]);
    if max(q[1]:-math) = a[1]:-math then
        Annotate(s,"Correct");
    else
        Annotate(s,"This isn't the maximum value");
    end if;
    ToString(s);
end proc:

cv := NewCanvas( [ "Find the biggest number",
                   Math([6,2,-9],custom="Q"),
                   Math("",custom="ANS",annotation="Answer Here",border=true),
   ScriptButton("Check Answer", checkAnswer, position=[800,90] ),
   ScriptButton("Try Another", tryAnother, position=[800,150])
 ] ):

ShowCanvas(cv,entrybox=false);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

A few comments:

• 

 GetMath() always returns an array of elements even if there is only one.  After q := GetMath(...), use q[1] to refer to the first (and maybe only) entry returned.

• 

 The 'custom' option let's us put an attribute on each math box, the question, labeled custom="Q", and the answer area, labeled custom="ANS".  This makes it simple to use GetMath(custom="Q") to fetch the desired element.

• 

 We used the border=true option because we want to use this in Maple.  Maple Learn won't show a border, but the answer area will be clear.

Of course, this application can be shared and used in the Maple Learn interface as follows:

ShareCanvas(cv);

https://learn.maplesoft.com/#/?d=KUCUJPCIFNIGASAPFKAUBKMTCIAINSNPLTPQILOFBGNKDMAQJQEGATHTPSLTEOMKHJNGALIIBLMTDQCHCNEQGSJTDUGIDGLULLGS

Next Steps

Advanced Overview of Scripting

Return to DocumentTools[Canvas] Overview Page

 


Download Help Document