function tiltHandler()

in src/index.js [291:521]


function tiltHandler(event) {

  if (!touching && orientation_changed) {

    var tilt_time;
    var tilt_selection;
    var now = new Date(); 
  
    scale_tilt_selection.domain([25,65]);
    scale_tilt_time.domain([2,40]);

    if (window.innerHeight > window.innerWidth) {
      tilt_time = event.gamma;
      tilt_selection = event.beta;
    }
    else {
      tilt_time = event.beta;
      tilt_selection = Math.abs(event.gamma);
    }
  
    if (globals.time_tilt_array == undefined) {
      globals.time_tilt_array = [];
    }
    globals.time_tilt_array.push(tilt_time);
    if (globals.time_tilt_array.length >= 5) {
      globals.time_tilt_array.splice(0,1);    
    }
  
    if (globals.selection_tilt_array == undefined) {
      globals.selection_tilt_array = [];
    }
    globals.selection_tilt_array.push(tilt_selection);
    if (globals.selection_tilt_array.length >= 5) {
      globals.selection_tilt_array.splice(0,1);    
    }
      
    if (last_tilt != undefined) {
      if (now.getSeconds() != last_tilt.getSeconds()) {
        last_tilt = now;           
        tps = tilt_counter;
        tilt_counter = 0;
      }    
    }
    else {
      last_tilt = now; 
    }
  
    var j = globals.time_tilt_array.length - 1;
    var consistent_time_tilt = true;
    var time_tilt_sign = 0;
    var consistent_selection_tilt = true;
    var selection_tilt_sign = 0;  
  
    time_tilt_sign = Math.sign(globals.time_tilt_array[j]);
    selection_tilt_sign = Math.sign(globals.selection_tilt_array[j] - globals.selection_tilt_array[j-1]);
    for (var i = j-1; i > 0; i--) {
      if (Math.sign(globals.time_tilt_array[i]) != time_tilt_sign) {
        consistent_time_tilt = false;
      }
      if (Math.sign(globals.selection_tilt_array[i] - globals.selection_tilt_array[i-1]) != selection_tilt_sign) {
        consistent_selection_tilt = false;
      }
    }
    
    tilt_counter++;
    
    if (document.getElementById('chart_g') != null && chart_instance.tilt() == 'on') {
    
      var param_pop = chart_instance.params().radius;
      var scale_pop = chart_instance.scale_pop();
      
      if (tilt_counter % 10) {
        chart_instance.tilt_selection(tilt_selection);      
        d3.selectAll('.country_btn_enabled')
        .style('border', function(d){
          return (chart_instance.bubbleset_points().indexOf(d.code) == -1) ? '1px solid #ccc' : '5px solid gold';
        });
  
        d3.selectAll('.mark').select('circle')
        .attr('r',function(d){
          return scale_pop(Math.sqrt(d[param_pop]));
        });
  
        var highlighted_points = chart_instance.highlight_points();     
  
        if (Math.abs(tilt_time) > 2 && !carousel_touching) {
  
          d3.selectAll('.carousel_item').style('display','none');
  
          if (consistent_time_tilt) {
            chart_instance.tilt_time(tilt_time);
            
            var year = chart_instance.current_year();
            var min_year = chart_instance.params().yearMin;
            var max_year = chart_instance.params().yearMax;
            if ((year - scale_tilt_time(Math.abs(tilt_time))) < min_year && Math.sign(tilt_time) == -1) {
              chart_instance.current_year(min_year);
            }
            else if ((year + scale_tilt_time(Math.abs(tilt_time))) > max_year && Math.sign(tilt_time) == 1){
              chart_instance.current_year(max_year);
            }
            else {
              chart_instance.current_year(year + Math.sign(tilt_time) * scale_tilt_time(Math.abs(tilt_time)));
            }    
          }    
          // var bubble_ind;
          // if (chart_instance.current_year() > 1981 && chart_instance.current_year() < 1989) {
          //   if (chart_instance.bubbleset_points().indexOf('SAU') == -1) {
          //     chart_instance.bubbleset_points().push("SAU");              
          //   }
          //   d3.select('#annotation_div').style('display',null);
          //   d3.select('#annotation_div').select('.annotation')
          //   .html('Notice the rise and fall of Saudi Arabia\'s GDP during the mid 1980s.');
          //   d3.selectAll('.carousel_item').style('display','none');       
          //   d3.selectAll('.carousel_clutch').style('display','none'); 
          // }   
          // else if (chart_instance.current_year() > 1990 && chart_instance.current_year() < 2000) {
          //   if (chart_instance.bubbleset_points().indexOf('SAU') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('SAU');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('RWA') == -1) {
          //     chart_instance.bubbleset_points().push("RWA");              
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('GMB') == -1) {
          //     chart_instance.bubbleset_points().push("GMB");              
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('LBR') == -1) {
          //     chart_instance.bubbleset_points().push("LBR");              
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('ZAF') == -1) {
          //     chart_instance.bubbleset_points().push("ZAF");              
          //   }            
          //   d3.select('#annotation_div').style('display',null);
          //   d3.select('#annotation_div').select('.annotation')
          //   .html('Notice how the African nations diverge from one another during the 1990s.');
          //   d3.selectAll('.carousel_item').style('display','none');       
          //   d3.selectAll('.carousel_clutch').style('display','none'); 
          // }
          // else {
          //   if (chart_instance.bubbleset_points().indexOf('SAU') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('SAU');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('RWA') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('RWA');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('GMB') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('GMB');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('LBR') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('LBR');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }
          //   if (chart_instance.bubbleset_points().indexOf('ZAF') != -1) {
          //     bubble_ind = chart_instance.bubbleset_points().indexOf('ZAF');
          //     chart_instance.bubbleset_points().splice(bubble_ind,1); 
          //   }            
          //   d3.select('#annotation_div').style('display','none');
          //   d3.select('#annotation_div').select('.annotation')
          //   .html('');
          // }
          chart_g.call(chart_instance);  
        }    
  
        else if (highlighted_points.length > 0 && carousel_touching) {
          highlighted_points.sort(function(x, y){
            return d3.ascending(x, y);
          });
  
          d3.selectAll('.carousel_item').style('display','inline');
  
          scale_tilt_selection.range([0,(highlighted_points.length-1)]);
  
          if (consistent_selection_tilt) {
            var highlighted_point_index = 0;
            var selection_tilts = 0;
            for (var g = globals.selection_tilt_array.length - 1; g >= 0; g--){
              selection_tilts = selection_tilts + globals.selection_tilt_array[g];
            }
            if (selection_tilt_sign == 1) {
              highlighted_point_index = Math.round(scale_tilt_selection(selection_tilts / globals.selection_tilt_array.length));
              if (highlighted_point_index > highlighted_points-1){
                highlighted_point_index = highlighted_points - 1;
              }
            }
            else if (selection_tilt_sign == -1){
              highlighted_point_index = Math.round(scale_tilt_selection(selection_tilts / globals.selection_tilt_array.length));
              if (highlighted_point_index < 0){
                highlighted_point_index = 0;
              }
            }
                     
            if (globals.highlighted_point != highlighted_points[highlighted_point_index]) {
              globals.highlighted_point = highlighted_points[highlighted_point_index];
              carousel_instance.carousel_focus(highlighted_point_index);
              carousel_g.call(carousel_instance); 
              var node_r = d3.select('#mark_' + globals.highlighted_point).select('circle').attr('r');        
              d3.select('#mark_' + globals.highlighted_point).select('circle')
              .transition()
              .ease(d3.easeCubic)
              .duration(250)
              .attr('r', (node_r * 1.5))
              .style('stroke-width','2px')
              .transition()
              .ease(d3.easeCubic)
              .duration(250)
              .attr('r', node_r)
              .style('stroke-width','1px');
            }       
          }
          
          d3.select('#' + globals.highlighted_point + '_bttn')
          .style('border', '5px solid cyan');
            
          var node = document.getElementById('mark_' + globals.highlighted_point);
          node.parentElement.appendChild(node);
          chart_g.call(chart_instance);   
        
        }
        

      }
    }
  }
  else {
    carousel_touching = false;
  }
}