Jump To …

displayout.js

(function (global) {
var displayout = function (conf, id) {
  var canvas, 
    Canvas, fs, path,
    context,
    timer = null, 
    displayCallback,
    running,
    apis = {}, 
    api,
    requestAnimFrame,
    frame = 0,
    run, stop, nodeCanvasOutputDir,
    that = {};

  if (typeof document != 'undefined') {
    apis.interval = !!document.createElement('canvas').getContext;
    if (typeof window != 'undefined') {
      requestAnimFrame = window.requestAnimationFrame ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame ||
          window.oRequestAnimationFrame ||
          window.msRequestAnimationFrame;
    }
    if (requestAnimFrame) {
      apis.requestAnimationFrame = true;
    }
    if (typeof require != 'undefined') {
      Canvas = require('canvas');
      if (Canvas) {
        apis.nodeCanvas = true;
      }
    }
  }

  conf = conf || {};
  conf.frameRate = conf.frameRate || 30;
  conf.displayWidth = conf.displayWidth || 1280;
  conf.displayHeight = conf.displayHeight || 720;
  conf.backgroundColor = conf.backgroundColor || '#ffffff';

  function nextFrame () {
    return displayCallback(context);
  } 

  function init (dir) {
    frame = 0;
    if (api == 'nodeCanvas') { 
      canvas = canvas || new Canvas();
      nodeCanvasOutputDir = dir;
    } else {
      canvas = canvas || document.createElement('canvas');
      if (id) { canvas.id = id; }

canvas.style.backgroundColor = conf.backgroundColor;

    }
    canvas.width = conf.displayWidth;
    canvas.height = conf.displayHeight;
    context = canvas.getContext('2d');
    return canvas;
  }

  function stopInterval () {
    if (timer !== null) {
      clearInterval(timer);
      timer = null;
      running = false;
      return true;
    } 
  }

  function runInterval () {
    if (timer === null) {
      timer = setInterval( function () {
        if (!nextFrame()) {
          stopInterval();
        }
      }, Math.floor(1000/conf.frameRate));
      running = true;
      return true;
    }
  }

  function stopRequestAnimationFrame () {
    running = false;
  }

  function runRequestAnimationFrame () {
    running = true;
    (function animloop () {
      if (!running) { return; }
      requestAnimFrame(animloop);
      if (!nextFrame()) { 
        stopRequestAnimationFrame(); 
      } 
    }());
  }

  function zeroPad(number, width) {
    width -= number.toString().length;
    if ( width > 0 ) {
      return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + 
          number;
    }
    return number;
  }

  function stopNodeCanvas () {
    running = false;
  }

  function runNodeCanvas () {
    var frameOutputFile;
    running = true;

should run for correct number of frames

    while (running) {
      frameOutputFile = 
          path.join(nodeCanvasOutputDir, zeroPad(frame, 10)+'.png');
      if (!nextFrame()) {
        stopNodeCanvas();
        return;
      }
      fs.writeFileSync(frameOutputFile, canvas.toBuffer());
      frame++;
    }
  }

  function isRunning () {
    return running;
  }

  function hasApi (theApi) {
    if (theApi) {
      return apis[theApi];
    } else {
      return (apis.interval || apis.requestAnimationFrame);
    }
  }

  function setApi (theApi) {
    if (theApi) { 
      api = theApi; 
    } else if (apis.requestAnimationFrame) {
      api = 'requestAnimationFrame';
    } else if (apis.interval) {
      api = 'interval';
    } else if (apis.nodeCanvas) {
      api = 'nodeCanvas';
    }
    if (api == 'requestAnimationFrame') {
      that.run = runRequestAnimationFrame;
      that.stop = stopRequestAnimationFrame;
    } else if (api == 'interval') {
      that.run = runInterval;
      that.stop = stopInterval;
    } else if (api == 'nodeCanvas') {
      Canvas = require('canvas');
      fs = require('fs');
      path = require('path');
      that.run = runNodeCanvas;
      that.stop = stopNodeCanvas;
    }
    return api;
  }

  function getApi () {
    return api;
  }

  function setDisplayCallback (cb) {
    displayCallback = cb;
  }

  function setFrameRate (fps) {
    conf.frameRate = fps;
  }

  that.hasApi = hasApi;
  that.setApi = setApi;
  that.getApi = getApi;
  that.setFrameRate = setFrameRate;
  that.init = init;
  that.isRunning = isRunning;
  that.setDisplayCallback = setDisplayCallback;
  return that;
};

if (typeof module != 'undefined' && module.exports) {
  module.exports = displayout;
} else {
  global.trurl = global.trurl || {};
  global.trurl.displayout = displayout;
}

}(this));