Canvas is a simple, open-source tool for drawing shapes and images in C#. The goal of this program is to provide an intuitive, easy to use drawing interface for people new to coding in C#, while still allowing higher level manipulation and functionality.
Canvas can be installed directly from NuGet using a package manager.
See Canvas on NuGet here: https://www.nuget.org/packages/PapaBarst.Canvas/
Canvas runs on .NET Framework 4.7.2. Please make sure to set the target framework to this. Additionally, make sure that the output type is "Windows Application". The easiest way to select these settings on startup is to create a new Windows Forms application in visual studio, then delete the pre-generated classes and follow the guide below.
To create an instance of Canvas, begin by extending the CBase class in a class of your own. Next, implement the abstract methods Draw() , DrawOnce(), and Setup() (blank is fine). Finally, add CBase.Main("namespace", "className") to your static void Main or the entry point of the application.
Once setup is complete, you're finally ready to start creating your own program in Canvas! Below are some explanations of the default methods.
The Draw() method executes each tick(whenever the graphics refreshes, controlled by the refresh rate). By default, drawing methods in Draw() will add objects to the first layer above the background with the layer clearing its drawstack every tick. This behavior can be changed by switching the retain property of the targeted layer.
The DrawOnce() method executes once at the beginning of runtime after Setup(). By default, drawing methods in DrawOnce() will add objects to the background layer with objects being retained each tick. This behavior can be changed by switching the retain property of the targeted layer.
The Setup() method executes once at the very beginning of runtime before window creation. In this method you can set window properties such as Width and Height as well as change other properties like rescaling and refreshRate.
Canvas was designed with learning in mind, allowing students and new learners to visualize their code while slowly introducing them to new ideas. A general learning structure using Canvas could be similar to the following:
- Learning basic C# syntax (for loops, if statements, switches) using methods like
RectangleandEllipseto visualize the results - Learning about objects by using a
RectangleCobject and manipulating its properties to move it around the window - Learning about Lists and Arrays by manipulating the drawstack
- Learning about interfaces by looking at the
Drawableinterface - Creating custom shapes by creating custom classes and implementing the
Drawableinterface