To Javascript or not to Javascript...that is WebAssembly's question.

I thought we could use a more light tone around here.

So I've been having the dandiest time creating a online game skeleton in WebAssembly. I did an example - the CodePen - using vanilla Javascript. Click, make targets, bonk them out of existence with your mouse or finger.
At the root of every game, that's as technical as it gets. You just add details, write characters, import transparent GIFs containing skinnable weapons and cartoon heroes and villains...
It works, sorta - I can't shoot the objects that are created first in the queue, for example. And the crosshairs targeting object is off, a lot. It's a sketchpad, though, and that's what you do in a sketchpad.
Anyway, I am now translating it into an app using the wasm-bindgen library of Rust. For a low-level coding experience, it feels quite good. Commanding DOM objects is very easy: everything looks like javascript, but it's in strongly typed snake case - get_element_by_id(), set_id(), set_attribute(), on to append_child() and so forth - as nature intended.
I've worked on a canvas manipulation project as well, a riff on this, and it's delightfully straightforward to make complex online visuals happen. I also built this recipe, and drew this with it:


Yes, it works with a drawing pen. Add features like saveability and different "brush" types, and make your own MS Paint ripoff!
So the documentation is excellent. The Rust compiler is fast and offers very good direction when you break something, which at this stage is a lot.
There's strangeness to be encountered, sure...like failing to compile due to "borrowed" variables. This became problematic trying to work with mouse events via the Gloo functionality; when I move the mouse, the crosshairs object can't be added to the playing field. Why? Because the reference to the playing field is booked solid establishing the mouse event, therefore it is now unavailable to add another DOM thing to it. Thank you, call again!
There's bound to be a way to get through that obstacle course!
...but then I figured it would be impossible to align the crosshairs to the cursor in Rust anyway, and just added the mouse-tracking functionality into the head of the HTML page. Good old javascript saves the day!
Is that better or worse? Maybe javascript has better power, speed, and stability with .wasm backing it up? That would be great.
But what about emscripten, the C/C++ rival and O.G. Webassembly superpower? I've played with it and setting it up is a nightmare. It seems to be less compact and have a drastically steeper learning curve than wasm-bindgen. But your mileage may vary.
So it's a learning process. Working with .wasm files in Mozilla's strict and secure environment is another issue, as is home servers (npm is fine, and I understand Python's runserver command is great).
This stuff is only a few years old.
Imagine what we'll be doing in another few years.

I always feel like I'm not smart enough to do any of this. Algorithms and the stuff on HackerRank stymie me. I just know I shouldn't do this for a living!
Then suddenly, somehow, through hours of brooding and beating my head on a wall, something actually happens and I feel redeemed. And then the next obstacle appears...lather, rinse, repeat.
Chop the wood, carry the water.


Shop Gaiam Yoga

Comments

Popular Posts