script.js 5.3 KB

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