Canvas
Drawing to the screen using the Canvas API
nx.js does not implement a full DOM for rendering content to the screen. Instead, it offers the more low-level Canvas API which allows your application to draw to the screen.
The global screen
object is used to create a CanvasRenderingContext2D
instance, at which point the app is in canvas rendering mode.
You should avoid using any console
printing functions when in canvas rendering mode.
Doing so will change the app back to console rendering mode.
Example
Let's draw a simple house with the Canvas API:
This example was adapted from the MDN Canvas tutorial.
Using React
The Canvas API is a low-level API, and it's not very well suited to building complex UIs. However, it can be used to build simple UIs, and it's also possible to use React with the Canvas API.
To use React with the Canvas API, you'll need to use a library like
react-canvas
.
Here's an example of a simple React app that uses the Canvas API: