Skip to content

Instantly share code, notes, and snippets.

@jamorton
Created August 11, 2014 21:55
Show Gist options
  • Save jamorton/9903199604d24cc8f6cd to your computer and use it in GitHub Desktop.
Save jamorton/9903199604d24cc8f6cd to your computer and use it in GitHub Desktop.
diff --git a/src/proxyClient.js b/src/proxyClient.js
index 3fc8313..2decd1a 100644
--- a/src/proxyClient.js
+++ b/src/proxyClient.js
@@ -3,6 +3,8 @@
// utils
+var hasWorkerCanvas = "WorkerCanvas" in window;
+
function FPSTracker(text) {
var last = 0;
var mean = 0;
@@ -79,10 +81,17 @@ var frameId = 0;
var worker = new Worker('{{{ filename }}}.js');
-WebGLClient.prefetch();
+if (!hasWorkerCanvas) {
+ WebGLClient.prefetch();
+}
setTimeout(function() {
- worker.postMessage({ target: 'worker-init', width: Module.canvas.width, height: Module.canvas.height, preMain: true });
+ if (Module.canvas.transferControlToWorker) {
+ var workerCanvas = Module.canvas.transferControlToWorker();
+ worker.postMessage({ target: 'worker-init', canvas: workerCanvas, preMain: true }, [workerCanvas]);
+ } else {
+ worker.postMessage({ target: 'worker-init', width: Module.canvas.width, height: Module.canvas.height, preMain: true });
+ }
}, 0); // delay til next frame, to make sure html is ready
var workerResponded = false;
diff --git a/src/proxyWorker.js b/src/proxyWorker.js
index 7647b5d..632a73b 100644
--- a/src/proxyWorker.js
+++ b/src/proxyWorker.js
@@ -1,4 +1,8 @@
+var hasWorkerCanvas = "WorkerCanvas" in self;
+
+function dump(m) { postMessage({ target: 'stdout', content: x }); }
+
if (typeof console === 'undefined') {
// we can't call Module.printErr because that might be circular
var console = {
@@ -47,7 +51,7 @@ function FPSTracker(text) {
}
}
last = now;
- }
+ };
}
function Element() { throw 'TODO: Element' }
@@ -130,8 +134,6 @@ window.alert = function(text) {
window.scrollX = window.scrollY = 0; // TODO: proxy these
-window.WebGLRenderingContext = WebGLWorker;
-
window.requestAnimationFrame = (function() {
// similar to Browser.requestAnimationFrame
var nextRAF = 0;
@@ -150,7 +152,11 @@ window.requestAnimationFrame = (function() {
};
})();
-var webGLWorker = new WebGLWorker();
+if (!hasWorkerCanvas) {
+ self.webGLWorker = new WebGLWorker();
+ window.WebGLRenderingContext = WebGLWorker;
+ addRunDependency('gl-prefetch');
+}
var document = new EventListener();
@@ -287,9 +293,7 @@ Audio.prototype.pause = function(){};
Audio.prototype.cloneNode = function() {
return new Audio;
-}
-
-Module.canvas = document.createElement('canvas');
+};
Module.setStatus = function(){};
@@ -315,9 +319,6 @@ Module['postMainLoop'] = function() {
commandBuffer = [];
};
-// Wait to start running until we receive some info from the client
-
-addRunDependency('gl-prefetch');
addRunDependency('worker-init');
// buffer messages until the program starts to run
@@ -336,6 +337,29 @@ function messageResender() {
}
}
+function wrapWorkerCanvas(cvs) {
+ cvs.style = new PropertyBag();
+ var el = new EventListener();
+ cvs.addEventListener = function(ev, func) { return el.addEventListener(ev, func); };
+ cvs.removeEventListener = function(ev, func) { return el.removeEventListener(ev, func); };
+ cvs.fireEvent = function(ev) { return el.fireEvent(ev); };
+ cvs.exitPointerLock = function(){};
+ cvs.boundingClientRect = {};
+ cvs.getBoundingClientRect = function () {
+ return {
+ width: cvs.boundingClientRect.width,
+ height: cvs.boundingClientRect.height,
+ top: cvs.boundingClientRect.top,
+ left: cvs.boundingClientRect.left,
+ bottom: cvs.boundingClientRect.bottom,
+ right: cvs.boundingClientRect.right
+ };
+ };
+
+ cvs.ensureData = function() { Module.print("ENSURE DATA"); };
+ return cvs;
+}
+
onmessage = function onmessage(message) {
if (!calledMain && !message.data.preMain) {
if (!messageBuffer) {
@@ -390,9 +414,13 @@ onmessage = function onmessage(message) {
break;
}
case 'worker-init': {
- Module.canvas = document.createElement('canvas');
- Module.canvas.width_ = message.data.width;
- Module.canvas.height_ = message.data.height;
+ if (message.data.canvas) {
+ Module.canvas = wrapWorkerCanvas(message.data.canvas);
+ } else {
+ Module.canvas = document.createElement('canvas');
+ Module.canvas.width_ = message.data.width;
+ Module.canvas.height_ = message.data.height;
+ }
removeRunDependency('worker-init');
break;
}
diff --git a/src/webGLWorker.js b/src/webGLWorker.js
index 98fb008..aeb2b4b 100644
--- a/src/webGLWorker.js
+++ b/src/webGLWorker.js
@@ -971,7 +971,7 @@ function WebGLWorker() {
func();
postRAF();
});
- }
+ };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment