Bringing quick math-typesetting rendering to the canvas
Try it yourself
To see a basic list of supported commands, it's recommended to look in
test/selenium/expressions.yml. If you want to see their rendered results, you can do so by either running the tests, or by copy/pasting into the input field when the server is running.
This project is built on top of the open-source project KaTeX. You can find a full list of their supported functions here: https://khan.github.io/KaTeX/function-support.html. Please not that not all are supported yet.
CanvasLatex is written in a way which should support any Canvas based renderer. The NodeBuilder creates "VirtualCanvasNodes" which are renderer agnostic models.
Currently supported renderers include:
Future renderer support could include:
ThreeJS (3D rendering?)
const widget = new Widget(latex: string, options: object)
||The base font size for the latex rendering. Note: This may be larger/smaller depending on the specific latex (i.e. an exponent will render in a smaller font.|
||If true the math will be rendered in display mode, which will put the math in display style (so \int, \frac and \sum are large, for example)|
||Displays a rectangle around the Widget's bounds.|
||Allows the default text color to be changed by specifying a string (e.g. yellow) or a hex value.|
||The default size index for the fonts. The size mapping is as follows: [ null, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.44, 1.728, 2.074, 2.488 ]. The numbers represent the scale factors applied to fonts. A number of 6 maps to 1.0, a number of 7 maps to 1.2, etc.|
||The default alignment when using newlines (\newline, \, etc.). Options are 'center', 'left', 'right'.|
||Returns a flattened 1d array of all createjs.DisplayObject's with the specified className. For example, when calling getObjectsByClass('a') with the latex:
||Returns a 2d array of createjs.DisplayObject's separated by their className. For example, when calling getObjectsByClassSeparated('a') with the latex:
||Returns the latex value as a string|
||Re-renders with the given latex value|
||Gets a full list of classes applied to the widget|
||Gets the distance to the baseline from the top of the widget|
Loading the Fonts
Since canvas doesn't load fonts automatically, you can use the
demo/demo.js for an example. Additionally you'll need to make sure the fonts are available via the css. We leverage Katex's CSS file as it contains the fonts we need. You can use their CDN here: https://cdnjs.com/libraries/KaTeX. Or, you can include the fonts locally.
CanvasLatex is licensed under the MIT License. https://opensource.org/licenses/MIT