5个版本 (3个破坏性更新)
0.4.0 | 2023年11月11日 |
---|---|
0.3.0 | 2022年1月10日 |
0.2.1 | 2021年11月21日 |
0.1.2 | 2020年4月1日 |
0.1.1 |
|
#167 in WebAssembly
每月 28 次下载
在触摸屏中使用
92KB
159 行
嵌入式图形Web模拟器
该Web模拟器基于embedded-graphics-simulator。这是一个示例项目,演示了如何使用无std rust-embedded库与Webassembly结合使用。
Web模拟器允许您使用浏览器测试嵌入式图形代码和运行图形。无需安装SDL及其开发库即可运行该项目。您可以在这里查看演示。
开发
此库旨在用于Rust + Webassembly项目。查看示例,了解如何使用该库。查看嵌入式图形模拟器项目的示例以获取灵感。您可以使用wasm-pack或trunk并将此库作为依赖项添加
使用示例
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(())
}
工作原理
嵌入式图形Web模拟器实现了HTML DrawTarget
元素。它将一个 <canvas>
元素附加到文档主体,或附加到用户提供的父元素。为了最小化开销,需要在需要看到实际更新时显式地 flush()
绘图操作。
致谢
该项目基于由@jamwaffles创建的嵌入式图形库
工具
感谢所有贡献者 :)
依赖关系
~12MB
~204K SLoC