-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandelbrot.js
176 lines (140 loc) · 5.41 KB
/
mandelbrot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2011 Adam Goforth
//
// This file is part of the Web Workers Mandelbrot Set Renderer.
//
// The Web Workers Mandelbrot Set Renderer is free software: you can
// redistribute it and/or modify it under the terms of the GNU General
// Public License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
//
// The Web Workers Mandelbrot Set Renderer is distributed in the hope
// that it will be useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with the Web Workers Mandelbrot Set Renderer. If not,
// see <http://www.gnu.org/licenses/>.
var startTime;
var slicesFinished = 0;
var workerPool = null;
function init() {
reInitWorkerPool();
}
function reInitWorkerPool() {
var numWorkers = getNumWorkersFromPage();
logToMessageDiv("Re-initializing the worker pool to size: " + numWorkers);
workerPool = null;
workerPool = new Array(numWorkers);
for (var i = 0; i < workerPool.length; i++) {
workerPool[i] = getNewWorker();
}
}
function getNewWorker() {
var worker = new Worker("mandelbrotRenderer.js");
worker.onmessage = function(event) {
var theTime = new Date();
var renderValues = event.data;
var retWorkerID = renderValues.workerID;
var retStartX = renderValues.startX;
var retStartY = renderValues.startY;
var retImageData = renderValues.imageData;
logToMessageDiv("Worker " + retWorkerID + " returned from rendering: " + theTime + " diff: " + ((theTime - startTime) / 1000));
var width = getCanvasContext().canvas.width;
var height = getCanvasContext().canvas.height;
var sliceForEachWorker = parseInt(height / workerPool.length);
var newImageData = getCanvasContext().createImageData(width, sliceForEachWorker);
for (var i = 0; i < retImageData.length; i++) {
newImageData.data[i] = retImageData[i];
}
getCanvasContext().putImageData(newImageData, retStartX, retStartY);
theTime = new Date();
var elapsedTime = ((theTime - startTime) / 1000);
logToMessageDiv("Worker " + retWorkerID + " finished drawing: " + theTime + " diff: " + elapsedTime);
slicesFinished++;
if (slicesFinished == workerPool.length) {
logToMessageDiv("All workers finished. Total time elapsed: " + elapsedTime);
logToTimeTable(workerPool.length, elapsedTime);
}
};
worker.onerror = function(error) {
logToMessageDiv("Worker error: " + error.message + "\n");
throw error;
};
return worker;
}
function draw() {
var imageWidth = getCanvasContext().canvas.width;
var imageHeight = getCanvasContext().canvas.height;
// Clear the whole canvas
getCanvasContext().clearRect(0, 0, imageWidth, imageHeight);
slicesFinished = 0;
// We have all the info we need, so do the drawing
startTime = new Date();
logToMessageDiv("Starting the worker threads.");
drawMandelbrotWithWorkers();
}
function getNumWorkersFromPage() {
var retVal = 0;
var nwSelect = document.getElementById("numWorkers");
if (nwSelect) {
retVal = parseInt(nwSelect.options[nwSelect.selectedIndex].value);
}
return retVal;
}
function drawMandelbrotWithWorkers() {
var maxIterations = 100;
var minReal = -2.0;
var maxReal = 1.0;
var minImaginary = -1.2;
var width = getCanvasContext().canvas.width;
var height = getCanvasContext().canvas.height;
if (getCanvasContext()) {
var maxImaginary = minImaginary + (maxReal - minReal) * (height / width);
var realFactor = (maxReal - minReal) / (width - 1);
var imaginaryFactor = (maxImaginary - minImaginary) / (height - 1);
var startX = 0;
var startY = 0;
var sliceForEachWorker = parseInt(height / workerPool.length);
// Send work to the workers
for (var i = 0; i < workerPool.length; i++) {
startY = parseInt(i * sliceForEachWorker);
var imageData = getCanvasContext().createImageData(width, sliceForEachWorker);
var args = new Object;
args.workerID = i;
args.startX = startX;
args.width = width;
args.startY = startY;
args.height = sliceForEachWorker;
args.maxImaginary = maxImaginary;
args.imaginaryFactor = imaginaryFactor;
args.minReal = minReal;
args.realFactor = realFactor;
args.maxIterations = maxIterations;
args.imageData = imageData.data;
workerPool[i].postMessage(args);
}
}
}
var ctx;
function getCanvasContext() {
if (!ctx) {
var canvas = document.getElementById("can");
if (canvas.getContext) {
ctx = canvas.getContext("2d");
}
}
return ctx;
}
var messageDiv;
function logToMessageDiv(message) {
return;
if (!messageDiv) {
messageDiv = document.getElementById("messageDiv");
}
messageDiv.innerHTML += message + "<br />\n";
}
function logToTimeTable(workers, time) {
var cell = document.getElementById("cell" + workers);
cell.innerHTML += time + "<br />\n";
}