Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

107
LINES

< > BotCompany Repo | #1013902 // Mandelbrot Worker HTML (only worker code used)

HTML

<!DOCTYPE html>
<html>
    <body>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    
    <script type="javascript/worker" id="worker1">
  addEventListener('message', function(e) {
  try {
    console.log("Calculating frame ${FRAME}");
    
var imageData = new ImageData(1280, 720);

function setPixel(x, y, r, g, b) {
    index = (x + y * imageData.width) * 4;
    imageData.data[index+0] = r;
    imageData.data[index+1] = g;
    imageData.data[index+2] = b;
    imageData.data[index+3] = 255;
}

function checkIfBelongsToMandelbrotSet(x,y) {
    var realComponentOfResult = x;
    var imaginaryComponentOfResult = y;
    var maxIterations = 1500;
    for(var i = 0; i < maxIterations; i++) {
         var tempRealComponent = realComponentOfResult * realComponentOfResult
                                 - imaginaryComponentOfResult * imaginaryComponentOfResult
                                 + x;
         var tempImaginaryComponent = 2 * realComponentOfResult * imaginaryComponentOfResult
                                 + y;
         realComponentOfResult = tempRealComponent;
         imaginaryComponentOfResult = tempImaginaryComponent;

         // Return a number as a percentage
         if(realComponentOfResult * imaginaryComponentOfResult > 5) 
            return Math.pow(i/maxIterations, 0.35)*100;
    }
    return 0;   // Return zero if in set        
}

var w = 1280, h = 720;

var x2 = -0.7+600*w/h/2900;
var y2 = -0.6+600/2900;
var x1 = -0.7;
var y1 = -0.6;

for(var x=0; x < w; x++) {
   for(var y=0; y < h; y++) {
   	   var px = x1+x*(x2-x1)/w;
   	   var py = y1+y*(y2-y1)/h;
       var belongsToSet = 
            checkIfBelongsToMandelbrotSet(px, py);
       if(belongsToSet == 0) {
         setPixel(x, y, 0, 0, 0); // Draw a black pixel
       } else {
	     var i = belongsToSet*(255/100);
	     setPixel(x, y, 255, i, i); // Draw a black pixel
       } 
    }
}

postMessage(imageData);
} finally { postMessage('done'); }
});
    </script>
    
    <script>
        
    // Create Or Get Canvas
    var myCanvas = document.getElementByID("apfelcanvas");
    if (myCanvas == null) {
      myCanvas = document.createElement("canvas");
      myCanvas.id = 'apfelcanvas';
      myCanvas.width=1280/4;
      myCanvas.height=720/4;
      document.body.appendChild(myCanvas);
    }
    var ctx = myCanvas.getContext("2d");

  	//var worker = new Worker('worker.js');
  	
  	var blob = new Blob([
      document.querySelector('#worker1').textContent
    ], { type: "text/javascript" })
  
    var worker = new Worker(window.URL.createObjectURL(blob));
	
	worker.onmessage = function(e) {
	  ctx.putImageData(e.data, 0, 0);
	  //alert('Got image data! ' + e.data.width);
	  var jpg = myCanvas.toDataURL("image/jpeg");
	  //alert("Got jpg: " + jpg.length);
	  
	  $.post("http://butter.botcompany.de:8080/1013900/raw/collect",
    {
        jpg: jpg,
        frame: ${FRAME}
    },
    function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
	};
	worker.postMessage('go');
        </script>
    </body>
</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: 412 / 10042
Version history: 11 change(s)
Referenced in: #1013906 - Extract worker code from HTML (Doesn't work because of < in <script>)