script.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. $(document).ready(function() {
  2. var _frames;
  3. var _$timelaps = $('#timelaps'),
  4. _$video = $("#timelapsvid"),
  5. _video = document.getElementById('timelapsvid'),
  6. _fps = 12,
  7. _vid_current_time;
  8. var _timeline, _timeline_container = document.getElementById('timeline'),
  9. _tl_data_ar = [], _tl_items, _tl_start, _tl_end;
  10. var _mapout, _mapout_zoom=7, _mapout_pin, _mapout_provider = "CartoDB.DarkMatter";
  11. var _mapin, _mapin_zoom=15, _mapin_pin, _mapin_provider = "CartoDB.DarkMatter";
  12. var _map_pin_coordinates;
  13. var _mapicon = L.icon({
  14. iconUrl: 'assets/icon.svg',
  15. iconSize: [20, 20],
  16. iconAnchor: [10, 10]
  17. });
  18. function init(){
  19. console.log("Init");
  20. loadData();
  21. };
  22. function loadData(){
  23. console.log('loadData');
  24. // $.getJSON('metadata/frame.json',function(data){
  25. // setUpData(data.frames)
  26. // });
  27. $.ajax({
  28. dataType:"json",
  29. url:'metadata/frames.json',
  30. cache: false,
  31. success:setUpData
  32. });
  33. console.log('getMetaData END');
  34. };
  35. function setUpData(d){
  36. console.log("setUpData");
  37. _frames = d.frames;
  38. // console.log(_frames);
  39. for (i in _frames) {
  40. // convert gps from string to float array
  41. _frames[i].gps = _frames[i].gps.split(',').map(function(a) {
  42. return parseFloat(a);
  43. });
  44. }
  45. // timeline
  46. _tl_start= new Date(_frames[0].datetime);
  47. _tl_end= new Date(_frames[_frames.length-1].datetime);
  48. // console.log('_tl_start : '+_tl_start+" | _tl_end"+_tl_end);
  49. // map
  50. _map_pin_coordinates = _frames[0].gps;
  51. console.log(_map_pin_coordinates);
  52. initDataReady();
  53. };
  54. function initDataReady(){
  55. console.log("initDataReady");
  56. setupTimeline();
  57. setupMap();
  58. setupVideoEvents();
  59. play();
  60. setTimeout(function(){
  61. _video.play();
  62. }, 2000);
  63. }
  64. /*
  65. _ _ _ _
  66. | | (_) | (_)
  67. | |_ _ _ __ ___ ___| |_ _ __ ___
  68. | __| | '_ ` _ \ / _ \ | | '_ \ / _ \
  69. | |_| | | | | | | __/ | | | | | __/
  70. \__|_|_| |_| |_|\___|_|_|_| |_|\___|
  71. */
  72. function setupTimeline(){
  73. // Create a DataSet (allows two way data-binding)
  74. // _tl_items = new vis.DataSet(_tl_data_ar);
  75. // Configuration for the Timeline
  76. // var options = {};
  77. // Create a Timeline
  78. _timeline = new vis.Timeline(_timeline_container, [], {
  79. // height:'50px',
  80. showCurrentTime:false,
  81. start:_tl_start,
  82. end:_tl_end,
  83. stack:false
  84. });
  85. _timeline.addCustomTime(_tl_start, "videotime");
  86. _timeline.on('click', onClickTimeline)
  87. }
  88. function onClickTimeline(props){
  89. console.log("onClickTimeline", props.time);
  90. // seekVideoTo(props.items[0]);
  91. };
  92. function moveTimelineCursor(date){
  93. _timeline.setCustomTime(date, "videotime", {animation:false});
  94. _timeline.moveTo(date, {animation:false});
  95. }
  96. /*
  97. __ __
  98. | \/ |
  99. | \ / | __ _ _ __
  100. | |\/| |/ _` | '_ \
  101. | | | | (_| | |_) |
  102. |_| |_|\__,_| .__/
  103. | |
  104. |_|
  105. */
  106. function setupMap(){
  107. console.log('setupMap');
  108. _mapout = L.map('mapout', {
  109. center:_map_pin_coordinates,
  110. zoom:_mapout_zoom,
  111. zoomControl:false,
  112. attributionControl:false
  113. });
  114. L.tileLayer.provider(_mapout_provider).addTo(_mapout);
  115. _mapout_pin = L.marker(_map_pin_coordinates, {icon:_mapicon}).addTo(_mapout);
  116. _mapin = L.map('mapin', {
  117. center:_map_pin_coordinates,
  118. zoom:_mapin_zoom,
  119. zoomControl:false,
  120. attributionControl:false
  121. });
  122. L.tileLayer.provider(_mapin_provider).addTo(_mapin);
  123. _mapin_pin = L.marker(_map_pin_coordinates, {icon:_mapicon}).addTo(_mapin);
  124. };
  125. function moveMapPin(c){
  126. _mapout.setView(c, _mapout_zoom);
  127. _mapout_pin.setLatLng(c);
  128. _mapout_pin.update(c);
  129. //
  130. _mapin.setView(c, _mapin_zoom);
  131. _mapin_pin.setLatLng(c);
  132. _mapin_pin.update(c);
  133. };
  134. /*
  135. __ ___ _
  136. \ \ / (_) | |
  137. \ \ / / _ __| | ___ ___
  138. \ \/ / | |/ _` |/ _ \/ _ \
  139. \ / | | (_| | __/ (_) |
  140. \/ |_|\__,_|\___|\___/
  141. */
  142. function setupVideoEvents(){
  143. // console.log("setupVideoEvents");
  144. _$video.on("click", onClickVid);
  145. };
  146. function onClickVid(e){
  147. console.log("onClickVid",e);
  148. if(_video.paused){
  149. _video.play();
  150. }else{
  151. _video.pause();
  152. }
  153. };
  154. // function seekVideoTo(f){
  155. // console.log("seekVideoTo frame", f);
  156. // _video.pause();
  157. // _video.currentTime = f/_fps;
  158. // };
  159. /*
  160. _ _ _
  161. (_) | | (_)
  162. __ _ _ __ _ _ __ ___ __ _| |_ _ ___ _ __
  163. / _` | '_ \| | '_ ` _ \ / _` | __| |/ _ \| '_ \
  164. | (_| | | | | | | | | | | (_| | |_| | (_) | | | |
  165. \__,_|_| |_|_|_| |_| |_|\__,_|\__|_|\___/|_| |_|
  166. */
  167. function play(millis){
  168. requestAnimationFrame(play);
  169. // console.log("_video.playing",_video.paused);
  170. if (!_video.paused) {
  171. moveTimelineCursor(_frames[vt2f(_video.currentTime)].datetime);
  172. moveMapPin(_frames[vt2f(_video.currentTime)].gps);
  173. }
  174. // now = Date.now();
  175. // delta = now - then;
  176. //
  177. // if(delta > interval){
  178. // then = now -(delta % interval);
  179. // }
  180. }
  181. /* helpers */
  182. function vt2f(ct){
  183. // console.log("Video Time 2 frame");
  184. return Math.round(ct/(1/_fps));
  185. // console.log("frame", frame);
  186. }
  187. init();
  188. });