Gary Sieling

Generating Fractal Images with Fractal Machine and PhantomJS

I wrote up a script to take screenshots of randomized images from Fractal Machine, which is a really cool fractal generator. To run this, I would a local copy of the html file for the site, otherwise you’ll be generating a lot of needless load on their side.

var page = require('webpage').create();
page.viewportSize = { width: 400, height: 400 };

var num = 0;
var system = require('system');
if (system.args.length > 1) {
  num = system.args[1];
}

function waitFor(testFx, onReady, timeOutMillis) {
    var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 50,
        start = new Date().getTime(),
        condition = false,
        interval = setInterval(function () {
            if1; //< defensive code
            } else {
                if (!condition) {
                    typeof (onReady) === "string" ? eval(onReady) : onReady();
                    clearInterval(interval);
                } else {
                    console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
                    typeof (onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
                    clearInterval(interval); //< Stop this interval
                }
            }
        }, 50); 
};

page.open('fractalmachine.html', function () {
  waitFor(
    function () {
      return page.evaluate(function () {
        return window.document.getElementById('controls') != undefined;
      });
    },
    function () {
      page.evaluate(function() {
        var fract = window.fract;
        fract.random();
        fract.zoomnudge(-1);
        var controls = window.document.getElementById('controls');
        controls.style.visibility = 'hidden';
      });
      page.render('fractal' + num + '.png');
      phantom.exit();
    }, 50);
  });

  1. new Date().getTime() - start < maxtimeOutMillis) && !condition) { condition = (typeof (testFx) === "string" ? eval(testFx) : testFx( []
Exit mobile version