pianoroll/app.js
author cavaliet
Mon, 13 Oct 2014 16:47:37 +0200
changeset 14 30ee8c47e48f
child 15 f1ae020c2872
permissions -rw-r--r--
first step of piano roll (needs to be much improved)

var sock = null;
var ellog = null;


function log(m) {
   ellog.innerHTML += m + '\n';
   ellog.scrollTop = ellog.scrollHeight;
};

//Config vars
var sceneWidth = 800;
var sceneHeight = 400;
var sceneBgColor = 0x66FF99;
var manualFramerate = 24;
var speed = 1; // container -x position at each frame. Speed = 1 ~ 110px for 2 seconds
var pixelsPerSecond = 50; // nb of pixels per second
var lineInterval = 2000; // means line every 2 seconds
var nbLines = 0;

var zeroTime = new Date("2014-10-06T12:16:43.000000Z").getTime();


//create an new instance of a pixi stage
var stage = new PIXI.Stage(sceneBgColor);

//create a renderer instance.
var renderer = PIXI.autoDetectRenderer(sceneWidth, sceneHeight);

//add the renderer view element to the DOM
document.body.appendChild(renderer.view);

//requestAnimFrame( animate );

var uberContainer = new PIXI.DisplayObjectContainer();
uberContainer.position.x = Math.floor(sceneWidth*4/5);
uberContainer.position.y = 0;
stage.addChild(uberContainer);
var container = new PIXI.DisplayObjectContainer();
container.position.x = 0;
container.position.y = 0;
uberContainer.addChild(container);

//create a texture from an image path
var texture = PIXI.Texture.fromImage("bunny.png");


/*function animate() {
    requestAnimFrame( animate );
    // just for fun, lets rotate mr rabbit a little
    //bunny.rotation += 0.1;
    container.x -= speed;
    //console.log("container.x = ", container.x);
    // render the stage  
    renderer.render(stage);
}*/

function moveContainer() {
    container.x -= pixelsPerSecond/manualFramerate;
    renderer.render(stage);
}

function addBunny(data){
    // create a new Sprite using the texture
    var bunny = new PIXI.Sprite(texture);
    // center the sprites anchor point
    //bunny.anchor.x = 0.5;
    //bunny.anchor.y = 0.5;
    deltaMilliseconds = new Date(data.ts).getTime() - zeroTime;
    //bunny.position.x = (sceneWidth / 2) - container.x;
    //bunny.position.y = Math.floor(Math.random()*sceneHeight);
    var x = deltaMilliseconds * pixelsPerSecond / 1000;
    bunny.position.x = x;
    bunny.position.y = Math.floor( data.content[3] * sceneHeight / 128 );
    bunny.alpha = data.content[4] / 128;
    console.log("diff temporelle = ", x);
    container.addChild(bunny);
}

window.onload = function() {

    ellog = document.getElementById('log');

    var wsuri;
    if (window.location.protocol === "file:") {
       wsuri = "ws://127.0.0.1:8090/broadcast";
    } else {
       wsuri = "ws://" + window.location.hostname + ":8090/broadcast";
    }
    if ("WebSocket" in window) {
       sock = new WebSocket(wsuri);
    } else if ("MozWebSocket" in window) {
       sock = new MozWebSocket(wsuri);
    } else {
       log("Browser does not support WebSocket!");
       window.location = "http://autobahn.ws/unsupportedbrowser";
    }

    if (sock) {
       sock.onopen = function() {
          log("Connected to " + wsuri);
       }

       sock.onclose = function(e) {
          log("Connection closed (wasClean = " + e.wasClean + ", code = " + e.code + ", reason = '" + e.reason + "')");
          sock = null;
       }

       sock.onmessage = function(e) {
          log("Got message: " + e.data);
          addBunny(JSON.parse(e.data));
       }
    }
};


function addLine(){
    nbLines++;
    var graphics = new PIXI.Graphics();
    var x = nbLines * (lineInterval/1000) * pixelsPerSecond;
    console.log("nbLines = ",nbLines, "x = ", x);
    graphics.beginFill(0xFFFF00);
    graphics.lineStyle(1, 0x444444);
    graphics.moveTo(x, 0);
    graphics.lineTo(x, sceneHeight);
    graphics.endFill();

    container.addChild(graphics);
}
var moveInterval = window.setInterval(moveContainer, 1000/manualFramerate);
var linesInterval = window.setInterval(addLine, lineInterval);