BlazorExtensions/Canvas

I need the canvas bounding client rectangle

dannypike opened this issue · 2 comments

I want to be able to call getBoundingClientRect() on the <canvas>, so that I can convert mouse coordinates into client coordinates. My instinct is to use JS Interop to do that, but the Id property is not public, so I am thinking that I will have to wrap the BECanvas in a <div> with an id that I can control, and then use look that up and get its child control.

This feels a bit convoluted to me. Is there a better way?

Perhaps you could make Id a public property?

You could write an extension to get the Id of the canvas:

public static class BECanvasExtensions
{
	public static string GetHtmlId(this BECanvasComponent beCanvas)
	{
		var field = typeof(BECanvasComponent).GetField("Id", BindingFlags.Instance | BindingFlags.NonPublic);
		return field == null ? null : field.GetValue(beCanvas) as string;
	}
}

Thank you. Unfortunately, I've moved on to another project now, but I will try to remember that for next time!