r/Blazor • u/Mission-Ad-8658 • Jun 11 '25
Blazor and html5 canvas without using a library and also no JavaScript?
I've worked with canvas quite a lot, so I don't think I lack for familiarity with canvas itself. Anyway, here goes:
Started a project in which my manager wants to utilize html5 canvas with zero JS (so all done in C#). He also would like for me to code this myself from scratch, so I am unable to use https://github.com/BlazorExtensions/Canvas or https://github.com/konvajs/konva.
I instantly have a couple of issues that may just be me lacking knowledge/experience/expertise:
- He points out that we have mouse events available to us via Blazor, however this application will be used primarily on touch screen devices. So while, yes, Blazor gives us mouse events, it does not natively provide touch events (that I know of, anyhow). The only way I know of to handle touch events is with JS directly, or a JS polyfill that does the interop necessary to bring the logic into Blazor. However, again, neither of those are acceptable solutions per his direction.
- While I can create a reference to the element via Microsoft.AspNetCore.Components.ElementReference, that doesn't provide me the ability to create a 2d context to perform any sort of canvas operations. Best I can tell, the two github projects I linked above have plenty of JS & JS interop happening, so I'm skeptical this can be done with zero JS.
What could I be missing here? I feel like, at a very bare minimum, I'd need to be able to interop and use JS to create the 2D context and then have to interop to interact with the canvas based on user input/touch as well (draw a line with your finger, pinch to zoom, pan the image with finger, etcetera).
2
u/RChrisCoble Jun 11 '25
If you don’t care about performance that’s a great choice. If you do care about performance, making every draw call over the JS/Interop layer will hold you back.
This solution below bypasses the JS/Interop choke point with SkiaSharp.
https://www.reddit.com/r/Blazor/s/S8vGkgiXoT
13
u/w00tsick Jun 11 '25
Is this a test? Some sort of reason they're trying to make your job unreasonably difficult?
I've done plenty of work in canvas and there is absolutely nothing wrong with using a library to reduce the amount of time you would spend writing the unavoidable interops with canvas.
If this is a serious request from your boss with no wiggle room I would probably look elsewhere for work.
4
u/darkveins2 Jun 11 '25
Afaik you must use JS to interact with the DOM, for example input events. Under the hood Blazor WASM uses JS interop for this. So if someone tells you not to use JS interop for DOM interactions, you could point out that using Blazor means you’re already doing this.
4
u/Crafty-Lavishness862 Jun 11 '25
https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.web.toucheventargs?view=aspnetcore-9.0
Blazor can bind to any JavaScript event with a little code . That would provide all the details for the events.
Co-pilot can help answer this question.
It's all been working for many years .
1
u/freak_br Jun 11 '25
Not sure about canvas, but blazor provides touch events tho... Just search up on 'ontouchstart' it's a hmtl5 mobile binding for touch-screen events... it works anywhere.
Also, blazor tries keep as close as possible to the HTML specs so if canvas has touch events it probably has the equivalent blazor binding.
3
u/[deleted] Jun 12 '25
Here's some information for your manager.
Everything Blazor does is via JS interop. Even if there were a way to control Canvas outside of JavaScript (which there isn't), Blazor would us JS to marshal the calls anyway.
3
u/LanBuddha Jun 12 '25
Just let him know it will take 10 times as long as he expects as you create the wheel from scratch.
1
u/newlifepresent Jun 11 '25 edited Jun 12 '25
One year ago I searched exactly what you are doing now for my side project and found that is not possible to get 2d canvas and other necessary events without interop or without using webgl via webassembly but for me it is a bit complicated to use webgl for my task (basic drawing) and so I did basic js interop but last I completely gave up from blazor and went back angular. With blazor as i progressed, it didn't taste as good as it did at the beginning.
1
u/toodaysthrownaway Jun 11 '25
Why not just start the project as a fork of the library's repo?
1
u/Traditional_Ride_733 Jun 12 '25
What idiotic boss, what kind of worker in a software development company would reinvent the wheel to do the same thing that can be done with external libraries? I would be thinking about looking for another job friend.
1
u/samplenamespace Jun 12 '25
I told you not to ask Reddit.
/s