pano.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. if (ref_points == undefined) var ref_points = new Array();
  2. if (image_loop == undefined) var image_loop = false;
  3. var debug_mode = false;
  4. var canvas;
  5. var cntext;
  6. var point_list = new Array();
  7. var zoom = 0;
  8. var zooms = new Array();
  9. var prev_zm;
  10. var zm;
  11. var tile = {width:256, height:256};
  12. var ntiles = {x:228, y:9};
  13. var border_width = 2;
  14. var imageObj = new Array();
  15. // minimum and maximum azimuth
  16. var alpha_domain = {start:0, end:360};
  17. var fingr = 0; // mémorisation de lécart entre doigts;
  18. var last = {x:0,y:0};
  19. var shift = {x:0,y:0};
  20. var mouse = {x:0,y:0};
  21. var speed = {x:0,y:0};
  22. var canvas_pos = {x:0,y:0};
  23. var tmt;
  24. var is_located = false;
  25. var point_colors = {
  26. 'pano_point' : '255,128,128', // red
  27. 'ref_point' : '128,128,255', // blue
  28. 'loc_point' : '128,255,128', // green
  29. 'temporary' : '255,255,128', // yellow
  30. 'unlocated' : '255,255,255' // white
  31. };
  32. var map_never_drawn = true; //Pour empecher d'enlever des Layers inexistants
  33. var map; // minimap object
  34. var viewField; // cone drawn on the minimap
  35. var viewDirection; // blue line drawn on the minimap
  36. function getXMLHttpRequest() {
  37. var xhr = null;
  38. if (window.XMLHttpRequest || window.ActiveXObject) {
  39. if (window.ActiveXObject) {
  40. try {
  41. xhr = new ActiveXObject("Msxml2.XMLHTTP");
  42. } catch(e) {
  43. xhr = new ActiveXObject("Microsoft.XMLHTTP");
  44. }
  45. } else {
  46. xhr = new XMLHttpRequest();
  47. }
  48. } else {
  49. alert("Votre navigateur ne supporte pas l'objet XMLHTTPRequest...");
  50. return null;
  51. }
  52. return xhr;
  53. }
  54. function nmodulo(val, div) { // pour obtenir un modulo dans l'espace des nombres naturels N.
  55. return Math.floor((val%div+div)%div); // il y a peut être plus simple, mais en attendant ....
  56. }
  57. function fmodulo(val, div) { // pour obtenir un modulo dans l'espace des nombres réels positifs.
  58. return (val%div+div)%div; // il y a peut être plus simple, mais en attendant ....
  59. }
  60. function distort_canvas(p, x, y) {
  61. if (p == 0) distort = 0;
  62. else {
  63. cntext.save();
  64. distort++;
  65. cntext.clearRect(0, 0, canvas.width, 2*canvas.height);
  66. var ratio = (canvas.width-2*distort)/canvas.width;
  67. var shift = canvas.height/2*(1-ratio);
  68. cntext.scale(1, ratio);
  69. if (p == 1) cntext.translate(0, 0);
  70. else if (p == -1) cntext.translate(0, 0);
  71. draw_image(x, y);
  72. cntext.restore();
  73. }
  74. }
  75. function draw_image(ox, oy) {
  76. var ref_vals = {x:last.x, y:last.y, zoom:zoom};
  77. ox = nmodulo(ox-canvas.width/2, zm.im.width); // pour placer l'origine au centre du canvas
  78. oy = Math.floor(oy-canvas.height/2); // pas de rebouclage vertical
  79. cntext.clearRect(0, 0, canvas.width, canvas.height);
  80. cntext.fillStyle = "rgba(128,128,128,0.8)";
  81. if (canvas.height > zm.im.height) {
  82. var fy = Math.floor((oy+canvas.height/2-zm.im.height/2)/(tile.height*zm.ntiles.y))*zm.ntiles.y;
  83. if (fy < 0) fy = 0;
  84. var ly = fy + zm.ntiles.y;
  85. } else {
  86. var fy = Math.floor(oy/tile.height);
  87. var ly = Math.floor((oy+canvas.height+tile.height-1)/tile.height+1);
  88. if (fy < 0) fy = 0;
  89. if (ly > zm.ntiles.y) ly = zm.ntiles.y;
  90. }
  91. for (var j=fy; j<ly; j++) {
  92. var delta_y = (Math.floor(j/zm.ntiles.y) - Math.floor(fy/zm.ntiles.y)) * (tile.height - zm.last_tile.height);
  93. var dy = j*tile.height - oy - delta_y;
  94. var ny = j%ntiles.y;
  95. var wy = zm.tile.width;
  96. if (ny == zm.ntiles.y - 1) wy = zm.last_tile.height;
  97. var cpx = 0;
  98. var i = 0;
  99. var Nx = zm.ntiles.x;
  100. while (cpx < ox+canvas.width) {
  101. var cur_width = zm.tile.width;
  102. if (i%Nx == zm.ntiles.x-1) cur_width = zm.last_tile.width;
  103. if (cpx >= ox-cur_width) {
  104. var nx = i%Nx;
  105. var idx = nx+'-'+ny+'-'+ref_vals.zoom;
  106. if (imageObj[idx] && imageObj[idx].complete) {
  107. draw_tile(idx, cpx-ox, dy); // l'image est déja en mémoire, on force le dessin sans attendre.
  108. } else {
  109. var fname = get_file_name(nx, ny, ref_vals.zoom);
  110. imageObj[idx] = new Image();
  111. imageObj[idx].src = fname;
  112. var ts = zm.get_tile_size(nx, ny);
  113. cntext.fillRect(cpx-ox, dy, ts.width, ts.height);
  114. imageObj[idx].addEventListener('load', (function(ref, idx, dx, dy, ox, oy, ts) {
  115. return function() { // closure nécéssaire pour gestion assynchronisme !!!
  116. draw_tile_del(ref, idx, dx, dy, ox, oy, ts.width, ts.height);
  117. };
  118. })(ref_vals, idx, cpx-ox, dy, ox, oy, ts), false);
  119. }
  120. // load_image(zoom, nx, ny, shx, shy, cpx-ox, dy, ox, oy);
  121. }
  122. cpx += cur_width;
  123. i++;
  124. }
  125. }
  126. drawDecorations(ox, oy);
  127. var cap_ele = zm.get_cap_ele(last.x, zm.im.height/2-last.y);
  128. angle_control.value = cap_ele.cap.toFixed(2);
  129. elvtn_control.value = cap_ele.ele.toFixed(2);
  130. update_url();
  131. }
  132. function draw_tile_del(ref, idx, tx, ty, ox, oy, twidth, theight) {
  133. if (ref.zoom == zoom && ref.x == last.x && ref.y == last.y) {
  134. draw_tile(idx, tx, ty);
  135. drawDecorations(ox, oy, tx, ty, twidth, theight);
  136. }
  137. }
  138. function draw_tile(idx, ox, oy) {
  139. var img = imageObj[idx];
  140. cntext.drawImage(img, ox, oy);
  141. }
  142. /** Draws the colored circles and the central line
  143. */
  144. function drawDecorations(ox, oy, tx, ty, twidth, theight) {
  145. if (twidth) {
  146. cntext.save();
  147. cntext.beginPath();
  148. cntext.rect(tx, ty, twidth, theight);
  149. cntext.clip();
  150. }
  151. var wgrd = zm.im.width/360;
  152. var od = ((ox+canvas.width/2)/wgrd)%360;
  153. var el = (zm.im.height/2 - (oy+canvas.height/2))/wgrd;
  154. // draw a vertical blue line with the central dot
  155. // the dot is centered on (ox, oy) = (canvas.width/2, canvas.width/2)
  156. var line_width = 3;
  157. cntext.fillStyle = "rgba(43, 130, 203, 0.7)";
  158. cntext.strokeStyle = "yellow";
  159. cntext.lineWidth = 2;
  160. cntext.fillRect(canvas.width/2-line_width/2, 0, line_width, canvas.height);
  161. cntext.strokeRect(canvas.width/2-line_width, canvas.height/2-line_width, line_width*2, line_width*2);
  162. for(var i = 0; i < zm.pt_list.length; i++) {
  163. if (zm.pt_list[i]['type'] != 'unlocated') {
  164. cntext.fillStyle = 'rgba('+point_colors[zm.pt_list[i]['type']]+',0.5)';
  165. var cx = nmodulo(zm.pt_list[i]['xc'] - ox, zm.im.width);
  166. var cy = zm.pt_list[i]['yc'] - oy;
  167. cntext.beginPath();
  168. cntext.arc(cx, cy, 20, 0, 2*Math.PI, true);
  169. cntext.fill();
  170. }
  171. }
  172. if (twidth) {
  173. cntext.restore();
  174. }
  175. }
  176. function insert_drawn_point(lat, lon, alt) {
  177. var rt = 6371; // Rayon de la terre
  178. var pt_alt = document.getElementById('pos_alt').childNodes[0].nodeValue;
  179. var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
  180. var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
  181. var alt1 = pt_alt;
  182. var lat1 = pt_lat*Math.PI/180;
  183. var lon1 = pt_lon*Math.PI/180;
  184. var alt2 = alt;
  185. var lat2 = lat*Math.PI/180;
  186. var lon2 = lon*Math.PI/180;
  187. var dLat = lat2-lat1;
  188. var dLon = lon2-lon1;
  189. var a = Math.sin(dLat/2)*Math.sin(dLat/2) + Math.sin(dLon/2)*Math.sin(dLon/2)*Math.cos(lat1)*Math.cos(lat2); //
  190. var angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  191. var d = angle*rt; // distance du point en Kms
  192. var y = Math.sin(dLon) * Math.cos(lat2);
  193. var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
  194. var cap = Math.atan2(y,x); // cap pour atteindre le point en radians
  195. var e = Math.atan2((alt2 - alt1)/1000 - d*d/(2*rt),d); // angle de l'élévation en radians
  196. return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI}; // les résultats sont en degrés
  197. }
  198. function localate_point() {
  199. var lat = document.getElementById("loca_latitude").value;
  200. var lon = document.getElementById("loca_longitude").value;
  201. var alt = document.getElementById("loca_altitude").value;
  202. if (lat == '' || isNaN(lat) || lat <= -90 || lat > 90) {
  203. alert("La latitude "+lat+"n'est pas correcte");
  204. return;
  205. }
  206. if (lat == '' || isNaN(lon) || lon <= -180 || lon > 180) {
  207. alert("La longitude "+lon+"n'est pas correcte");
  208. return;
  209. }
  210. if (lat == '' || isNaN(alt) || alt < -400 || alt > 10000000) {
  211. alert("l'altitude "+alt+"n'est pas correcte");
  212. return;
  213. }
  214. var opt_ced = new Array();
  215. opt_dce = insert_drawn_point(lat, lon, alt);
  216. display_temp(opt_dce.d, opt_dce.cap, opt_dce.ele);
  217. }
  218. function display_temp(d,cap,ele) {
  219. point_list[point_list.length] = new Array("point temporaire", d,cap,ele, "temporary");
  220. reset_zooms();
  221. putImage(last.x, last.y);
  222. }
  223. function arrayUnset(array, value){
  224. array.splice(array.indexOf(value), 1);
  225. }
  226. function erase_point() {
  227. for (var i=0; i<point_list.length; i++) {
  228. if(point_list[i][0] == "point temporaire"){
  229. arrayUnset(point_list,point_list[i]);
  230. loop = erase_point();
  231. }
  232. }
  233. reset_zooms();
  234. putImage(last.x, last.y);
  235. }
  236. /** Returns a 3-width zero-padded version of an int
  237. * ex: 3 -> "003"
  238. */
  239. function zero_pad(number) {
  240. var temp = number.toString(10);
  241. while (temp.length < 3) {
  242. temp = '0' + temp;
  243. }
  244. return temp;
  245. }
  246. function get_file_name(x, y, z) { // recherche du fichier correspondant au zoom et à la position
  247. return img_prefix+'/'+zero_pad(z)+'-'+zero_pad(x)+'-'+zero_pad(y)+'.jpg';
  248. }
  249. function get_base_name() {
  250. /**
  251. * @returns the base name, which is the name (not path) of the folder where
  252. * the tiles are.
  253. */
  254. return img_prefix.split('/').reverse()[0];
  255. }
  256. function keys(key) {
  257. hide_links();
  258. evt = key || event;
  259. //evt.preventDefault();
  260. //evt.stopPropagation();
  261. if (!key) {
  262. key = window.event;
  263. key.which = key.keyCode;
  264. }
  265. //alert(key);
  266. //if (!evt.shiftKey) return;
  267. switch (key.which) {
  268. case 36: // home
  269. case 35: // end
  270. angle_control = document.getElementById('angle_ctrl');
  271. angle_control.value = parseFloat(angle_control.value) + 180;
  272. change_angle();
  273. case 39: // left
  274. putImage(last.x+40, last.y);
  275. break;
  276. case 40: // up
  277. putImage(last.x, last.y+20);
  278. break;
  279. case 37: // right
  280. putImage(last.x-40, last.y);
  281. break;
  282. case 38: // down
  283. putImage(last.x, last.y-20);
  284. break;
  285. case 33: // pageup
  286. zoom_control.value--;
  287. change_zoom()
  288. break;
  289. case 34: // pagedown
  290. zoom_control.value++;
  291. change_zoom()
  292. break;
  293. default:
  294. // alert(key.which)
  295. break;
  296. }
  297. // Always update map when a key is pressed
  298. update_map();
  299. }
  300. function onImageClick(e) {
  301. hide_contextmenu();
  302. var index = {};
  303. if (e.touches && e.touches.length == 2) {
  304. e.preventDefault();
  305. fingr = Math.sqrt((e.touches[0].clientX - e.touches[1].clientX)^2 +
  306. (e.touches[0].clientY - e.touches[1].clientY)^2);
  307. } else {
  308. if (e.touches) {
  309. e.preventDefault();
  310. index.x = e.changedTouches[0].clientX;
  311. index.y = e.changedTouches[0].clientY;
  312. } else {
  313. index.x = e.pageX;
  314. index.y = e.pageY;
  315. }
  316. shift.x = last.x;
  317. shift.y = last.y;
  318. speed.x = 0;
  319. speed.y = 0;
  320. mouse.x = index.x;
  321. mouse.y = index.y;
  322. }
  323. clearTimeout(tmt); //arrêt de l'éffet eventuel d'amorti en cours.
  324. canvas.addEventListener('mousemove', stickImage, false);
  325. canvas.addEventListener('touchmove', stickImage, false);
  326. canvas.addEventListener('mouseup', launchImage, false);
  327. canvas.addEventListener('touchend', launchImage, false);
  328. //canvas.addEventListener('mouseout', launchImage, false);
  329. canvas.style.cursor='move';
  330. //document.onmousemove = stickImage;
  331. //document.onmouseup = launchImage;
  332. hide_links();
  333. }
  334. function stickImage(e) {
  335. var index = {};
  336. if (e.changedTouches && e.changedTouches.length == 2) {
  337. e.preventDefault();
  338. // cas du zoom à 2 doigts
  339. var nfingr = Math.sqrt((e.changedTouches[0].clientX - e.changedTouches[1].clientX)^2 +
  340. (e.changedTouches[0].clientY - e.changedTouches[1].clientY)^2);
  341. var evt = {}
  342. evt.pageX = (e.changedTouches[0].clientX + e.changedTouches[1].clientX)/2;
  343. evt.pageY = (e.changedTouches[0].clientY + e.changedTouches[1].clientY)/2;
  344. if (fingr > nfingr*2 || fingr < nfingr/2) {
  345. evt.wheelDelta = fingr - nfingr;
  346. fingr = nfingr;
  347. return wheel_zoom(evt);
  348. } else {
  349. return;
  350. }
  351. }
  352. if (e.touches) {
  353. e.preventDefault();
  354. index.x = e.changedTouches[0].clientX;
  355. index.y = e.changedTouches[0].clientY;
  356. } else {
  357. index.x = e.pageX;
  358. index.y = e.pageY;
  359. }
  360. var xs = mouse.x - index.x + shift.x;
  361. var ys = mouse.y - index.y + shift.y;
  362. speed.x = xs - last.x; //mémorisation des vitesses horizontales
  363. speed.y = ys - last.y; //et verticales lors de ce déplacement
  364. putImage(xs, ys);
  365. }
  366. function launchImage(e) {
  367. var index = {};
  368. if (e.touches) {
  369. e.preventDefault();
  370. index.x = e.changedTouches[0].clientX;
  371. index.y = e.changedTouches[0].clientY;
  372. } else {
  373. index.x = e.pageX;
  374. index.y = e.pageY;
  375. }
  376. distort_canvas(0);
  377. canvas.removeEventListener('mousemove', stickImage, false);
  378. canvas.removeEventListener('touchmove', stickImage, false);
  379. //document.onmousemove = null;
  380. shift.x = index.x - mouse.x + shift.x;
  381. shift.y = index.y - mouse.y + shift.y;
  382. tmt = setTimeout(inertialImage, 100);
  383. }
  384. function putImage(x, y) { // est destiné à permettre l'effet d'amortissement par la mémorisation de la position courante.
  385. if (!zm.is_updated) return;
  386. if (x >= zm.im.width) { // rebouclage horizontal
  387. shift.x -= zm.im.width;
  388. x -= zm.im.width;
  389. } else if (x < 0) {
  390. shift.x += zm.im.width;
  391. x += zm.im.width;
  392. }
  393. if (y >= zm.im.height) { // pas de rebouclage vertical mais blocage
  394. //distort_canvas(1, x, y);
  395. shift.y = zm.im.height-1;
  396. y = zm.im.height-1;
  397. } else if (y < 0) {
  398. //distort_canvas(-1, x, y);
  399. shift.y = 0;
  400. y = 0;
  401. }
  402. last.x = x;
  403. last.y = y;
  404. draw_image(x, y);
  405. }
  406. function inertialImage() {
  407. speed.x *= 0.9;
  408. speed.y *= 0.9;
  409. if (Math.abs(speed.x) > 2 || Math.abs(speed.y) > 2) {
  410. putImage(last.x+speed.x, last.y+speed.y);
  411. tmt = setTimeout(inertialImage, 100);
  412. } else {
  413. show_links();
  414. }
  415. update_map();
  416. }
  417. function tri_ref_points(v1, v2) {
  418. return v1['x'] - v2['x'];
  419. }
  420. function tzoom(zv) {
  421. this.value = zv;
  422. this.ntiles = {x:0,y:0};
  423. this.tile = {width:0,height:0};
  424. this.last_tile = {width:0,height:0};
  425. this.max_tile = {width:0,height:0};
  426. this.im = {width:0,height:0};
  427. this.is_updated = false;
  428. this.refresh = function() {
  429. this.im.visible_width = this.tile.width*(this.ntiles.x-1)+this.last_tile.width;
  430. this.is_updated = true;
  431. this.im.width = this.im.visible_width;
  432. this.im.height = this.tile.height*(this.ntiles.y-1)+this.last_tile.height;
  433. if (this.last_tile.width > this.tile.width) {
  434. this.max_tile.width = this.im.last_tile.width;
  435. } else {
  436. this.max_tile.width = this.tile.width;
  437. }
  438. if (this.last_tile.height > this.tile.height) {
  439. this.max_tile.height = this.im.last_tile.height;
  440. } else {
  441. this.max_tile.height = this.tile.height;
  442. }
  443. var ord_pts = new Array();
  444. for(var label in ref_points) {
  445. ord_pts.push(ref_points[label]);
  446. }
  447. ord_pts = ord_pts.sort(tri_ref_points);
  448. is_located = (ord_pts.length > 1)
  449. || image_loop && (ord_pts.length > 0);
  450. alpha_domain = {start:0, end:360};
  451. this.pixel_y_ratio = this.im.width/360;
  452. if (is_located) {
  453. this.ref_pixels = new Array;
  454. this.ref_pixels[0] = new Array(); // Attention il faut compter un intervalle de plus !
  455. for (var i=0; i < ord_pts.length; i++) { // premier parcours pour les paramètres cap/x
  456. this.ref_pixels[i+1] = new Array();
  457. this.ref_pixels[i+1].x = Math.floor(ord_pts[i].x*this.im.width);
  458. this.ref_pixels[i+1].cap = fmodulo(ord_pts[i].cap, 360);
  459. if (i != ord_pts.length-1) {
  460. this.ref_pixels[i+1].ratio_x = (ord_pts[i+1].x - ord_pts[i].x) /
  461. fmodulo(ord_pts[i+1].cap - ord_pts[i].cap, 360)*this.im.width;
  462. }
  463. }
  464. if (image_loop == true) {
  465. var dpix = this.im.width;
  466. var dangle = 360;
  467. if (ord_pts.length > 1) {
  468. dpix = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
  469. dangle = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[this.ref_pixels.length-1].cap, 360);
  470. }
  471. this.ref_pixels[0].ratio_x = dpix/dangle;
  472. this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[0].ratio_x;
  473. dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
  474. this.ref_pixels[0].cap = fmodulo(this.ref_pixels[ord_pts.length].cap + dpix / this.ref_pixels[0].ratio_x, 360);
  475. } else {
  476. this.ref_pixels[0].ratio_x = this.ref_pixels[1].ratio_x;
  477. this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[ord_pts.length-1].ratio_x;
  478. this.ref_pixels[0].cap = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[1].x / this.ref_pixels[1].ratio_x, 360);
  479. alpha_domain.start = this.ref_pixels[0].cap;
  480. alpha_domain.end = fmodulo(this.ref_pixels[ord_pts.length].cap+(this.im.width-this.ref_pixels[ord_pts.length].x)/this.ref_pixels[ord_pts.length].ratio_x, 360);
  481. this.pixel_y_ratio = this.im.width/fmodulo(alpha_domain.end-alpha_domain.start, 360);
  482. }
  483. this.ref_pixels[0].x = 0;
  484. for (var i=0; i < ord_pts.length; i++) { // second parcours pour les paramètres elevation/y
  485. this.ref_pixels[i+1].shift_y = Math.floor(this.pixel_y_ratio*ord_pts[i].ele - ord_pts[i].y*this.im.height);
  486. if (i != ord_pts.length-1) {
  487. var next_shift = Math.floor(this.pixel_y_ratio*ord_pts[i+1].ele - ord_pts[i+1].y*this.im.height);
  488. this.ref_pixels[i+1].dshft_y = (next_shift - this.ref_pixels[i+1].shift_y)/(this.ref_pixels[i+2].x - this.ref_pixels[i+1].x);
  489. }
  490. }
  491. if (image_loop == true) {
  492. var dpix = this.im.width;
  493. var delt = 0;
  494. if (ord_pts.length > 1) {
  495. dpix = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
  496. delt = this.ref_pixels[this.ref_pixels.length-1].shift_y - this.ref_pixels[1].shift_y;
  497. }
  498. this.ref_pixels[0].dshft_y = -delt/dpix;
  499. this.ref_pixels[ord_pts.length].dshft_y = this.ref_pixels[0].dshft_y;
  500. dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
  501. this.ref_pixels[0].shift_y = this.ref_pixels[ord_pts.length].shift_y + dpix*this.ref_pixels[0].dshft_y;
  502. } else {
  503. this.ref_pixels[0].shift_y = this.ref_pixels[1].shift_y;
  504. this.ref_pixels[0].dshft_y = 0;
  505. this.ref_pixels[ord_pts.length].dshft_y = 0;
  506. }
  507. }
  508. this.pt_list = new Array();
  509. for (var i=0; i<point_list.length; i++) {
  510. var lbl = point_list[i][0];
  511. var dst = point_list[i][1];
  512. var cap = point_list[i][2];
  513. var ele = point_list[i][3];
  514. var lnk = point_list[i][4];
  515. var url = point_list[i][5];
  516. var typ = 'unlocated';
  517. var rxy = this.get_pos_xy(cap, ele);
  518. var is_visible = (
  519. fmodulo(cap - alpha_domain.start, 360)
  520. <=
  521. fmodulo(alpha_domain.end -
  522. alpha_domain.start -0.0001, 360)+0.0001
  523. && is_located);
  524. this.pt_list[i] = new Array();
  525. if (ref_points[lbl] != undefined && lnk == '') {
  526. typ = 'ref_point';
  527. if (!is_located) {
  528. rxy = {
  529. x:ref_points[lbl].x*this.im.width,
  530. y:ref_points[lbl].y*this.im.height
  531. };
  532. }
  533. } else if(lnk == '' && is_visible && lbl != 'point temporaire') {
  534. typ = 'loc_point';
  535. }else if(is_visible && lbl =='point temporaire') {
  536. typ = 'temporary';
  537. } else if(is_visible) {
  538. typ = 'pano_point';
  539. }
  540. this.pt_list[i]['type'] = typ;
  541. this.pt_list[i]['cap'] = cap;
  542. this.pt_list[i]['ele'] = ele;
  543. this.pt_list[i]['dist'] = dst;
  544. this.pt_list[i]['label'] = lbl;
  545. this.pt_list[i]['lnk'] = lnk;
  546. this.pt_list[i]['url'] = url;
  547. this.pt_list[i]['xc'] = rxy.x;
  548. this.pt_list[i]['yc'] = Math.floor(this.im.height/2 - rxy.y);
  549. }
  550. },
  551. this.get_tile_size = function(nx, ny) {
  552. var res = {width:0, height:0};
  553. if (nx == this.ntiles.x-1) res.width = this.last_tile.width;
  554. else res.width = this.tile.width;
  555. if (ny == this.ntiles.y-1) res.height = this.last_tile.height;
  556. else res.height = this.tile.height;
  557. return res;
  558. }
  559. this.get_cap_ele = function(px, py) { // recherche d'un cap et d'une élévation à partir d'un pixel X,Y.
  560. if (is_located) {
  561. for (var i=0; i < this.ref_pixels.length; i++) {
  562. if (i == this.ref_pixels.length - 1 || px < this.ref_pixels[i+1].x) {
  563. var dpix = px-this.ref_pixels[i].x;
  564. var cp = fmodulo(this.ref_pixels[i].cap + dpix/this.ref_pixels[i].ratio_x, 360);
  565. var el = (py+this.ref_pixels[i].shift_y+this.ref_pixels[i].dshft_y*dpix)/this.pixel_y_ratio;
  566. return {cap:cp, ele:el};
  567. }
  568. }
  569. } else {
  570. var cp = 360*px/this.im.width;
  571. var el = 360*py/this.im.height;
  572. return {cap:cp, ele:el};
  573. }
  574. }
  575. this.get_pos_xy = function(cap, ele) { // recherche des coordonnées pixel à partir d'un cap et d'une élévation.
  576. if (is_located) {
  577. var dcap = fmodulo(cap-this.ref_pixels[0].cap, 360);
  578. for (var i=0; i < this.ref_pixels.length; i++) {
  579. if (i == this.ref_pixels.length - 1 || dcap < fmodulo(this.ref_pixels[i+1].cap-this.ref_pixels[0].cap, 360)) {
  580. var px = this.ref_pixels[i].x + this.ref_pixels[i].ratio_x*fmodulo(cap - this.ref_pixels[i].cap, 360);
  581. var dpix = px-this.ref_pixels[i].x;
  582. var py = this.pixel_y_ratio*ele - this.ref_pixels[i].shift_y - this.ref_pixels[i].dshft_y*dpix;
  583. if (image_loop || (dcap < fmodulo(alpha_domain.end - alpha_domain.start, 360)))
  584. // Position is inside the view
  585. return {x: px, y: py};
  586. else {
  587. // Position is outside the view, find out which edge is closest
  588. if (fmodulo(alpha_domain.start - cap, 360) < fmodulo(cap - alpha_domain.end, 360))
  589. // Left edge
  590. return {x: 0, y: py};
  591. else
  592. // Right edge
  593. return {x: image_width - 1, y: py};
  594. }
  595. }
  596. }
  597. } else {
  598. var px = fmodulo(cap, 360)/360*this.im.width;
  599. var py = ele/360*this.im.height;
  600. return {x:px, y:py};
  601. }
  602. }
  603. }
  604. function reset_zooms () {
  605. for(i=0; i<zooms.length; i++) zooms[i].is_updated = false;
  606. zm.refresh();
  607. }
  608. function wheel_zoom (event) {
  609. var zshift = {x:0, y:0};
  610. if (event.pageX != undefined && event.pageX != undefined) {
  611. zshift.x = event.pageX-canvas.width/2-canvas_pos.x;
  612. zshift.y = event.pageY-canvas.height/2-canvas_pos.y;
  613. }
  614. //event.preventDefault();
  615. var delta = (event.wheelDelta || -event.detail);
  616. if (delta < 0 && zoom_control.value < zoom_control.max) {
  617. zoom_control.value++;
  618. change_zoom(zshift.x, zshift.y);
  619. } else if (delta > 0 && zoom_control.value > zoom_control.min) {
  620. zoom_control.value--;
  621. change_zoom(zshift.x, zshift.y);
  622. }
  623. }
  624. function change_zoom(shx, shy) {
  625. var zoom_control = document.getElementById("zoom_ctrl");
  626. var v = zoom_control.value;
  627. prev_zm = zm;
  628. if (zooms[v]) {
  629. if (!zooms[v].is_updated) zooms[v].refresh();
  630. } else {
  631. zooms[v] = new tzoom(v);
  632. }
  633. if (zooms[v].is_updated) {
  634. if (shx == undefined || shy == undefined) {
  635. shx=0;
  636. shy=0;
  637. }
  638. zm = zooms[v];
  639. var px = (last.x+shx)*zm.im.width/prev_zm.im.width - shx;
  640. var py = (last.y+shy)*zm.im.height/prev_zm.im.height - shy;
  641. if (py < zm.im.height && py >= 0) {
  642. zoom = zm.value;
  643. tile = zm.tile;
  644. ntiles = zm.ntiles;
  645. update_url();
  646. putImage(px, py);
  647. } else {
  648. zm = prev_zm;
  649. zoom_control.value = zm.value;
  650. }
  651. }
  652. }
  653. function change_angle() {
  654. var elvtn_control = document.getElementById('elvtn_ctrl');
  655. var angle_control = document.getElementById('angle_ctrl');
  656. var resxy = zm.get_pos_xy(angle_control.value, elvtn_control.value);
  657. var pos_x = resxy.x;
  658. var pos_y = Math.floor(zm.im.height/2 - resxy.y);
  659. putImage(pos_x, pos_y);
  660. }
  661. function check_prox(x, y, r) { //verification si un point de coordonnées x, y est bien dans un cercle de rayon r centré en X,Y.
  662. return Math.sqrt(x*x + y*y) < r;
  663. }
  664. function check_links(e) {
  665. var mouse_x = e.pageX-canvas_pos.x;
  666. var mouse_y = e.pageY-canvas_pos.y;
  667. var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
  668. var pos_y = last.y + mouse_y - canvas.height/2;
  669. for(var i = 0; i < zm.pt_list.length; i++) {
  670. if (is_located && zm.pt_list[i]['type'] == 'pano_point') {
  671. if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
  672. if (zm.pt_list[i]['lnk'] != '') window.location = zm.pt_list[i]['lnk'];
  673. break;
  674. }
  675. }
  676. }
  677. }
  678. function display_links(e) {
  679. var index = {};
  680. if (e.touches) {
  681. e.preventDefault();
  682. index.x = e.changedTouches[0].clientX;
  683. index.y = e.changedTouches[0].clientY;
  684. } else {
  685. index.x = e.pageX;
  686. index.y = e.pageY;
  687. }
  688. var info = document.getElementById('info');
  689. var mouse_x = index.x-canvas_pos.x;
  690. var mouse_y = index.y-canvas_pos.y;
  691. var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
  692. var pos_y = last.y + mouse_y - canvas.height/2;
  693. //var cap = ((pos_x/zm.im.width)*360).toFixed(2);
  694. var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
  695. //var ele = ((zm.im.height/2 - pos_y)/zm.im.width)*360;
  696. info.innerHTML = 'élévation : '+res.ele.toFixed(2)+'&#176;<br/>cap : '+res.cap.toFixed(2)+'&#176;';
  697. info.style.top = index.y+'px';
  698. info.style.left = index.x+'px';
  699. info.style.backgroundColor = '#222';
  700. info.style.opacity = '0.6'
  701. info.style.color = 'white'
  702. info.style.display = 'block';
  703. canvas.style.cursor='crosshair';
  704. for(var i = 0; i < zm.pt_list.length; i++) {
  705. if (is_located || zm.pt_list[i]['type'] == 'ref_point') {
  706. if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
  707. info.innerHTML = zm.pt_list[i]['label'];
  708. info.style.opacity = '0.8'
  709. info.style.color = 'black'
  710. if (zm.pt_list[i]['dist'] < 10) var dst = Math.round(zm.pt_list[i]['dist']*1000)+' m';
  711. else var dst = zm.pt_list[i]['dist'].toFixed(1)+' kms';
  712. info.innerHTML += '<br/>(' + dst + ')';
  713. info.style.backgroundColor = 'rgb('+point_colors[zm.pt_list[i]['type']]+')';
  714. canvas.style.cursor='auto';
  715. break;
  716. }
  717. }
  718. }
  719. }
  720. function hide_links() {
  721. canvas.removeEventListener( "mousemove", display_links, false);
  722. canvas.removeEventListener( "touchmove", display_links, false);
  723. var info = document.getElementById('info');
  724. info.style.display = 'none';
  725. }
  726. function show_links() {
  727. canvas.addEventListener( "mousemove", display_links, false);
  728. canvas.addEventListener( "touchmove", display_links, false);
  729. // var info = document.getElementById('info');
  730. // info.style.display = 'block';
  731. }
  732. function hide_contextmenu() {
  733. document.getElementById('insert').style.display = 'none';
  734. }
  735. function manage_ref_points(e) {
  736. //event.preventDefault();
  737. //event.stopPropagation();
  738. var sel_pt = document.getElementById('sel_point');
  739. var do_insert = document.getElementById('do-insert');
  740. var do_delete = document.getElementById('do-delete');
  741. var do_cancel = document.getElementById('do-cancel');
  742. //var show_cap = document.getElementById('show-cap');
  743. var insrt = document.getElementById('insert');
  744. var pos_x = nmodulo(last.x + e.pageX - canvas_pos.x - canvas.width/2, zm.im.width);
  745. var pos_y = last.y + e.pageY - canvas_pos.y - canvas.height/2;
  746. insrt.style.left = e.pageX+'px';
  747. insrt.style.top = e.pageY+'px';
  748. insrt.style.display = 'block';
  749. if (do_insert) {// true if there are ref points
  750. for(var i = 0; i < zm.pt_list.length; i++) {
  751. if (zm.pt_list[i]['type'] == 'ref_point') {
  752. if (check_prox(zm.pt_list[i]['xc']-pos_x,
  753. zm.pt_list[i]['yc']-pos_y, 20)) {
  754. sel_pt.value = zm.pt_list[i]['label'];
  755. }
  756. }
  757. }
  758. do_delete.onclick = function() {delete_ref_point(insrt)};
  759. do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
  760. // TODO: adapt to the new backend
  761. //show_cap.onclick = function() {
  762. // window.open('show_capline.php?title='+encodeURIComponent(btoa(title))+'&cap='+res.cap+'&org_lat='+pt_lat+'&org_lon='+pt_lon+'&dist=120000');
  763. //};
  764. }
  765. do_cancel.onclick = hide_contextmenu;
  766. var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
  767. var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
  768. var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
  769. return false;
  770. }
  771. function insert_ref_point(el, x, y) {
  772. var label, posx, posy;
  773. el.style.display = 'none';
  774. var selected_label = document.getElementById('sel_point').value;
  775. var found = false;
  776. var refpoint_url;
  777. for(var i = 0; i < zm.pt_list.length; i++) {
  778. label = zm.pt_list[i]['label'];
  779. if(label == selected_label) {
  780. refpoint_url = zm.pt_list[i]['url'];
  781. posx = nmodulo(last.x + x - canvas.width/2, zm.im.width)/zm.im.width;
  782. posy = 0.5 - (last.y + y - canvas.height/2)/zm.im.height;
  783. var pval = {x:posx, y:posy, cap:zm.pt_list[i]['cap'], ele:zm.pt_list[i]['ele'], label:label};
  784. ref_points[label] = pval;
  785. reset_zooms();
  786. putImage(last.x, last.y);
  787. found = true;
  788. break;
  789. }
  790. }
  791. if (!found) {
  792. alert('unknown ref_point: '+label);
  793. }
  794. // Then push the modif
  795. var xhr = getXMLHttpRequest();
  796. xhr.open("POST", "/api/v1/references/", true);
  797. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  798. xhr.setRequestHeader("X-CSRFToken", csrf_token);
  799. xhr.send("reference_point=" + refpoint_url
  800. + "&panorama=" + panorama_url
  801. + "&x=" + Math.floor(posx * image_width)
  802. + "&y=" + Math.floor((posy + 0.5) * image_height));
  803. // update the course of the panorama boundaries
  804. // (update cap_min/cap_max of the panorama object)
  805. var xhr = getXMLHttpRequest();
  806. xhr.open("POST", "/api/v1/panoramas/", true);
  807. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  808. xhr.setRequestHeader("X-CSRFToken", csrf_token);
  809. }
  810. function delete_ref_point(el) {
  811. var ref_name = document.getElementById('sel_point').value;
  812. el.style.display = 'none';
  813. var url = ref_points[ref_name].url;
  814. delete ref_points[ref_name];
  815. reset_zooms();
  816. putImage(last.x, last.y);
  817. // Then push the modif
  818. var xhr = getXMLHttpRequest();
  819. xhr.open("DELETE", url, true);
  820. xhr.setRequestHeader("X-CSRFToken", csrf_token);
  821. xhr.send();
  822. }
  823. function clean_canvas_events(e) {
  824. canvas.removeEventListener('mousemove', stickImage, false);
  825. canvas.removeEventListener('touchmove', stickImage, false);
  826. document.getElementById('info').style.display = 'none';
  827. speed.x = 0;
  828. speed.y = 0;
  829. }
  830. canvas_set_size = function() {
  831. canvas.style.border = border_width+"px solid black";
  832. canvas.width = window.innerWidth-2*border_width;
  833. canvas.height = window.innerHeight-2*border_width;
  834. canvas_pos.x = canvas.offsetLeft+border_width;
  835. canvas_pos.y = canvas.offsetTop+border_width;
  836. }
  837. canvas_resize = function() {
  838. canvas_set_size();
  839. putImage(last.x, last.y);
  840. update_map();
  841. }
  842. function paramIn(e) {
  843. e = e || window.event;
  844. var relatedTarget = e.relatedTarget || e.fromElement;
  845. while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
  846. relatedTarget = relatedTarget.parentNode;
  847. }
  848. if (relatedTarget != adding && relatedTarget != localisation) {
  849. document.removeEventListener('keydown', keys, false);
  850. }
  851. }
  852. function paramOut(e) {
  853. e = e || window.event;
  854. var relatedTarget = e.relatedTarget || e.toElement;
  855. while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
  856. relatedTarget = relatedTarget.parentNode;
  857. }
  858. if (relatedTarget != adding && relatedTarget != localisation) {
  859. document.addEventListener('keydown', keys, false);
  860. }
  861. }
  862. /* Parse initial orientation from URL, either:
  863. #zoom=A/x=B/y=C
  864. #zoom=A/cap=B/elev=C
  865. In the first case, (x, y) is an image coordinate in pixels, where (0, 0)
  866. is the lower left corner.
  867. In the second case, point to the given cap and elevation, assuming the
  868. current panorama is already calibrated.
  869. */
  870. function get_orientation_from_url() {
  871. // Parse window.location.hash to get either x/y or cap/ele
  872. var regexp1 = /^#zoom=(\d)\/cap=(-?\d+|-?\d+\.\d+)\/ele=(-?\d+|-?\d+\.\d+)$/;
  873. var regexp2 = /^#zoom=(\d)\/x=(\d+)\/y=(\d+)$/;
  874. var res = window.location.hash.match(regexp1);
  875. if (res) {
  876. return { zoom: parseInt(res[1], 10),
  877. cap: parseFloat(res[2]),
  878. elevation: parseFloat(res[3]) };
  879. }
  880. else {
  881. res = window.location.hash.match(regexp2);
  882. if (res) {
  883. return { zoom: parseInt(res[1], 10),
  884. x: parseInt(res[2], 10),
  885. y: parseInt(res[3], 10) };
  886. }
  887. else {
  888. /* By default, center the view */
  889. return { zoom: 2, x: image_width / 2, y: image_height / 2 };
  890. }
  891. }
  892. }
  893. /* Update the URL to reflect the current zoom/orientation, so that it acts
  894. * as a permalink. */
  895. function update_url() {
  896. var x = last.x << zm.value;
  897. var y = image_height - (last.y << zm.value);
  898. // Important: don't set window.location.hash directly, because it
  899. // records a new entry in the browser history...
  900. window.location.replace("#zoom=" + zm.value + "/x=" + x + "/y=" + y);
  901. }
  902. function load_pano() {
  903. localisation = document.getElementById("locadraw");
  904. adding = document.getElementById("adding");
  905. canvas = document.getElementById("mon-canvas");
  906. cntext = canvas.getContext("2d");
  907. canvas_set_size();
  908. canvas.addEventListener("click", check_links, false);
  909. //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
  910. canvas.oncontextmenu = manage_ref_points;
  911. canvas.addEventListener("mouseout" , clean_canvas_events, false);
  912. show_links();
  913. var initial_orientation = get_orientation_from_url();
  914. var to_zoom = initial_orientation.zoom;
  915. var max_zoom = zooms.length - 1;
  916. zoom_control = document.getElementById("zoom_ctrl");
  917. zoom_control.onchange = change_zoom;
  918. zoom_control.max = max_zoom;
  919. if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
  920. zm = zooms[to_zoom];
  921. zoom_control.value = to_zoom;
  922. zm.refresh();
  923. zoom = zm.value;
  924. tile = zm.tile;
  925. ntiles = zm.ntiles;
  926. if (!("cap" in initial_orientation)) {
  927. /* Compute cap and elevation from (x, y) coordinates */
  928. var res = zm.get_cap_ele(initial_orientation.x >> zoom,
  929. (initial_orientation.y - image_height / 2) >> zoom);
  930. initial_orientation.cap = res.cap;
  931. initial_orientation.elevation = res.ele;
  932. }
  933. angle_control = document.getElementById("angle_ctrl");
  934. angle_control.value = initial_orientation.cap;
  935. angle_control.addEventListener('change', change_angle, false);
  936. angle_control.addEventListener('change', update_map, false);
  937. angle_control.addEventListener('onclick', change_angle, false);
  938. elvtn_control = document.getElementById("elvtn_ctrl");
  939. elvtn_control.value = initial_orientation.elevation;
  940. elvtn_control.onchange = change_angle;
  941. elvtn_control.onclick = change_angle;
  942. change_angle();
  943. loca_temp = document.getElementById("loca_show");
  944. if (loca_temp) {
  945. loca_temp.onclick = showLoca;
  946. loca_temp = document.getElementById("loca_hide");
  947. loca_temp.onclick = hideLoca;
  948. loca_temp = document.getElementById("loca_button");
  949. loca_temp.onclick = localate_point;
  950. loca_erase = document.getElementById("loca_erase");
  951. loca_erase.onclick = erase_point;
  952. localisation.addEventListener('mouseover',paramIn,false);
  953. localisation.addEventListener('mouseout',paramOut,false);
  954. }
  955. canvas.addEventListener('mousedown', onImageClick, false);
  956. canvas.addEventListener('touchstart', onImageClick, false);
  957. document.addEventListener('keydown', keys, false);
  958. canvas.addEventListener('mousewheel', wheel_zoom, false);
  959. canvas.addEventListener('DOMMouseScroll', wheel_zoom, false);
  960. //map events
  961. canvas.addEventListener('mousewheel', update_map, false);
  962. canvas.addEventListener('DOMMouseScroll', update_map, false);
  963. canvas.addEventListener('mousedown', update_map, false);
  964. canvas.addEventListener('mouseup', update_map, false);
  965. //
  966. window.onresize = canvas_resize;
  967. if (adding) {
  968. document.getElementById("paramFormHide").onclick = hideForm;
  969. document.getElementById("paramFormShow").onclick = showForm;
  970. adding.addEventListener('mouseover', paramIn, false);
  971. adding.addEventListener('mouseout', paramOut, false);
  972. }
  973. };
  974. function toRad(n) {
  975. return n * Math.PI / 180;
  976. }
  977. function toDeg(n) {
  978. return n * 180 / Math.PI;
  979. }
  980. function destVincenty(lat1, lon1, brng, dist) {
  981. /* JavaScript function to calculate the destination point given start point
  982. * latitude / longitude (numeric degrees), bearing (numeric degrees) and
  983. * distance (in m).
  984. * Original scripts by Chris Veness
  985. * Taken from http://movable-type.co.uk/scripts/latlong-vincenty-direct.html
  986. * and optimized / cleaned up by Mathias Bynens <http://mathiasbynens.be/>
  987. *
  988. * Based on the Vincenty direct formula by T. Vincenty, “Direct and Inverse
  989. * Solutions of Geodesics on the Ellipsoid with application of nested
  990. * equations”, Survey Review, vol XXII no 176, 1975
  991. * <http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf>
  992. */
  993. var a = 6378137,
  994. b = 6356752.3142,
  995. f = 1 / 298.257223563, // WGS-84 ellipsiod
  996. s = dist,
  997. alpha1 = toRad(brng),
  998. sinAlpha1 = Math.sin(alpha1),
  999. cosAlpha1 = Math.cos(alpha1),
  1000. tanU1 = (1 - f) * Math.tan(toRad(lat1)),
  1001. cosU1 = 1 / Math.sqrt((1 + tanU1 * tanU1)), sinU1 = tanU1 * cosU1,
  1002. sigma1 = Math.atan2(tanU1, cosAlpha1),
  1003. sinAlpha = cosU1 * sinAlpha1,
  1004. cosSqAlpha = 1 - sinAlpha * sinAlpha,
  1005. uSq = cosSqAlpha * (a * a - b * b) / (b * b),
  1006. A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq))),
  1007. B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq))),
  1008. sigma = s / (b * A),
  1009. sigmaP = 2 * Math.PI;
  1010. while (Math.abs(sigma - sigmaP) > 1e-12) {
  1011. var cos2SigmaM = Math.cos(2 * sigma1 + sigma),
  1012. sinSigma = Math.sin(sigma),
  1013. cosSigma = Math.cos(sigma),
  1014. deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
  1015. sigmaP = sigma;
  1016. sigma = s / (b * A) + deltaSigma;
  1017. };
  1018. var tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1,
  1019. lat2 = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f) * Math.sqrt(sinAlpha * sinAlpha + tmp * tmp)),
  1020. lambda = Math.atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1),
  1021. C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha)),
  1022. Lo = lambda - (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM))),
  1023. revAz = Math.atan2(sinAlpha, -tmp); // final bearing
  1024. return {lat: toDeg(lat2), lng: lon1 + toDeg(Lo)};
  1025. };
  1026. /*
  1027. function getCone(lat, lng, bearing, angle, distance){
  1028. // Returns a polygon to be drawn to the map to show the current visual field
  1029. //
  1030. var conepoints = [];
  1031. // by default, points are drawn every 5°, but if the angle to draw is
  1032. // smaller than 5°, we draw no intermediary points
  1033. var delta = 5;
  1034. if (angle < 5){delta=angle};
  1035. conepoints.push ([lat,lng]);
  1036. for (i=0; i<=angle; i+=delta){
  1037. conepoints.push([destVincenty(lat, lng, bearing-(angle/2.0)+i, distance).lat,
  1038. destVincenty(lat, lng, bearing-(angle/2.0)+i, distance).lng]);
  1039. }
  1040. var p = L.polygon(conepoints, {
  1041. color: 'grey',
  1042. fillColor: 'grey',
  1043. fillOpacity: 0.5
  1044. });
  1045. return p;
  1046. };
  1047. */
  1048. function getCone(lat, lng, bearing, cap, distance){
  1049. /* Returns a polygon to be drawn to the map to show the current visual field
  1050. */
  1051. var conepoints = [];
  1052. // by default, points are drawn every 5°, plus the end-point.
  1053. var delta = 5;
  1054. var total_angle = cap.cap_max - cap.cap_min;
  1055. if (cap.cap_max<cap.cap_min){total_angle+=360}
  1056. conepoints.push ([lat,lng]);
  1057. for (i=0; i<=total_angle; i+=delta){
  1058. angle = cap.cap_min+i;
  1059. if (angle > 360){angle-=360}
  1060. conepoints.push([destVincenty(lat, lng, angle, distance).lat,
  1061. destVincenty(lat, lng, angle, distance).lng])
  1062. }
  1063. // add extrem point
  1064. conepoints.push([destVincenty(lat, lng, cap.cap_max, distance).lat,
  1065. destVincenty(lat, lng, cap.cap_max, distance).lng])
  1066. var p = L.polygon(conepoints, {
  1067. color: 'black',
  1068. weight: '1',
  1069. fillColor: 'grey',
  1070. fillOpacity: 0.5
  1071. });
  1072. return p;
  1073. };
  1074. function getCapMinMaxVisible(){
  1075. /* Return the minimun and maximum cap visible
  1076. */
  1077. var cw = canvas.width;
  1078. var initial_orientation = get_orientation_from_url();
  1079. var x = initial_orientation.x ;
  1080. var to_zoom = initial_orientation.zoom ;
  1081. zm = zooms[to_zoom];
  1082. // x min and max visible in the screen
  1083. // 1 pixel_screen = X pixel_photo = pixel_photo / ((nb_tiles-1) * pixel_tile + pixel_last_tile)
  1084. // (nb_tiles-1)*pixel_tile + pixel_last_tile = zm.im.visible_width
  1085. var half_width = (cw - 2*border_width) * ( image_width / (zm.im.visible_width)) / 2 ;
  1086. var total_angle = fmodulo(image_cap_max - image_cap_min, 360); // panorama total angle
  1087. if (total_angle == 0){total_angle = 360}; // if panorama loops
  1088. var half_angle = total_angle * (half_width / image_width);
  1089. // min and max visible cap
  1090. var bearing = parseInt($('#angle_ctrl').val());
  1091. var cap_min = fmodulo(bearing - half_angle, 360);
  1092. var cap_max = fmodulo(bearing + half_angle, 360);
  1093. // check outside border
  1094. if (! image_loop){
  1095. if (x - half_width < 0){cap_min = image_cap_min};
  1096. if (x + half_width > image_width){cap_max = image_cap_max};
  1097. };
  1098. // check if we repeat the pano
  1099. if ( half_angle > 180 && image_loop){
  1100. var cap_min = 0;
  1101. var cap_max = 360;
  1102. };
  1103. return {cap_min: cap_min, cap_max : cap_max}
  1104. };
  1105. function load_map(){
  1106. /* Create the map object with the view cone and bearing object
  1107. */
  1108. // initialize the map object (global, to be view from update_map())
  1109. map = L.map('mapid').setView([panorama_lat, panorama_lng], 13);
  1110. // create the tile layer with correct attribution
  1111. var osmUrl='https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
  1112. var osmAttrib='Map data © <a href="https://openstreetmap.org">OpenStreetMap</a> contributors';
  1113. var osm = new L.TileLayer(osmUrl, {attribution: osmAttrib});
  1114. map.addLayer(osm);
  1115. map.addLayer( markerClusters );
  1116. map.addLayer(pointsOfInterest);
  1117. L.marker([panorama_lat, panorama_lng]).addTo(map);
  1118. map.on('click', function(e) {
  1119. /* Compute the new cap to show given the clic lat/lng */
  1120. var lat = toRad(e.latlng.lat); // latitude
  1121. var phi = toRad(e.latlng.lng); // longitude
  1122. var lat_ref = toRad(panorama_lat);
  1123. var phi_ref = toRad(panorama_lng);
  1124. // angle between the ref_point and the clic_point
  1125. var a = Math.acos( Math.cos(lat_ref) * Math.cos(phi - phi_ref) * Math.cos(lat) + Math.sin(lat_ref) * Math.sin(lat) );
  1126. // azimuth between the ref_point and the clic_point (=new cap)
  1127. var B = Math.asin( Math.sin(phi - phi_ref) * Math.sin(Math.PI/2 - lat)/Math.sin(a));
  1128. // little hack because asin give an angle in [-pi/2, pi/2]
  1129. if (lat>lat_ref){
  1130. var newCap = toDeg(B);
  1131. } else {
  1132. var newCap = toDeg(Math.PI-B);
  1133. }
  1134. // change the cap
  1135. angle_control = document.getElementById('angle_ctrl');
  1136. angle_control.value = newCap;
  1137. // update the panorama & minimap
  1138. change_angle(); // update panorama
  1139. update_map(); // update minimap
  1140. });
  1141. update_map();
  1142. };
  1143. function update_map(){
  1144. /* Update the map: view cone and bearing
  1145. */
  1146. if (map_never_drawn){
  1147. map_never_drawn = false;
  1148. } else {
  1149. map.removeLayer(viewField);
  1150. map.removeLayer(viewDirection);
  1151. };
  1152. var bearing = $('#angle_ctrl').val();
  1153. var cap = getCapMinMaxVisible();
  1154. /* TODO: allow to configure the maximum distance. */
  1155. viewField = getCone(panorama_lat,panorama_lng,bearing,cap,50000);
  1156. viewDirection = L.polygon([[panorama_lat, panorama_lng],[destVincenty(panorama_lat, panorama_lng, bearing, 50000).lat,destVincenty(panorama_lat, panorama_lng, bearing, 50000).lng]],{color: '#2880ca', opacity: 1, weight: 2});
  1157. viewField.addTo(map);
  1158. viewDirection.addTo(map);
  1159. }