web_sys::FontFace::load function

Example

This example asynchronously loads a true type Code 39 barcode font using the FontFace load method. After the font is loaded, it is added to the Document to display a barcode in a div element. The load method returns a JavaScript Promise which is wrap in a Rust JsFuture. The await keyword yields control of the current thread until the font is loaded. Finally, the loaded font is added to the list of fonts available in the HTML document element.


    pub async fn web_sys_fontface_load() 
    {
        let window = web_sys::window().expect("global window does not exists");    
        let document = window.document().expect("expecting a document on window");
        let fontface = FontFace::new_with_str("barcodefont", 
                        "url(ConnectCode39.ttf)").unwrap();
        let promise=fontface.load().unwrap();
        let _result= wasm_bindgen_futures::JsFuture::from(promise).await;
        document.fonts().add(&fontface).unwrap();
    }    

HTML div element using the "barcodefont" font-face


        <div style="font-weight: normal; font-style: normal; line-height:normal; 
        font-family: 'barcodefont', sans-serif; font-size: 32px">*12345678*</div>
    

Function


	pub fn load(&self) -> Result<Promise, JsValue>

features/dependencies

Source Code