summaryrefslogtreecommitdiff
path: root/webhogg/wasm/src/context/graphics.rs
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-13 17:21:44 +0200
committernatrixaeria <janng@gmx.de>2019-06-13 17:21:44 +0200
commit03cedca74429c326fcca176576702873e32a8455 (patch)
tree7fc85a00e009eeed00c3e2525be93ae9382c6b63 /webhogg/wasm/src/context/graphics.rs
parent7884252333cc102a8144e4eafc36f9ef605b1be7 (diff)
Add rendering loop
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(())
+ }
}