diff --git a/combo.js b/combo.js index 04132f7..9a5ecb9 100644 --- a/combo.js +++ b/combo.js @@ -1,11 +1,20 @@ +importScripts('fractal.js'); + class ComboFractal extends Mandelbrot { constructor() { super(); + // TODO(mime): move out of web worker document.getElementById('julia-map').innerHTML = ''; this.julia = new Julia('julia-canvas'); } + setCanvas(canvas, height) { + super.setCanvas(canvas, height); + + postMessage(this.variables); + } + setOptionsAndDraw(options, opt_mouseX, opt_mouseY) { super.setOptionsAndDraw(options); @@ -25,6 +34,20 @@ class ComboFractal extends Mandelbrot { } dispose() { + // TODO(mime): hook up to webworker document.getElementById('julia-map').innerHTML = ''; } } + + +const instance = new ComboFractal(); +onmessage = (e) => { + switch (e.data.msg) { + case 'init': + instance.setCanvas(e.data.canvas, e.data.height); + break; + default: + instance.setOptionsAndDraw(e.data.options, e.data.mouseX, e.data.mouseY); + break; + } +}; diff --git a/fractal.js b/fractal.js index eb62df5..61e1517 100644 --- a/fractal.js +++ b/fractal.js @@ -1,12 +1,15 @@ class Fractal { - constructor(canvasId) { + constructor() { this.variables = {}; + this.variableLocations = {}; + } - this.canvas = document.getElementById(canvasId || 'canvas'); - this.canvas.width = this.canvas.height = this.canvas.clientHeight; + setCanvas(canvas, height) { + this.canvas = canvas; + this.canvas.width = this.canvas.height = height; this.gl = this.canvas.getContext('webgl'); - this.gl.viewport(0, 0, this.canvas.clientHeight, this.canvas.clientHeight); + this.gl.viewport(0, 0, height, height); this.glDrawArraysMode = this.gl.TRIANGLE_FAN; @@ -46,13 +49,14 @@ class Fractal { } dispose() { - + } setOptionsAndDraw(options, opt_mouseX, opt_mouseY) { for (const key in options) { this.variables[key].value = options[key]; } + postMessage(this.variables); this.draw(); } @@ -64,7 +68,7 @@ class Fractal { for (const key in this.variables) { const variable = this.variables[key]; - this.gl['uniform' + variable.type](variable.location, variable.value); + this.gl['uniform' + variable.type](this.variableLocations[key], variable.value); } this.gl.drawArrays(this.glDrawArraysMode, 0, 4); } @@ -92,7 +96,7 @@ class Fractal { for (const key in this.variables) { const variable = this.variables[key]; - variable.location = this.gl.getUniformLocation(prog, key); + this.variableLocations[key] = this.gl.getUniformLocation(prog, key); } } diff --git a/fractals.html b/fractals.html index a69a95c..304c23e 100644 --- a/fractals.html +++ b/fractals.html @@ -58,10 +58,6 @@