create account

Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM by tensor

View this thread on: hive.blogpeakd.comecency.com
· @tensor ·
$110.57
Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM
![wasm_logo.png](https://cdn.steemitimages.com/DQmV7viDydF4JeJqAnnkyf9qyRk7MgN8LzsQFfAGZ3rJjZ3/wasm_logo.png)

#### Repository
https://github.com/rust-lang/rust

#### What Will I Learn?

- You will learn how to use Cargo Web
- You will learn about Stdweb
- You will learn about the Rust Web API from Stdweb
- You will learn how to use the js! and console! macros
- You will learn how to make event listeners in Rust
- You will learn how to draw on canvas elements with Stdweb

#### Requirements

##### System Requirements:

- [Visual Studio Code](https://code.visualstudio.com/) or any other Text Editor with Rust language plugins
- The [Rustup](https://rustup.rs/) command line tool and the nightly Rust compiler

##### Required Knowledge
- Some understanding of JavaScript and Node.Js
- Some basic understanding of Rust
- A fair amount of knowledge on web technologies and how they work

##### Resources for Rust
- Rust Website: https://www.rust-lang.org/
- Web Assembly Website: https://webassembly.org/
- Awesome Rust: https://github.com/rust-unofficial/awesome-rust
- Wasm Bindgen Repo: https://github.com/rustwasm/wasm-bindgen

##### Sources
- WASM Logo:  https://webassembly.org/

#### Difficulty

- Intermediate


#### Description

##### Outline and Overview

In this Rust Web Assembly Video tutorial, we take a look at the `stdweb` library for Rust.  This library has certain pros and cons when compared to the `wasm_bindgen` library that we've been using thus far. The `stdweb` library forms the basis for many of the web assembly frameworks and libraries that exist in Rust.  This includes the **Yew framework** that we've looked at in the past.  In this project, we build a bunch of event listeners to capture key and mouse events and we build some functions to allow us to draw on a 2d canvas.


##### Using Stdweb and Cargo Web to Build a Project

The `stdweb` library has many of the same design goals as the `wasm_bindgen` library. How it achieves these goals however, is vastly different.  Stdweb exposes a full native set of Web based APIs for interfacing with web browsers where as with `wasm_bindgen` the developer needs to specify which parts of the web API they want to use.  As a result of this, smaller projects tend to be more resource heavy in `stdweb` unless you strip out unnecessary elements of the API.  `Stdweb` has many nice features built directly on top of it; this includes the ability to write JavaScript code inside of the Rust application using the `js!` macro and the ability to access various top level objects using the `console!` macro.

![js!.png](https://cdn.steemitimages.com/DQmNuEH4QZY9vD1npySNH5bif8S9wVRGbTpWHLvTHQHD3Ma/js!.png)

In our example, we make use of the `js!` macro to write a simple "hello world" application and we also make use of the `console!` macro to be able to streamline access to the console object.  These macros help the library maintain the Rust language feel while still providing the ability to build flexible APIs and projects.  You can see how the lines in the `js!` macro are structured with a syntax that is like any normal JavaScript application. 


##### Making Event Listeners for the Mouse and Keyboard

This library also exposes many event based types which we can use to create listeners.  This includes `IEvent`, `KeyDownEvent`, `KeyUpEvent`, `MouseDownEvent`, `MouseMoveEvent`, `MouseUpEvent` as well as many others.  These types and functions follow the conventions of their JavaScript counterparts.  This means that building applications using the `stdweb` library is very familiar for developers who are well versed in JavaScript and front-end development without giving up the advantages of using Rust as a language.      

![rust_event.png](https://cdn.steemitimages.com/DQmaH9j8LyeqydsH2swf9VjHAtP6mxAdBSz3t4A1PJ62T1V/rust_event.png)

In this sample project, we add event listeners to the `window` object. We are able to gain access to this `window` object through the web module from inside of the `stdweb` library.  We add the event listeners by using closures and first class functions.  In this application, we create event listeners for key up and down, mouse up and down and mouse movement.  Notice how similar this syntax is to a typical JavaScript program. 


##### Accessing HTML Canvas from Rust

Along with the ability to create event listeners and use macros to access various parts of the web API; `stdweb` gives developers the ability to gain access to the canvas API.  This includes the ability to access the canvas context with a `CanvasRenderingContext2d` type and a `CanvasRenderingContext3d` type. The canvas itself can be expressed using the `CanvasElement` type as well.  

![rust_canvas.png](https://cdn.steemitimages.com/DQmWNmXHzeZT7pQFR2B1rZFx2NDxSJruoFKMzGBzKMPeFZA/rust_canvas.png)

In our project, we use this canvas API to draw multiple boxes.  We first draw three boxes using a `draw_box` function and the context which we access using an HTML selector.  We are then able to also attach this canvas `draw_box` function to our `MouseMoveEvent` event listener so that when a user moves their mouse inside of the Canvas element on the HTML document, it draws small squares under the mouse pointer.  


The source code for this project can be found [here](https://github.com/tensor-programming/rust_wasm_stdweb_example)



#### Video Tutorial

<iframe width="560" height="315" src="https://www.youtube.com/embed/kC3ExdRTzsM" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

#### Curriculum
- [Intro to Rust Web Assembly with Rust's Wasm Bindgen Library](https://steemit.com/utopian-io/@tensor/intro-to-rust-web-assembly-with-rust-s-wasm-bindgen-library)
- [Rust Web Assembly - Building a Simple Markdown Parser with Rust's Wasm Bindgen](https://steemit.com/utopian-io/@tensor/rust-web-assembly-building-a-simple-markdown-parser-with-rust-s-wasm-bindgen)

#### Proof of Work Done
https://github.com/tensor-programming/
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 6 others
πŸ‘Ž  , ,
properties (23)
authortensor
permlinkrust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm
categoryutopian-io
json_metadata{"tags":["utopian-io","video-tutorials","steemstem","science","technology"],"image":["https://cdn.steemitimages.com/DQmV7viDydF4JeJqAnnkyf9qyRk7MgN8LzsQFfAGZ3rJjZ3/wasm_logo.png","https://cdn.steemitimages.com/DQmNuEH4QZY9vD1npySNH5bif8S9wVRGbTpWHLvTHQHD3Ma/js!.png","https://cdn.steemitimages.com/DQmaH9j8LyeqydsH2swf9VjHAtP6mxAdBSz3t4A1PJ62T1V/rust_event.png","https://cdn.steemitimages.com/DQmWNmXHzeZT7pQFR2B1rZFx2NDxSJruoFKMzGBzKMPeFZA/rust_canvas.png","https://img.youtube.com/vi/kC3ExdRTzsM/0.jpg"],"links":["https://github.com/rust-lang/rust","https://code.visualstudio.com/","https://rustup.rs/","https://www.rust-lang.org/","https://webassembly.org/","https://github.com/rust-unofficial/awesome-rust","https://github.com/rustwasm/wasm-bindgen","https://github.com/tensor-programming/rust_wasm_stdweb_example","https://www.youtube.com/embed/kC3ExdRTzsM","https://steemit.com/utopian-io/@tensor/intro-to-rust-web-assembly-with-rust-s-wasm-bindgen-library","https://steemit.com/utopian-io/@tensor/rust-web-assembly-building-a-simple-markdown-parser-with-rust-s-wasm-bindgen","https://github.com/tensor-programming/"],"app":"steemit/0.1","format":"markdown"}
created2018-07-13 02:52:51
last_update2018-07-13 02:52:51
depth0
children7
last_payout2018-07-20 02:52:51
cashout_time1969-12-31 23:59:59
total_payout_value84.008 HBD
curator_payout_value26.558 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,964
author_reputation87,856,203,149,624
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,480,855
net_rshares48,786,076,619,770
author_curate_reward""
vote details (73)
@buckydurddle ·
$0.06
Excellent video tutorial @tensor. I liked the way you introduce concepts then give a little side-note about its function and uses. Almost like a guided nature tour. Your practical knowledge of the bird and plant life is outstanding and your ability to put these creatures to work for you is equally outstanding. 

Thank you for the great video tutorial. 

Cheers, 
Bucky


Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/9/121211312).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , ,
properties (23)
authorbuckydurddle
permlinkre-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t213000085z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"users":["tensor"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/9/121211312","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-07-13 21:30:00
last_update2018-07-13 21:30:00
depth1
children1
last_payout2018-07-20 21:30:00
cashout_time1969-12-31 23:59:59
total_payout_value0.055 HBD
curator_payout_value0.006 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length864
author_reputation101,965,608,464,038
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,581,661
net_rshares27,663,859,831
author_curate_reward""
vote details (3)
@tensor ·
Again, thanks for reviewing my contributions.  I like the metaphor haha.
πŸ‘  
properties (23)
authortensor
permlinkre-buckydurddle-re-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t214053732z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-07-13 21:40:51
last_update2018-07-13 21:40:51
depth2
children0
last_payout2018-07-20 21:40:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length72
author_reputation87,856,203,149,624
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,582,386
net_rshares0
author_curate_reward""
vote details (1)
@drsensor · (edited)
Curious how long it takes to compile when someone building full scale web site/app purely in Rust.
Ah yes, have you measure the bundle size of JS file as a result of compile using wasm-bindgen and/or stdweb? I found many garbage when compiling without wasm-bindgen and stdweb (pure wasm, no js binding). Also found out the bundle size can be reduced significantly when enabling LTO. 
properties (22)
authordrsensor
permlinkre-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t073108158z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.2","format":"markdown","tags":["utopian-io"],"users":[],"links":[],"image":[]}
created2018-07-13 07:31:12
last_update2018-07-13 07:33:18
depth1
children2
last_payout2018-07-20 07:31:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length383
author_reputation17,679,210,755,117
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,502,157
net_rshares0
@tensor · (edited)
There are quite a few tools that you can use to vastly reduce the bundle size with both libraries.  In fact, you can just pull out most of the Rust functionality that you don't need and you can get it down to <100 kb with even very large projects.  Of course this doesn't account for some of the more wasm specific tools which can reduce the size much further.  

Compilation timing in Rust is average id say when compared to other AoT based languages.  Its not as fast as Go for instance, but it is much faster then Java.

When I start to do some larger projects with WASM, I will start showing off some of the techniques and tools to optimize performance and bundle size of WASM projects built from Rust.  I am also considering doing some computation benchmark tutorials but we will see about that one.
πŸ‘  
properties (23)
authortensor
permlinkre-drsensor-re-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t074814699z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-07-13 07:48:12
last_update2018-07-13 07:50:42
depth2
children1
last_payout2018-07-20 07:48:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length804
author_reputation87,856,203,149,624
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,503,497
net_rshares3,160,019,910
author_curate_reward""
vote details (1)
@drsensor · (edited)
Nice! For the tools to reduce the bundle size, I have tried using [wasm-gc](https://github.com/alexcrichton/wasm-gc) and [Binaryen][]. The most intriguing (and the best) one is Binaryen that [I can't still figure out which configuration is the best for some use case](https://github.com/DrSensor/binaryen-loader/issues/2).

[Binaryen]: https://github.com/WebAssembly/binaryen
properties (22)
authordrsensor
permlinkre-tensor-re-drsensor-re-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t151002338z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.2","format":"markdown","tags":["utopian-io"],"users":[],"links":["https://github.com/alexcrichton/wasm-gc","https://github.com/WebAssembly/binaryen","https://github.com/DrSensor/binaryen-loader/issues/2"],"image":[]}
created2018-07-13 15:10:03
last_update2018-07-14 03:05:00
depth3
children0
last_payout2018-07-20 15:10:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length375
author_reputation17,679,210,755,117
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,545,631
net_rshares0
@kamsamita ·
fossbot voter comment
Get More Upvote - FREE 

https://i.imgsafe.org/7f/7f306de28e.png
πŸ‘Ž  
properties (23)
authorkamsamita
permlinkre-tensor-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180713t031208233z
categoryutopian-io
json_metadata{}
created2018-07-13 03:12:09
last_update2018-07-13 03:12:09
depth1
children0
last_payout2018-07-20 03:12:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length68
author_reputation-1,178,327,544,156
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,482,335
net_rshares-314,022,667,396
author_curate_reward""
vote details (1)
@utopian-io ·
Hey @tensor
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlinkre-rust-web-assembly-using-stdweb-to-build-a-client-side-application-with-rust-and-wasm-20180714t155508z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-07-14 15:55:09
last_update2018-07-14 15:55:09
depth1
children0
last_payout2018-07-21 15:55:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length298
author_reputation152,955,367,999,756
root_title"Rust Web Assembly - Using Stdweb to Build a Client side Application with Rust and WASM"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id64,662,917
net_rshares5,838,392,836
author_curate_reward""
vote details (1)