GithubHelp home page GithubHelp logo

aptacode / blazorcanvas Goto Github PK

View Code? Open in Web Editor NEW
18.0 2.0 3.0 24.46 MB

A high performance dotnet 7 blazor wrapper around the HTML5 Canvas

License: GNU General Public License v3.0

HTML 5.89% C# 70.65% CSS 3.83% JavaScript 19.63%
blazor blazor-webassembly canvas aptacode graphics drawing performance-blazor-wrapper html5-canvas dotnet dotnet7

blazorcanvas's Introduction

A high performance blazor wrapper around the HTML5 Canvas utilizing unmarshalled JS calls

demo Codacy Badge code metrics nuget last commit License: MIT

NuGet last commit

Usage

RazorComponent.razor

Setup your canvas element

<BlazorCanvas @ref="Canvas">
    <canvas width="100" height="100"></canvas>
</BlazorCanvas>

RazorComponent.razor.cs

Draw to the canvas!

    protected BlazorCanvas Canvas { get; set; }
    protected override async Task OnInitializedAsync()
    {
        using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(15));
        while (await timer.WaitForNextTickAsync())
        {
            await Draw();
        }
    }
 
   protected async Task Draw()
   {
      if (!Canvas.Ready)
      {
         return;
      }
      
      //Clear
      Canvas.ClearRect(0, 0, Width, Height);
      
      //Draw Ellipse
      Canvas.LineWidth(2);
      Canvas.StrokeStyle("blue");
      Canvas.FillStyle("green");
      Canvas.Ellipse(40, 40, 30, 30, (float)Math.PI, 0, 2 * (float)Math.PI);
      Canvas.Stroke();
      Canvas.Fill();
   }

  #endregion

blazorcanvas's People

Contributors

timmoth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

blazorcanvas's Issues

Only a single Canvas on a page supported?

I was moving from the using Blazor.Extensions.Blazor to this one and enjoy the library but stumbled upon a problem. When I add more than one canvas it seems drawing ends up in one and the same canvas.
Except for having multiple canvases at different positions, it is also very useful with layers having different z order for two canvases.

This is the layout. Drawing in Canvas1 actually ends up in Canvas 2.

<div class="canvas1">
    <BlazorCanvas @ref="Canvas1">
        <canvas width="@Width" height="@Height"></canvas>
    </BlazorCanvas>
</div>
<div class="canvas2">
    <BlazorCanvas @ref="Canvas2">
        <canvas width="@Width" height="@Height"></canvas>
    </BlazorCanvas>
</div>

<style>
    .canvas1 {
        position: relative;
        top: 8px;
        width: 100%;
        height: 600px;
        left: 0
    }

    .canvas2 {
        position: relative;
        top: 608px;
        width: 100%;
        left: 0
    }
</style>

image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.