froleyks

timeline.js

Nov 5th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(document).ready(function() {
  2.     var paused = true;
  3.     var interval;
  4.     var currentTime = 0;
  5.     var duration = 100;
  6.     var pButton = document.getElementById('pButton'); // play button
  7.     var playhead = document.getElementById('playhead'); // playhead
  8.     var timeline = document.getElementById('timeline'); // timeline
  9.  
  10.     // timeline width adjusted for playhead
  11.     var timelineWidth = 200;
  12.  
  13.     // play button event listenter
  14.     pButton.addEventListener("click", play);
  15.  
  16.  
  17.     // makes timeline clickable
  18.     timeline.addEventListener("click", function(event) {
  19.         moveplayhead(event);
  20.     }, false);
  21.  
  22.     // returns click as decimal (.77) of the total timelineWidth
  23.     function clickPercent(event) {
  24.         return (event.clientX - getPosition(timeline)) / timelineWidth;
  25.     }
  26.  
  27.     // makes playhead draggable
  28.     playhead.addEventListener('mousedown', mouseDown, false);
  29.     window.addEventListener('mouseup', mouseUp, false);
  30.  
  31.     // Boolean value so that audio position is updated only when the playhead is released
  32.     var onplayhead = false;
  33.  
  34.     // mouseDown EventListener
  35.     function mouseDown() {
  36.         onplayhead = true;
  37.         window.addEventListener('mousemove', moveplayhead, true);
  38.     }
  39.  
  40.     // mouseUp EventListener
  41.     // getting input from all mouse clicks
  42.     function mouseUp(event) {
  43.         if (onplayhead == true) {
  44.             moveplayhead(event);
  45.             window.removeEventListener('mousemove', moveplayhead, true);
  46.             // change current time
  47.         }
  48.         onplayhead = false;
  49.     }
  50.     // mousemove EventListener
  51.     // Moves playhead as user drags
  52.     function moveplayhead(event) {
  53.         var newMargLeft = event.clientX - getPosition(timeline);
  54.  
  55.         if (newMargLeft >= 0 && newMargLeft <= timelineWidth) {
  56.             playhead.style.marginLeft = newMargLeft + "px";
  57.             currentTime = (timelineWidth * (newMargLeft / duration));
  58.         }
  59.         if (newMargLeft < 0) {
  60.             playhead.style.marginLeft = "0px";
  61.             currentTime = 0;
  62.         }
  63.         if (newMargLeft > timelineWidth) {
  64.             playhead.style.marginLeft = timelineWidth + "px";
  65.             currentTime = duration;
  66.         }
  67.     }
  68.  
  69.     // timeUpdate
  70.     // Synchronizes playhead position with current point in audio
  71.     function timeUpdate() {
  72.         var playPercent = timelineWidth * (currentTime / duration);
  73.         playhead.style.marginLeft = playPercent + "px";
  74.         $.post("/ticktock", {
  75.             javascript_data: currentTime
  76.         });
  77.  
  78.         $.ajax({
  79.             url: "/eval_network",
  80.             type: 'GET',
  81.             dataType: 'json',
  82.             contentType: 'application/json',
  83.             mimeType: 'application/json',
  84.             success: function(data) {
  85.                 // source.addFeatures(featuresLive);
  86.                 var feat = source.getFeatures();
  87.                 for (var i = 0; i < feat.length; i++) {
  88.                     var id = feat[i].O.id;
  89.  
  90.                     for (var j = 0; j < data[1].length; j++) {
  91.                         if (id == data[1][j].id) {
  92.                             feat[i].flow = data[1][j].flow;
  93.                         }
  94.                     }
  95.  
  96.                 }
  97.                 console.log(feat);
  98.             },
  99.             error: function(data, status, er) {
  100.                 console.log(er);
  101.             }
  102.         });
  103.  
  104.     }
  105.  
  106.     //Play and Pause
  107.     function play() {
  108.         if (paused) {
  109.             paused = false;
  110.             // remove play, add pause
  111.             pButton.className = "";
  112.             pButton.className = "pause";
  113.             interval = setInterval(function() {
  114.                 currentTime += 5;
  115.                 timeUpdate();
  116.             }, 2000);
  117.  
  118.         } else { // pause music
  119.             // remove pause, add play
  120.             paused = true;
  121.             pButton.className = "";
  122.             pButton.className = "play";
  123.             clearInterval(interval);
  124.             interval = 0;
  125.         }
  126.     }
  127.  
  128.     // Returns elements left position relative to top-left of viewport
  129.     function getPosition(el) {
  130.         return el.getBoundingClientRect().left;
  131.     }
  132. });
Add Comment
Please, Sign In to add comment