HTLLeonding.Utility.SimpleDrawing 3.1.1

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package HTLLeonding.Utility.SimpleDrawing --version 3.1.1                
NuGet\Install-Package HTLLeonding.Utility.SimpleDrawing -Version 3.1.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HTLLeonding.Utility.SimpleDrawing" Version="3.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HTLLeonding.Utility.SimpleDrawing --version 3.1.1                
#r "nuget: HTLLeonding.Utility.SimpleDrawing, 3.1.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HTLLeonding.Utility.SimpleDrawing as a Cake Addin
#addin nuget:?package=HTLLeonding.Utility.SimpleDrawing&version=3.1.1

// Install HTLLeonding.Utility.SimpleDrawing as a Cake Tool
#tool nuget:?package=HTLLeonding.Utility.SimpleDrawing&version=3.1.1                

SimpleDrawing - Cross Platform

  • Allows for the simple drawing of basic shapes on a canvas
  • Renders the canvas in a window
  • Works on multiple platforms (tested Windows, Linux; should also work on Mac)
  • Based on SkiaSharp
  • User click events are registered

This library is meant for simple applications, so only basic functions are available to keep the API easy to use.

Sample Usage

using Avalonia.Input;
using Avalonia.Media;
using SimpleDrawing;
using SimpleDrawing.Core;

LeoCanvas.Init(Run, 400, 400, clickAction: DrawCircleOnClick);

return;

static async void Run()
{
    try
    {
        LeoCanvas.DrawLine(new(100, 100), new(300, 300));
        LeoCanvas.DrawLine(new(300, 100), new(100, 300), thickness: 4, color: Brushes.Green);
        LeoCanvas.DrawRectangle(new(50, 50), new(100, 80), lineColor: Brushes.Red);
        LeoCanvas.DrawRectangle(new(325, 325), new(375, 375), fillColor: Brushes.Azure);
        LeoCanvas.DrawEllipse(new(200, 300), 50, 75,
                              lineColor: Brushes.Firebrick, fillColor: Brushes.Firebrick);
        LeoCanvas.DrawCircle(new(350, 75), 40, lineThickness: 2.5D,
                             lineColor: Brushes.BlueViolet, fillColor: Brushes.Gold);
        LeoCanvas.DrawText(new(25, 350), "Hello World", 24, Brushes.Lime);

        LeoCanvas.Render();

        LeoCanvas.DrawLine(new(200, 25), new(200, 375), 8);
        LeoCanvas.Render();

        await Task.Delay(TimeSpan.FromSeconds(2));
        LeoCanvas.Clear();
        var x = 50;
        for (var i = 0; i < 10; i++)
        {
            LeoCanvas.DrawLine(new(x, 25), new(x, 375), 8);
            x += 25;
            LeoCanvas.Render();
            await Task.Delay(TimeSpan.FromSeconds(1));
        }

        var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Data/logo.png");
        LeoCanvas.DrawImageAtLocation(imagePath, new(50, 100), new(350, 175));
        LeoCanvas.Render();

        Console.Write("Press any key to exit...");
        Console.ReadKey();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

static void DrawCircleOnClick(ClickEvent clickEvent)
{
    var radius = clickEvent.Button switch
    {
        MouseButton.Left => 25,
        MouseButton.Right => 50,
        _ => 10
    };
    LeoCanvas.DrawCircle(clickEvent.ClickedPoint, radius);
    LeoCanvas.Render();
}

Main Method

To also support macOS (which requires the UI thread to be the main thread), it is necessary to pass the actual main method to the LeoCanvas.Init method. It will be run in a background thread, hiding most of the complexity from the students, but this method reference is a technical necessity.

Click Events

An Action can be passed to the Init function to register a click handler which will receive the location the user clicked within the canvas as well as information if the left or right mouse button was used.

LeoCanvas.Init(Run, 600, 600, clickAction: DrawCircleOnClick);

static void DrawCircleOnClick(ClickEvent @event)
{
    var radius = @event.Button switch
    {
        MouseButton.Left => 25,
        MouseButton.Right => 50,
        _ => 10
    };
    LeoCanvas.DrawCircle(@event.ClickedPoint, radius);
    LeoCanvas.Render();
}

Rendering Images

Images can be rendered on the canvas using the DrawImageAtLocation function. The image path should be passed as the first parameter and the location as the second parameter.

It is also possible to directly pass an IImage to an overload. Images can be (pre)loaded via the TryLoadImage method to avoid loading them from the disk every time.

This library should support all image formats handled by SkiaSharp (e.g., PNG, JPEG, BMP).

var imagePath = Path.Combine(Directory.GetCurrentDirectory(), "Data/logo.png");
LeoCanvas.DrawImageAtLocation(imagePath, new(50, 100), new (350, 175));

Scale of the images is not checked, make sure to use a proper target rectangle.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HTLLeonding.Utility.SimpleDrawing:

Package Downloads
HTLLeonding.Utility.Turtle

Allows ordering a turtle around a beach to draw simple shapes

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.2.2 206 9/5/2024
3.2.1 156 8/22/2024
3.2.0 117 8/22/2024
3.1.2 121 8/22/2024
3.1.1 93 6/6/2024
3.1.0 107 5/21/2024
3.0.1 119 4/30/2024
3.0.0 113 4/30/2024
2.0.0 108 4/26/2024
1.0.0 114 4/26/2024
1.0.0-preview9 91 4/26/2024
1.0.0-preview8 100 4/26/2024
1.0.0-preview7 208 3/9/2023
1.0.0-preview6 119 3/9/2023
1.0.0-preview5 115 3/9/2023
1.0.0-preview4 133 3/9/2023
1.0.0-preview3 135 3/8/2023
1.0.0-preview2 128 3/8/2023
1.0.0-preview1 137 3/5/2023