1 | <!DOCTYPE html> |
2 | <html> |
3 | <body> |
4 | <script src="https://code.jquery.com/jquery-1.10.2.js"></script> |
5 | |
6 | <script type="javascript/worker" id="worker1"> |
7 | addEventListener('message', function(e) { |
8 | try { |
9 | console.log("Calculating frame ${FRAME}"); |
10 | |
11 | var imageData = new ImageData(1280, 720); |
12 | |
13 | function setPixel(x, y, r, g, b) { |
14 | index = (x + y * imageData.width) * 4; |
15 | imageData.data[index+0] = r; |
16 | imageData.data[index+1] = g; |
17 | imageData.data[index+2] = b; |
18 | imageData.data[index+3] = 255; |
19 | } |
20 | |
21 | function checkIfBelongsToMandelbrotSet(x,y) { |
22 | var realComponentOfResult = x; |
23 | var imaginaryComponentOfResult = y; |
24 | var maxIterations = 1500; |
25 | for(var i = 0; i < maxIterations; i++) { |
26 | var tempRealComponent = realComponentOfResult * realComponentOfResult |
27 | - imaginaryComponentOfResult * imaginaryComponentOfResult |
28 | + x; |
29 | var tempImaginaryComponent = 2 * realComponentOfResult * imaginaryComponentOfResult |
30 | + y; |
31 | realComponentOfResult = tempRealComponent; |
32 | imaginaryComponentOfResult = tempImaginaryComponent; |
33 | |
34 | // Return a number as a percentage |
35 | if(realComponentOfResult * imaginaryComponentOfResult > 5) |
36 | return Math.pow(i/maxIterations, 0.35)*100; |
37 | } |
38 | return 0; // Return zero if in set |
39 | } |
40 | |
41 | var w = 1280, h = 720; |
42 | |
43 | var x2 = -0.7+600*w/h/2900; |
44 | var y2 = -0.6+600/2900; |
45 | var x1 = -0.7; |
46 | var y1 = -0.6; |
47 | |
48 | for(var x=0; x < w; x++) { |
49 | for(var y=0; y < h; y++) { |
50 | var px = x1+x*(x2-x1)/w; |
51 | var py = y1+y*(y2-y1)/h; |
52 | var belongsToSet = |
53 | checkIfBelongsToMandelbrotSet(px, py); |
54 | if(belongsToSet == 0) { |
55 | setPixel(x, y, 0, 0, 0); // Draw a black pixel |
56 | } else { |
57 | var i = belongsToSet*(255/100); |
58 | setPixel(x, y, 255, i, i); // Draw a black pixel |
59 | } |
60 | } |
61 | } |
62 | |
63 | postMessage(imageData); |
64 | } finally { postMessage('done'); } |
65 | }); |
66 | </script> |
67 | |
68 | <script> |
69 | |
70 | // Create Or Get Canvas |
71 | var myCanvas = document.getElementByID("apfelcanvas"); |
72 | if (myCanvas == null) { |
73 | myCanvas = document.createElement("canvas"); |
74 | myCanvas.id = 'apfelcanvas'; |
75 | myCanvas.width=1280/4; |
76 | myCanvas.height=720/4; |
77 | document.body.appendChild(myCanvas); |
78 | } |
79 | var ctx = myCanvas.getContext("2d"); |
80 | |
81 | //var worker = new Worker('worker.js'); |
82 | |
83 | var blob = new Blob([ |
84 | document.querySelector('#worker1').textContent |
85 | ], { type: "text/javascript" }) |
86 | |
87 | var worker = new Worker(window.URL.createObjectURL(blob)); |
88 | |
89 | worker.onmessage = function(e) { |
90 | ctx.putImageData(e.data, 0, 0); |
91 | //alert('Got image data! ' + e.data.width); |
92 | var jpg = myCanvas.toDataURL("image/jpeg"); |
93 | //alert("Got jpg: " + jpg.length); |
94 | |
95 | $.post("http://butter.botcompany.de:8080/1013900/raw/collect", |
96 | { |
97 | jpg: jpg, |
98 | frame: ${FRAME} |
99 | }, |
100 | function(data, status){ |
101 | alert("Data: " + data + "\nStatus: " + status); |
102 | }); |
103 | }; |
104 | worker.postMessage('go'); |
105 | </script> |
106 | </body> |
107 | </html> |
download render html show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1013902 |
Snippet name: | Mandelbrot Worker HTML (only worker code used) |
Eternal ID of this version: | #1013902/12 |
Text MD5: | 26109103bad92d3e45da626a5b385eaa |
Author: | stefan |
Category: | javax / html |
Type: | HTML |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2018-03-19 04:28:05 |
Source code size: | 3194 bytes / 107 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 413 / 10042 |
Version history: | 11 change(s) |
Referenced in: | [show references] |