Git Product home page Git Product logo

embedded-graphics-web-simulator's Introduction

Embedded Graphics Web Simulator

The web simulator is based on embedded-graphics-simulator. This is a sample project demonstrating using a no_std rust-embedded library with Webassembly.

The Web Simulator allows you to use a browser to test embedded-graphics code and run graphics. There is no need to install SDL and its development libraries for running the project. You can see the demo here.

basic demo

For Development

This library is intended to be used in Rust + Webassembly projects. Check the examples which illustrate how to use the library. Look at the examples in the Embedded Graphics Simulator project for inspiration. You can use wasm-pack or trunkand add this library as a dependency

Usage example:

use embedded_graphics_web_simulator::{
    display::WebSimulatorDisplay, output_settings::OutputSettingsBuilder,
};
use wasm_bindgen::prelude::*;
use web_sys::console;

use embedded_graphics::{
    image::Image,
    mono_font::{ascii::FONT_6X9, MonoTextStyle},
    pixelcolor::Rgb565,
    prelude::{Point, Primitive, WebColors},
    primitives::{Circle, PrimitiveStyle},
    text::Text,
    Drawable,
};

use tinybmp::Bmp;

// When the `wee_alloc` feature is enabled, this uses `wee_alloc` as the global
// allocator.
//
// If you don't want to use `wee_alloc`, you can safely delete this.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

// This is like the `main` function, except for JavaScript.
#[wasm_bindgen(start)]
pub fn main_js() -> Result<(), JsValue> {
    // This provides better error messages in debug mode.
    // It's disabled in release mode so it doesn't bloat up the file size.
    #[cfg(debug_assertions)]
    console_error_panic_hook::set_once();

    let document = web_sys::window().unwrap().document().unwrap();

    let output_settings = OutputSettingsBuilder::new()
        .scale(1)
        .pixel_spacing(1)
        .build();
    let mut text_display = WebSimulatorDisplay::new((128, 64), &output_settings, None);
    let mut img_display = WebSimulatorDisplay::new(
        (128, 128),
        &output_settings,
        document.get_element_by_id("custom-container"),
    );

    let style = MonoTextStyle::new(&FONT_6X9, Rgb565::CSS_WHITE);

    if Text::new("Hello, wasm world!", Point::new(10, 30), style)
        .draw(&mut text_display)
        .is_err()
    {
        console::log_1(&"Couldn't draw text".into());
    }

    // Load the BMP image
    let bmp = Bmp::from_slice(include_bytes!("./assets/rust-pride.bmp")).unwrap();
    let image = Image::new(&bmp, Point::new(32, 32));
    if image.draw(&mut img_display).is_err() {
        console::log_1(&"Couldn't draw image".into());
    }

    if Circle::new(Point::new(29, 29), 70)
        .into_styled(PrimitiveStyle::with_stroke(Rgb565::CSS_WHITE, 1))
        .draw(&mut img_display)
        .is_err()
    {
        console::log_1(&"Couldn't draw circle".into());
    }

    img_display.flush().expect("Couldn't update");
    Ok(())
}

How it works

Embedded Graphics Web Simulator implements DrawTarget for the HTML <canvas> element. It will attach a <canvas> either to the document body, or to a user-supplied parent element. To minimize overhead, draw operations need to be explicitly flush()ed whenver you want to see an actual update.

Credits

This project is based on the embedded-graphics library, originally by @jamwaffles

Tools

Thanks to all contributors :)

embedded-graphics-web-simulator's People

Contributors

dependabot[bot] avatar jacobrosenthal avatar rahul-thakoor avatar reecestevens avatar spookyvision avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

embedded-graphics-web-simulator's Issues

display.size changed when output_settings.scale set

Hello!

I use your simulator for testing my drawing function.
The code below draws an image on the center of the display.

fn draw_image<D>(&raw_image: &ImageRawLE<Rgb565>, display: &mut D) -> Result<(), D::Error>
where
    D: DrawTarget<Rgb565>,
{
    use core::convert::TryFrom;
    let (w, h) = display.size().into();
    let (iw, ih) = (raw_image.width(), raw_image.height());
    let (x, y) = (
        i32::try_from(w / 2 - iw / 2).unwrap(),
        i32::try_from(h / 2 - ih / 2).unwrap(),
    );
    let top_left = Point::new(x, y);
    let image = Image::new(&raw_image, top_left);
    image.draw(display)
}

But I got different outputs according to the scale of display.

localhost_8081_

Current implementation of WebSimulatorDisplay looks to change its size by output_settings.

https://github.com/meganetaaan/embedded-graphics-web-simulator/blob/0158a015e0295a62db85c293590f7553fd289b99/examples/basic/src/lib.rs#L40

I think it might be better the display.size returns its original (not scaled) display size?

Thank you!

Time for another release?

Hi! Love your work <3

I'm using embedded-graphics 0.8 but embedded-graphics-web-simulator only supports this in repo. I think it would be nice to put out another release.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.