Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!-- : class="re0">diese Zeile nicht löschen
- @echo off
- title %~nx0
- echo this will draw a "Mandelbrot-Fraktal" as mutlithreaded HTML5 Site.
- echo it uses the highest available IE-version running by mshta.exe .
- echo at Windows 10 and the Edge-Browser this will run very fast.
- echo Edge uses by default extensions like asm.js...
- echo the Code of this program itself is not optimized for speed.
- echo this code will calculate brute-force up to 10.000 iterations per point!
- echo.
- echo you can dive into the fractal by clicking at the desired areal.
- echo the calculation-deep is limited by the floating point precision of javascript.
- echo so it will produce weird output in far deep...
- echo.
- echo now press any key to run...
- pause>nul
- ::there are 3 Files in in this one... 1 Batch, 1 HTML and 1 Javascript
- ::to work "multithreaded" the Html needs an external workerfile.
- ::so it's impossible? to include the workercode into the Html.
- ::extract webworker-file from end of this File
- call :extract_worker "%~f0"
- mshta "%~f0"
- del worker.js
- exit /b 0
- :extract_worker
- call :find1stLine2read "%~dpnx1"
- for /F "skip=%firstLine2Read% delims=" %%a in ('type "%~1"') do (
- echo. %%a >>worker.js
- )
- exit /b
- :find1stLine2read
- for /f "usebackq delims=:" %%a in (`findstr /nic:"::::worker" "%~1"`) do (
- set /a "firstLine2Read=%%a"
- )
- exit /b
- und diese auch nicht -->
- <!DOCTYPE html>
- <meta http-equiv="x-ua-compatible" content="ie=edge">
- <html><head>
- <title>Mandelbrot</title>
- <style type="text/css">
- html, body {
- width: 100%;
- height: 100%;
- margin: 0px;
- padding: 0px;
- }
- </style>
- <script type="text/javascript">
- window.addEventListener("load", function (event) {
- var canvas = document.getElementById("fractal")
- var maxTheads = 6;
- mandelbrot = new Mandelbrot(canvas, maxTheads);
- // This will resize the canvas and kick off the initial redraw.
- mandelbrot.resize_to_parent();
- }, false);
- //engine
- var Mandelbrot = function (canvas, n_workers) {
- var self = this; // for use in closures where 'this' is rebound
- this.canvas = canvas;
- this.ctx = canvas.getContext("2d");
- this.row_data = this.ctx.createImageData(canvas.width, 1);
- this.canvas.addEventListener("click", function(event) {
- self.click(event.clientX + document.body.scrollLeft +
- document.documentElement.scrollLeft - canvas.offsetLeft,
- event.clientY + document.body.scrollTop +
- document.documentElement.scrollTop - canvas.offsetTop);
- }, false);
- window.addEventListener("resize", function(event) {
- self.resize_to_parent();
- }, false);
- this.workers = [];
- for (var i = 0; i < n_workers; i++) {
- var worker = new Worker("worker.js");
- worker.onmessage = function(event) {
- self.received_row(event.target, event.data)
- }
- worker.idle = true;
- this.workers.push(worker);
- }
- this.i_max = 1.5;
- this.i_min = -1.5;
- this.r_min = -2.5;
- this.r_max = 1.5;
- this.max_iter = 10000;
- this.escape = 100;
- this.generation = 0;
- this.nextrow = 0;
- this.make_palette()
- }
- Mandelbrot.prototype = {
- make_palette: function() {
- this.palette = []
- // wrap values to a saw tooth pattern.
- function wrap(x) {
- x = ((x + 256) & 0x1ff) - 256;
- if (x < 0) x = -x;
- return x;
- }
- for (i = 0; i <= this.max_iter; i++) {
- this.palette.push([wrap(7*i), wrap(5*i), wrap(11*i)]);
- }
- },
- draw_row: function(data) {
- var values = data.values;
- var pdata = this.row_data.data;
- for (var i = 0; i < this.row_data.width; i++) {
- var pixel;
- pdata[4*i+3] = 255;
- if (values[i] < 0) {
- pdata[4*i] = pdata[4*i+1] = pdata[4*i+2] = 0;
- } else {
- var colour = this.palette[values[i]];
- pdata[4*i] = colour[0];
- pdata[4*i+1] = colour[1];
- pdata[4*i+2] = colour[2];
- }
- }
- this.ctx.putImageData(this.row_data, 0, data.row);
- },
- received_row: function (worker, data) {
- if (data.generation == this.generation) {
- // Interesting data: display it.
- this.draw_row(data);
- }
- this.process_row(worker);
- },
- process_row: function(worker) {
- var row = this.nextrow++;
- if (row >= this.canvas.height) {
- worker.idle = true;
- } else {
- worker.idle = false;
- worker.postMessage({
- row: row,
- width: this.row_data.width,
- generation: this.generation,
- r_min: this.r_min,
- r_max: this.r_max,
- i: this.i_max + (this.i_min - this.i_max) * row / this.canvas.height,
- max_iter: this.max_iter,
- escape: this.escape,
- })
- }
- },
- redraw: function() {
- this.generation++;
- this.nextrow = 0;
- for (var i = 0; i < this.workers.length; i++) {
- var worker = this.workers[i];
- if (worker.idle)
- this.process_row(worker);
- }
- },
- click: function(x, y) {
- var width = this.r_max - this.r_min;
- var height = this.i_min - this.i_max;
- var click_r = this.r_min + width * x / this.canvas.width;
- var click_i = this.i_max + height * y / this.canvas.height;
- var zoomf = 10;
- this.r_min = click_r - width/zoomf;
- this.r_max = click_r + width/zoomf;
- this.i_max = click_i - height/zoomf;
- this.i_min = click_i + height/zoomf;
- this.redraw()
- },
- resize_to_parent: function() {
- this.canvas.width = window.innerWidth;
- this.canvas.height = window.innerHeight;
- // Adjust the horizontal scale to maintain aspect ratio
- var width = ((this.i_max - this.i_min) *
- this.canvas.width / this.canvas.height);
- var r_mid = (this.r_max + this.r_min) / 2;
- this.r_min = r_mid - width/2;
- this.r_max = r_mid + width/2;
- // Reallocate the image data object used to draw rows.
- this.row_data = this.ctx.createImageData(this.canvas.width, 1);
- this.redraw();
- },
- }
- </script>
- </head>
- <body>
- <canvas id="fractal" width="1404" height="743"></canvas>
- </body></html>
- <!--- don't remove/change the next line it's a marker for the extract-routine
- ::::worker
- self.onmessage = function (event) {
- var data = event.data;
- var c_i = data.i;
- var max_iter = data.max_iter;
- var escape = data.escape * data.escape;
- data.values = [];
- for (var i = 0; i < data.width; i++) {
- var c_r = data.r_min + (data.r_max - data.r_min) * i / data.width;
- var z_r = 0, z_i = 0;
- for (iter = 0; z_r*z_r + z_i*z_i < escape && iter < max_iter; iter++) {
- // z -> z^2 + c
- var tmp = z_r*z_r - z_i*z_i + c_r;
- z_i = 2 * z_r * z_i + c_i;
- z_r = tmp;
- }
- if (iter == max_iter) {
- iter = -1;
- }
- data.values.push(iter);
- }
- self.postMessage(data);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement