summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/graphics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'webhogg/wasm/src/context/graphics.rs')
-rw-r--r--webhogg/wasm/src/context/graphics.rs29
1 files changed, 20 insertions, 9 deletions
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(())
+ }
}