Hello, how can I access Html APIs via webassembly, is it possible? I’d like to try to use the canvas api like I would in javascript. Right now I’ve got :
public class Program {
public func HelloWorld() {
var canvas = Browser.GetElementById("canvas")
}
}
I can get the canvas element without any problems, but how can I get context out? In javascript I could do this:
var canvas = document.getElementById("canvas")
var context = canvas.getContext("2d")
context.fillStyle = "red"
context.fillRect(0,0, 50,50)
how can I do the same in Fire using swift and webassembly? Thank you for your time
Once you have a javascript object, such as ‘canvas’, that’s a dynamic type, and you can call any method you like on it, and the call will get translated to a JS call at runtime.
So just
public class Program {
public func HelloWorld() {
var canvas = Browser.GetElementById("canvas")
var context = canvas.getContext("2d")
context.fillStyle = "red"
context.fillRect(0,0, 50,50)
}
}
Curious. but getContext() compiles? If so, that sounds like a bug, with dynamic only allowing method calls but not property access, possibly related to Swift. in oxygene, this compiles ok for me:
namespace Module26;
type
[Export]
Program = public class
public
method HelloWorld;
begin
writeLn('HelloWorld');
var el := Browser.GetElementById('helloWorld');
if el = nil then begin
writeLn('Element by ID test is null!');
exit;
end;
var canvas := Browser.GetElementById("canvas");
var context := canvas.getContext("2d");
context.fillStyle := "red";
context.fillRect(0,0, 50,50);
var t2 := Browser.CreateTextNode('Hello from Elements WebAssembly!');
el.appendChild(t2);
end;
end;
end.
Build .2475 is out now, as public/stable release and should have the fix. Our weekly preview builds would also have ad this fix for the past two weeks now.