Created
November 23, 2010 10:07
-
-
Save christianmeichtry/711553 to your computer and use it in GitHub Desktop.
Mandelbrot Web Worker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xMax = window.innerWidth ; | |
yMax = window.innerHeight ; | |
maxThreads = 6 ; | |
function workerMessage(thread) { | |
return '' +thread + ' ' + xMax + " " + yMax ; | |
} | |
var b_canvas = document.getElementById("a"); | |
var b_context = b_canvas.getContext("2d"); | |
var worker = new Worker("js/mandelbrot_worker.js") ; | |
for(thread = 1 ; thread <= maxThreads ; thread++) { | |
worker.postMessage(workerMessage(thread)) ; | |
} | |
worker.onmessage = function(e) { | |
data = e.data.split(" ") ; | |
x = parseInt(data.shift()) ; | |
for(y = 0 ; y < yMax ; y++) { | |
b_context.fillStyle = data[y] ; | |
b_context.fillRect(x, y, 1, 1); | |
} | |
x = x+maxThreads ; | |
if (x < xMax) { | |
worker.postMessage(workerMessage(x)) | |
} | |
} ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doYLine(screenX, xMax, yMax) | |
{ | |
computedLine = new String ; | |
computedLine = screenX ; | |
xZoom = xMax/1000.0/.5 ; | |
yZoom = yMax/1000.0/.5 ; | |
xThingy = screenX/xMax ; | |
xOffset = xZoom/4 ; | |
yOffset = yZoom/4 ; | |
for (screenY=0 ; screenY < yMax ; screenY++) { | |
x0 = xZoom*xThingy-1 - xOffset ; | |
y0 = yZoom*screenY/yMax-1 - yOffset ; | |
x = 0 | |
y = 0 | |
iteration = 0 | |
max_iteration = 50 | |
while ( (x*x + y*y <= (2*2)) && (iteration < max_iteration) ) | |
{ | |
xtemp = x*x - y*y + x0 | |
y = 2*x*y + y0 | |
x = xtemp | |
iteration = iteration + 1 | |
} | |
if (iteration == max_iteration) { | |
computedLine += " rgb(0,0,0)" | |
} | |
else { | |
computedLine += " rgb(" + parseInt((iteration/max_iteration)*255+30) + ",10,10)" ; | |
} | |
} | |
return computedLine; | |
} | |
onmessage = function(event) { | |
m = event.data.split(" ") ; | |
line = doYLine(parseInt(m[0]), parseInt(m[1]), parseInt(m[2])) ; | |
postMessage(line) ; | |
} ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment