This commit is contained in:
2024-06-18 16:27:33 +02:00
parent e1c89f8982
commit ed1f331a85
3 changed files with 61 additions and 0 deletions

5
.gitignore vendored
View File

@@ -138,3 +138,8 @@ Cargo.lock
**/*.rs.bk
*.pdb
# Added by cargo
/target

29
beppe_online/Cargo.toml Normal file
View File

@@ -0,0 +1,29 @@
[package]
name = "beppe_online"
version = "0.1.0"
edition = "2021"
authors = ["Beppe Vanrolleghem beppe.vanrolleghem@gmail.com"]
description = "Testing out wasm"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "0.2"
js-sys = "0.3"
[dependencies.web-sys]
version = "0.3"
features = [
'Document',
'Window',
'HtmlCanvasElement',
'WebGlRenderingContext',
'WebGl2RenderingContext',
'WebGlProgram',
'WebGlShader',
'WebGlBuffer',
'WebGlUniformLocation'
]

27
beppe_online/src/lib.rs Normal file
View File

@@ -0,0 +1,27 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{WebGlRenderingContext, WebGlShader, WebGlProgram};
extern crate js_sys;
pub fn init_webgl_context(canvas_id: &str) -> Result<WebGlRenderingContext, JsValue> {
let document = web_sys::window().unwrap().document().unwrap();
let canvas = document.get_element_by_id(canvas_id).unwrap();
let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::<web_sys::HtmlCanvasElement>()?;
let gl: WebGlRenderingContext = canvas.get_context("webgl")?
.unwrap()
.dyn_into::<WebGlRenderingContext>()
.unwrap();
gl.viewport(
0,
0,
canvas.width().try_into().unwrap(),
canvas.height().try_into().unwrap(),
);
Ok(gl);
}
// https://blog.logrocket.com/implement-webassembly-webgl-viewer-using-rust/#setup-environment
pub fn create_