summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornatrixaeria <janng@gmx.de>2019-06-13 03:21:53 +0200
committernatrixaeria <janng@gmx.de>2019-06-13 03:21:53 +0200
commit7884252333cc102a8144e4eafc36f9ef605b1be7 (patch)
tree622630a07c6439a8970178548e20966320cc4fb1
parent5de862b4d175578ed67e177d482ac31bf811d486 (diff)
Draw violet background
-rw-r--r--webhogg/wasm/index.html4
-rw-r--r--webhogg/wasm/pkg/main.js16
-rw-r--r--webhogg/wasm/pkg/worker.js22
-rw-r--r--webhogg/wasm/src/context/graphics.rs10
-rw-r--r--webhogg/wasm/src/graphics.rs1
5 files changed, 39 insertions, 14 deletions
diff --git a/webhogg/wasm/index.html b/webhogg/wasm/index.html
index eae1cc2..b6c8b9b 100644
--- a/webhogg/wasm/index.html
+++ b/webhogg/wasm/index.html
@@ -5,7 +5,9 @@
<title>webhogg</title>
</head>
<body>
- <canvas id='c'>your browser is incompetent</canvas>
+ <canvas id='c' width='640' height='360'>
+ your browser is incompetent
+ </canvas>
<script src='pkg/main.js'></script>
</body>
</html>
diff --git a/webhogg/wasm/pkg/main.js b/webhogg/wasm/pkg/main.js
index f16a775..76afa1c 100644
--- a/webhogg/wasm/pkg/main.js
+++ b/webhogg/wasm/pkg/main.js
@@ -17,12 +17,22 @@ async function main() {
source = await fetchedSource.arrayBuffer();
const modules = [
- ['graphics', source, [offCanvas], 100],
- ['logic', source, [], 1000]
+ { type: 'graphics',
+ source: source,
+ canvas: offCanvas,
+ dt: 10000 },
+ { type: 'logic',
+ source: source,
+ canvas: [],
+ dt: 10000 },
];
for (var module of modules) {
let worker = new Worker('pkg/worker.js');
- worker.postMessage(module, module[2]);
+ if (module.type === 'graphics') {
+ worker.postMessage(module, [module.canvas]);
+ } else {
+ worker.postMessage(module);
+ }
workers.push(worker);
}
}
diff --git a/webhogg/wasm/pkg/worker.js b/webhogg/wasm/pkg/worker.js
index 0b68374..780ea6f 100644
--- a/webhogg/wasm/pkg/worker.js
+++ b/webhogg/wasm/pkg/worker.js
@@ -1,11 +1,17 @@
-onmessage = async function (e) {
+let data = null;
+
+onmessage = function (e) {
+ data = e.data;
+
importScripts('../bin/webhogg-wasm.js');
- let type = e.data[0];
- let source = e.data[1];
- let args = e.data[2];
- let dt = e.data[3];
- let ctx = await wasm_bindgen(source);
+ wasm_bindgen(data.source).then(ctx => {
+ if (data.type === 'graphics') {
+ wasm_bindgen.start_graphics(data.canvas);
+ setInterval(wasm_bindgen.loop_graphics, data.dt);
+ } else if (data.type === 'logic') {
+ wasm_bindgen.start_logic();
+ setInterval(wasm_bindgen.loop_logic, data.dt);
+ }
- ctx['start_' + type].apply(args);
- setInterval(ctx['loop_' + type], dt);
+ });
}
diff --git a/webhogg/wasm/src/context/graphics.rs b/webhogg/wasm/src/context/graphics.rs
index 3476e5d..853e1e1 100644
--- a/webhogg/wasm/src/context/graphics.rs
+++ b/webhogg/wasm/src/context/graphics.rs
@@ -8,6 +8,12 @@ pub struct GraphicsContext {
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")))?
@@ -18,8 +24,8 @@ impl GraphicsContext {
.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);
+ context.clear_color(0.6, 0.0, 0.6, 1.0);
+ context.clear(Gl::COLOR_BUFFER_BIT);
Ok(Self {
})
diff --git a/webhogg/wasm/src/graphics.rs b/webhogg/wasm/src/graphics.rs
index a879c5e..690b4cd 100644
--- a/webhogg/wasm/src/graphics.rs
+++ b/webhogg/wasm/src/graphics.rs
@@ -6,6 +6,7 @@ use crate::*;
pub fn start_graphics(canvas: web_sys::OffscreenCanvas) {
logger::init_logger();
info!("graphics: wasm entry-point reached");
+ //debug!("js value is?: undefined: {}", canvas.is_undefined());
match context::graphics::GraphicsContext::from_canvas(canvas) {
Ok(ctx) => context::set_graphics(ctx),