client/annotviz/app/js/stageview.js
author rougeronj
Thu, 22 Jan 2015 09:26:43 +0100
changeset 111 a7b72620d227
parent 105 25ac8802c189
child 131 0bb70072a56f
permissions -rw-r--r--
Add variable "wait". When this variable set, the annotsroll wait ignore some annotations, and wait before printing an otherone so there is no superposition. Can be passed as an options

/**
* scripts/stageview.js
*
* This is the starting point for your application.
* Take a look at http://browserify.org/ for more info
*/

/* global document: false */

'use strict';


var PIXI = require('pixi');
var _ = require('lodash');

var defaultConfig = {
    externalRefresh: false,
    logger: undefined,
    sceneWidth: 1024,
    sceneHeight: 768,
    framerate: 25,
    sceneBgColor: 0xFFFFFF,
    canvasContainer: 'canvasContainer',
};

function StageView(options) {

    var _this = this;
    var opts = _(options).defaults(defaultConfig).value();

    var externalRefresh = opts.externalRefresh;

    this.logger = opts.logger;
    this.framerate = opts.framerate;
    var sceneBgColor = opts.sceneBgColor;
    var sceneWidth = opts.sceneWidth;
    var sceneHeight = opts.sceneHeight;
    var canvasContainer = opts.canvasContainer;
    var timeContainer = [];
    var components = []; 
    
    //create an new instance of a pixi stage
    this.stage = new PIXI.Stage(sceneBgColor);
    //create a renderer instance.
    var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);
    	
    this.init = function() {

        if(typeof(canvasContainer) === 'string') {
            canvasContainer = document.getElementById(canvasContainer);
        }
        if(typeof(timeContainer) === 'string') {
            timeContainer = document.getElementById(timeContainer);
        }

        canvasContainer.appendChild(renderer.view);
        
        components.forEach(function(c){
    		c.init();
    	});
    };
    
    this.registerTimeContainer = function(container) {
    	timeContainer.push(container);
    };
    
    this.registerComponent = function(component) {
    	components.push(component);
    	this.stage.addChild(component.container);
    };

    this.refresh = function() {
    	components.forEach(function(c){
    		c.refresh();
    	});
        renderer.render(this.stage);
    };

    // Init page and intervals
    var refreshInterval;

    this.start = function() {

        if(!externalRefresh) {
            refreshInterval = setInterval(function() {_this.refresh();}, 1000/this.framerate);
        }
        
        components.forEach(function(c){
    		c.start();
    	});
    };

    this.stop = function() {
        if(!externalRefresh) {
            clearInterval(refreshInterval);
        }
        clearInterval(refreshTimeInterval);
        
        components.forEach(function(c){
    		c.stop();
    	});
    };


    this.log = function(m) {
        if(this.logger) {
            this.logger.log(m);
        }
    };


    return this;
}

module.exports = {
    StageView: StageView
};