client/pianoroll/app/js/pianoroll.js
changeset 85 eff9460bd4f2
parent 84 d7c5bffdd2d8
--- a/client/pianoroll/app/js/pianoroll.js	Mon Jan 12 17:23:05 2015 +0100
+++ b/client/pianoroll/app/js/pianoroll.js	Tue Jan 13 10:46:05 2015 +0100
@@ -10,7 +10,7 @@
 var PIXI = require('pixi');
 var randomColor = require('randomColor');
 
-function PianoRoll(parentContainer, xInit, yInit, height, linesDown, pixelsPerSecond, width, noteColors, colorsReg, lineColor, lineInterval){
+function PianoRoll(parentContainer, xInit, yInit, height, linesDown, pixelsPerSecond, width, noteColors, colorsReg, lineColor, lineInterval, offsetMusic, noteHeight){
     var _this = this;
     this.container = new PIXI.DisplayObjectContainer();
     this.container.position.x = xInit;
@@ -25,11 +25,17 @@
     this.colorsReg = colorsReg || {};
     this.lineColor = lineColor;
     this.lineInterval = lineInterval;
+    this.offsetMusic = offsetMusic || 0;
+    this.noteHeight = noteHeight;
 
     this.addNote = function(note, startTime, duration, velocity, canal){
         //console.log("coucou 1", note, timeFromZero, ts, velocity, pixelsPerSecond, container, prHeight);
-        var beginX = (offsetMusic + startTime) * this.pixelsPerSecond / 1000;
+        var beginX = (this.offsetMusic + startTime) * this.pixelsPerSecond / 1000;
         var width = duration * this.pixelsPerSecond / 1000;
+        if((beginX+width) <  Math.abs(this.container.x) - this.width) {
+            // not visible. do nothing
+            return;
+        }
         // We draw the rectangle
         var graphics = new PIXI.Graphics();
         //console.log("beginX = ", beginX, "canal = ", canal, "color = ", noteColor[canal], "width = ", width, "note = ", note, "velocity = ", velocity);
@@ -80,20 +86,25 @@
 
     this.removePassedObjets = function(){
         var nbChilds = _this.container.children.length;
-        var i = 0, childIsNowDisplayed = false;
+        var i = 0, childIsNowDisplayed = false, childrenToRemove = [];
         while(i<nbChilds && !childIsNowDisplayed){
-            var child = _this.container.children[0];
+            var child = _this.container.children[i++];
             //console.log("remove ? ", child.x, child.width, ((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)));
+            if(typeof(child) == 'undefined') {
+                continue;
+            }
             if((child.x + child.width) < (Math.abs(_this.container.x) - _this.width)){
-                _this.container.removeChild(child);
+                childrenToRemove.push(child);
                 //console.log("    remove !!!");
             }
-            else{
+            else {
                 childIsNowDisplayed = true;
                 //console.log("    childIsNowDisplayed");
             }
-            i++;
         }
+        childrenToRemove.forEach(function(child) {
+            _this.container.removeChild(child);
+        });
         //console.log("before : ", nbChilds, ", after : ", _this.container.children.length);
     };