Why Declarativas?
Comparisons
Summary
Here I will show a comparison between raw canvas, declarativas without jsx, and with jsx, using a function that will render a rotating 20x20 square centered on 100,100 in the canvas.
Declarativas Goals
One thing to note is declarativas isn't meant to make canvas programs shorter. While custom components can be made to manage multiple canvas calls, the main goal is to make canvas more approachable to the React generation of javascript developers. Thinking about canvas in terms of components makes things easier to manage, and using a virtual canvas operations (similar to virtual dom), it is far easier to test your canvas application and have predictable output, as well as build logical hierarchies.
Raw Canvas
Pros
No extra learning on top of the canvas API
Shortest program
Cons
No meaningful relationship between calls
save
does not imply a laterrestore
A lack of hierarchy makes it non-obvious what mutations to the canvas state require the save and restore
No community consensus on how to build drawing components
Arguments to some functions are non-obvious
Declarativas without JSX
Pros
Hierarchy of drawing operations
Clear distinction of drawing code from logic
Named parameters
Declarative components can make the code easier to debug and test
Cons
The
createElement
(aliased asc
) function can look awkwardMore code
Adds dependency
Could feel foreign
Declarativas with JSX
Pros
Hierarchy of drawing operations
Clear distinction of drawing code from logic
Named parameters
Declarative components can make the code easier to debug and test
Higher familiarity to the code (from the perspective of React) because of the JSX transform used for operations
Cons
More code
Adds dependency
Could feel foreign
TL;DR
If you want a very lean bare-metal canvas experience, there is nothing distinctly wrong with the javascript API for CanvasContext2d. If you want a more manageable code-base that uses a lot of canvas, I do think declarativas has a well defined use. I personally enjoy the way I can draw with declarativas, and hope you would try it and see the difference.
Last updated