summaryrefslogtreecommitdiff
path: root/webhogg/wasm
diff options
context:
space:
mode:
Diffstat (limited to 'webhogg/wasm')
-rw-r--r--webhogg/wasm/index.html4
-rw-r--r--webhogg/wasm/pkg/main.js2
-rw-r--r--webhogg/wasm/src/context/graphics.rs29
-rw-r--r--webhogg/wasm/src/graphics.rs4
4 files changed, 26 insertions, 13 deletions
diff --git a/webhogg/wasm/index.html b/webhogg/wasm/index.html
index b6c8b9b..315d9cb 100644
--- a/webhogg/wasm/index.html
+++ b/webhogg/wasm/index.html
@@ -4,8 +4,8 @@
<meta charset='utf-8'/>
<title>webhogg</title>
</head>
- <body>
- <canvas id='c' width='640' height='360'>
+ <body style='margin: 0; width: 100%; height: 100%; display: absolute;'>
+ <canvas id='c' style='width: 100%; height: 100%; display: absolute; overflow: hidden;'>
your browser is incompetent
</canvas>
<script src='pkg/main.js'></script>
diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js
index 76afa1c..90b3956 100644
--- a/webhogg/wasm/pkg/main.js
+++ b/webhogg/wasm/pkg/main.js
@@ -20,7 +20,7 @@ async function main() {
{ type: 'graphics',
source: source,
canvas: offCanvas,
- dt: 10000 },
+ dt: 16 },
{ type: 'logic',
source: source,
canvas: [],
diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs
index 853e1e1..b94441d 100644
--- a/webhogg/wasm/src/context/graphics.rs
+++ b/webhogg/wasm/src/context/graphics.rs
@@ -4,16 +4,12 @@ use wasm_bindgen::JsCast;
use web_sys::WebGl2RenderingContext as Gl;
pub struct GraphicsContext {
+ gl: Gl,
+ frame_nr: u64,
}
impl GraphicsContext {
pub fn from_canvas(canvas: web_sys::OffscreenCanvas) -> Result<Self, WasmError> {
- /*debug!("canvas object usw.: {:?}", canvas);
- let canvas: web_sys::OffscreenCanvas = js_sys::Reflect::get(&canvas,
- &wasm_bindgen::JsValue::from_str("canvas"))
- .map_err(|_| WasmError::WebGl2ContextCreation(
- format!("canvas object is not defined")))?
- .into();*/
let context = canvas.get_context("webgl2")
.map_err(|_| WasmError::WebGl2ContextCreation(
format!("context cration failed: getContext returned an exception")))?
@@ -23,11 +19,26 @@ impl GraphicsContext {
.dyn_into::<Gl>()
.map_err(|_| WasmError::WebGl2ContextCreation(
format!("context object is not a context")))?;
-
- context.clear_color(0.6, 0.0, 0.6, 1.0);
- context.clear(Gl::COLOR_BUFFER_BIT);
Ok(Self {
+ gl: context,
+ frame_nr: 0,
})
}
+
+ pub fn update(&mut self) -> Result<(), WasmError> {
+ let light = 0.5;
+
+ let a = (self.frame_nr as f32) / 60.0;
+ let a = f32::abs(f32::sin(a));
+ let b = f32::abs(f32::cos(a));
+ let (a, b) = (a * light, b * light);
+
+ self.gl.clear_color(a, light - a, b, 1.0);
+ self.gl.clear(Gl::COLOR_BUFFER_BIT);
+
+ self.frame_nr += 1;
+
+ Ok(())
+ }
}
diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs
index 690b4cd..c4902ae 100644
--- a/webhogg/wasm/src/graphics.rs
+++ b/webhogg/wasm/src/graphics.rs
@@ -16,5 +16,7 @@ pub fn start_graphics(canvas: web_sys::OffscreenCanvas) {
#[wasm_bindgen]
pub fn loop_graphics() {
- debug!("graphics: loopin'");
+ context::get_graphics().update()
+ .map_err(|e| error!("gaphics loop {}", e))
+ .unwrap();
}