[Tech Note] Changing what's displayed on the stack

General Discussions about the API

[Tech Note] Changing what's displayed on the stack

Postby oliver » Mon Jul 02, 2012 8:04 pm

MorphEngine calculators have an HTML5 display.

The stack, with all its items and graphics, is a HTML document. In-stack graphics are typically HTML img or HTML 5 Canvas objects.

You can change what's displayed on the stack, and how it's formatted.

As mentioned in http://naivedesign.com/ND1/ND1_Reference__JavaScript_API.html, there is a display object, which has methods that deal with graphics/normal display and formatting of stack items.

There's a stringForItem method, which returns HTML code for any given stack item. If you use the sanctioned extension mechanism for data types (http://naivedesign.com/ND1/ND1_Reference__Custom_Types.html), this function will call the optional toHTML method defined on your custom type, and there's nothing else you have to do to represent your (arbitrary) object on the stack.

You can also change the display of built-in types.

In principle, you can replace any built-in/stock function of the calculator with your own version, by doing just assigning a new function to it, as in
Code: Select all
stockFunc = myFunc

If you want to add to (or special-case) display, you'd do
Code: Select all
stockFunc_old = stockFunc

before assigning it, and call stockFunc_old in your own implementation.

For illustration, here's how you could change the display of numbers.

For numbers, stringForItem will call stringForNumber.

To do your own special case display of certain numbers, you'd do:
Code: Select all
display.stringForNumber_stock = display.stringForNumber;
display.stringForNumber = function(item, n_digits) { return (someTest(x) ? myProcessing(x) : display.stringForNumber_stock(item, n_digits); };

The following stock functions are defined and can be overwritten or extended by you: stringForComplexNumber, stringForVector, stringForMatrix, stringForDataURL. The display of other types happens through their respective wrapper classes. For example, display of BigInts happens through BigNum.toHTML. You could also overwrite or extend the display of these.

For a concrete example of changing display of numbers, see http://forums.naivedesign.com/viewtopic.php?f=11&t=599.
Site Admin
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to General

Who is online

Users browsing this forum: No registered users and 1 guest