pano.js 44 KB

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