curiosity-ai / h5

🚀 The next generation C# to JavaScript compiler
https://github.com/curiosity-ai/h5
Apache License 2.0
210 stars 30 forks source link

How do I access Bridge.Html5.Document.GetElementById? #78

Closed Riccsson closed 1 year ago

Riccsson commented 1 year ago

canvas = Bridge.Html5.Document.GetElementById(canvasId);

How do I use this line of code in H5?

Riccsson commented 1 year ago

Sorry, i found it: canvas = H5.Core.dom.document.getElementById<H5.Core.dom.HTMLCanvasElement>(canvasId);

theolivenbaum commented 1 year ago

No worries! Usually I add to the usings list this import, so you can use it directly without having to prefix with the whole namespace:

using static H5.Core.dom;
//...
var canvas = document.getElementById<HTMLCanvasElement>(canvasId);

//Just that you know, another useful method when handling DOM objects is the .As<T>(), it performs a "fake cast" to the final type:
var canvas = document.getElementById(canvasId).As<HTMLCanvasElement>();
ghost commented 1 year ago

canvas = Bridge.Html5.Document.GetElementById(canvasId);

How do I use this line of code in H5?

@Riccsson

HTMLCanvasElement element = Document.GetElementById<HTMLCanvasElement>("id");

You need to add h5 nuget package and namespace H5.Html5; to project and it will work;

Riccsson commented 1 year ago

I didnt find namespace H5.Html5. So I use H5.Core.dom instead. The .As() was a good thing 👍 Thank you

ghost commented 1 year ago

I didnt find namespace H5.Html5. So I use H5.Core.dom instead. The .As() was a good thing +1 Thank you

@Riccsson

You are right, I forgot that I ported it from Bridge.Frameworks to separate library. So it is available for me as usual.

P.S. I use own fork of H5 built with .NET 6 instead of .NET 7 as well.