If we want to draw something on canvas we need to get 2D context of it. I have a canvas element in index.html of project:
<body>
<canvas id="canv" width="200" height="200"></canvas>
</body>
So now I need to get access for that element, ok, let's write code:
var cans:CanvasElement = Browser.document.getElementById("canv");
and in compile phase I get error:
src/Main.hx:32: characters 2-78 : js.html.Element should be js.html.CanvasElement
But if we use unsafe casting, already will be fine:
var cans:CanvasElement = cast Browser.document.getElementById("canv");
And everything works fine, I get access and can get 2D context or make some settings like:
cans.width = cans.height = 800;
cans.style.backgroundColor = 'rgba(158, 167, 184, 0.4)';
Yes, I know, "if it works - don't fix", and I roughly understand that everything is normal, in principle I get what I need, when get cast, but can anybody explain this process for me? What does that mean - js.html.Element should be js.html.CanvasElement? I'm only started Haxe learning (and programming in particular), I'm glad, that I can do workable things, but I want to know, why it works when it works and why not when it doesn't.