From 5de862b4d175578ed67e177d482ac31bf811d486 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Thu, 13 Jun 2019 01:34:35 +0200 Subject: Create a WebGl2 context --- webhogg/wasm/src/context/graphics.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'webhogg/wasm/src/context') diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs index 57955d0..3476e5d 100644 --- a/webhogg/wasm/src/context/graphics.rs +++ b/webhogg/wasm/src/context/graphics.rs @@ -1,10 +1,26 @@ +use log::*; use crate::error::WasmError; +use wasm_bindgen::JsCast; +use web_sys::WebGl2RenderingContext as Gl; pub struct GraphicsContext { } impl GraphicsContext { - pub fn from_canvas() -> Result { + pub fn from_canvas(canvas: web_sys::OffscreenCanvas) -> Result { + let context = canvas.get_context("webgl2") + .map_err(|_| WasmError::WebGl2ContextCreation( + format!("context cration failed: getContext returned an exception")))? + .ok_or_else(|| WasmError::WebGl2ContextCreation( + format!("context cration failed: getContext returned nothing")))?; + let context = context + .dyn_into::() + .map_err(|_| WasmError::WebGl2ContextCreation( + format!("context object is not a context")))?; + + //context.clear(Gl::COLOR_BUFFER_BIT); + //context.clear_color(0.6, 0.0, 0.6, 1.0); + Ok(Self { }) } -- cgit v1.3.1