I'm looking for a quick example on how to get started using the following technologies together:
- HTML5 Canvas
- ReasonML
- ReasonReact: "ReasonReact is a safer, simpler way to build React components, in Reason."
- bs-webapi: Web API bindings for Reason
- React Hooks
To get me started a snippet that does the following would be great:
- Manages a reference to the HTML5 Canvas element elegantly and correctly
- Is a simple react component
- Clears the canvas and draws something
I already have the basic ReasonML React project setup.
Here is a sample that shows one way to put everything together:
Here are some random links I used when learning how to do this. (Adding them here in case they are useful for others too.):
The code has a bit more type declarations than necessary and some
open
statements could be added, but I like my answers a bit on the verbose side for a bit more instructiveness.It should be relatively easy to shorten the code.
The intermediate functions
canvasDimensions
anddrawOnCanvasElement
add a bit of structure to the code in my opinion, but I'm not sure if they make the sample more or less clear for readers or if there would be a more elegant way to work with the canvas size.