Projet

Général

Profil

Paste
Télécharger (29,7 ko) Statistiques
| Branche: | Révision:

root / js / pano.js @ f524783f

1
if (img_prefix == undefined) var img_prefix = 'http://pano.tetaneutral.net/data/tsf2/vpongnian/tiles/ttn_mediatheque/mediatheque_70';
2
if (to_cap == undefined) var to_cap = 0;
3
if (to_ele == undefined) var to_ele = 0;
4
if (to_zoom == undefined) var to_zoom = 0;
5
if (cap == undefined) var cap = 0;
6
if (cap_min == undefined) var cap_min = cap;
7
if (cap_max == undefined) var cap_max = cap_min+360;
8
if (ref_points == undefined) var ref_points = new Array();
9
if (image_loop == undefined) var image_loop = true;
10

    
11
var pt_alt;
12
var pt_lat;
13
var pt_lon;
14

    
15
var debug_mode = false;
16
var canvas;
17
var cntext;
18
var point_list = new Array();
19
var zoom = 0;
20
var zooms = new Array();
21
var prev_zm;
22
var zm;
23
var tile = {width:256, height:256};
24
var ntiles = {x:228, y:9};
25
var border_width = 2;
26
var imageObj = new Array();
27

    
28
var last  = {x:0,y:0};
29
var shift = {x:0,y:0};
30
var mouse = {x:0,y:0};
31
var speed = {x:0,y:0};
32
var canvas_pos = {x:0,y:0};
33
var tmt;
34
var is_located = false;
35
var point_colors = {'pano_point' : '255,128,128',
36
                    'ref_point'  : '128,128,255',
37
                    'loc_point'  : '128,255,128',
38
                    'temporary'  : '255,255,128',
39
                    'unlocated'  : '255,255,255'};
40
var test = {x:0, y:0, i:100};
41

    
42

    
43
function nmodulo(val, div) {                // pour obtenir un modulo dans l'espace des nombres naturels N.
44
    return Math.floor((val%div+div)%div);   // il y a peut être plus simple, mais en attendant .... 
45
}
46

    
47
function fmodulo(val, div) {                // pour obtenir un modulo dans l'espace des nombres réels positifs.
48
    return (val%div+div)%div;               // il y a peut être plus simple, mais en attendant .... 
49
}
50

    
51
function distort_canvas(p, x, y) {
52
    if (p == 0) distort = 0;
53
    else {
54
        cntext.save();
55
        distort++;
56
        cntext.clearRect(0, 0, canvas.width, 2*canvas.height);
57
        var ratio = (canvas.width-2*distort)/canvas.width;
58
        var shift = canvas.height/2*(1-ratio);
59
        cntext.scale(1, ratio);
60
        if (p == 1) cntext.translate(0, 0);
61
        else if (p == -1) cntext.translate(0, 0);
62
        draw_image(x, y);
63
        cntext.restore();
64
        document.getElementById('res').innerHTML = 'distort : ' + distort + ' shift ' + shift + ' ratio : ' + ratio + '<br/>';
65
    }
66
}
67

    
68
function draw_image(ox, oy) {
69
    var ref_vals  = {x:last.x, y:last.y, zoom:zoom};
70
    ox = nmodulo(ox-canvas.width/2, zm.im.width);        // pour placer l'origine au centre du canvas 
71
    oy = Math.floor(oy-canvas.height/2);                 // pas de rebouclage vertical
72

    
73
    cntext.clearRect(0, 0, canvas.width, canvas.height);
74
    cntext.fillStyle = "rgba(128,128,128,0.8)";
75
    
76
    if (canvas.height > zm.im.height) {
77
        var fy = Math.floor((oy+canvas.height/2-zm.im.height/2)/(tile.height*zm.ntiles.y))*zm.ntiles.y;
78
        if (fy < 0) fy = 0; 
79
        var ly = fy + zm.ntiles.y;
80
    } else {
81
        var fy = Math.floor(oy/tile.height);
82
        var ly = Math.floor((oy+canvas.height+tile.height-1)/tile.height+1);
83
        if (fy < 0) fy = 0; 
84
        if (ly > zm.ntiles.y) ly = zm.ntiles.y; 
85
    }
86

    
87
    for (var j=fy; j<ly; j++) {
88
        var delta_y = (Math.floor(j/zm.ntiles.y) - Math.floor(fy/zm.ntiles.y)) * (tile.height - zm.last_tile.height);
89
        var dy = j*tile.height - oy - delta_y;
90
        var ny = j%ntiles.y;
91
        var wy = zm.tile.width;
92
        if (ny == zm.ntiles.y - 1) wy = zm.last_tile.height;
93

    
94
        var cpx = 0;
95
        var i = 0;
96
        var Nx = zm.ntiles.x;
97
        while (cpx < ox+canvas.width) {
98
            var cur_width = zm.tile.width;
99
            if (i%Nx == zm.ntiles.x-1) cur_width = zm.last_tile.width;
100
            if (cpx >= ox-cur_width) {
101
                var nx = i%Nx;
102
                var idx = nx+'-'+ny+'-'+ref_vals.zoom;
103
                if (imageObj[idx] && imageObj[idx].complete) {
104
                    draw_tile(idx, cpx-ox, dy); // l'image est déja en mémoire, on force le dessin sans attendre.
105
                } else {
106
                    var fname = get_file_name(nx, ny, ref_vals.zoom);
107
                    imageObj[idx] = new Image();
108
                    imageObj[idx].src = fname;
109
                    var ts = zm.get_tile_size(nx, ny);
110
                    cntext.fillRect(cpx-ox, dy, ts.width, ts.height);
111
                    imageObj[idx].addEventListener('load', (function(ref, idx, dx, dy, ox, oy, ts) {
112
                        return function() {        // closure nécéssaire pour gestion assynchronisme !!!
113
                            draw_tile_del(ref, idx, dx, dy, ox, oy, ts.width, ts.height);
114
                        };
115
                    })(ref_vals, idx, cpx-ox, dy, ox, oy, ts), false);
116
                }
117
//                load_image(zoom, nx, ny, shx, shy, cpx-ox, dy, ox, oy);
118
            }
119
            cpx += cur_width;
120
            i++;
121
        }
122
    }
123
    drawDecorations(ox, oy);
124
}
125

    
126
function draw_tile_del(ref, idx, tx, ty, ox, oy, twidth, theight) {
127
    if (ref.zoom == zoom && ref.x == last.x && ref.y == last.y) {
128
        draw_tile(idx, tx, ty);
129
        drawDecorations(ox, oy, tx, ty, twidth, theight);
130
    }
131
}
132

    
133
function draw_tile(idx, ox, oy) {
134
    var img = imageObj[idx];
135
    cntext.drawImage(img, ox, oy);
136
}
137

    
138
function drawDecorations(ox, oy, tx, ty, twidth, theight) {
139
    if (twidth) {
140
        cntext.save();
141
        cntext.beginPath();
142
        cntext.rect(tx, ty, twidth, theight);
143
        cntext.clip();
144
    } 
145
    var wgrd = zm.im.width/360;
146
    var od = ((ox+canvas.width/2)/wgrd)%360;
147
    var el = (zm.im.height/2 - (oy+canvas.height/2))/wgrd;
148
    cntext.fillStyle = "rgba(0,128,128,0.9)";
149
    cntext.strokeStyle = "rgb(255,255,255)";
150
    cntext.lineWidth = 1;
151
    cntext.fillRect(canvas.width/2-5, canvas.height/2-5, 10, 10);
152
    cntext.strokeRect(canvas.width/2-5, canvas.height/2-5, 10, 10);
153
    for(var i = 0; i < zm.pt_list.length; i++) {
154
        if (zm.pt_list[i]['type'] != 'unlocated') {
155
            cntext.fillStyle = 'rgba('+point_colors[zm.pt_list[i]['type']]+',0.5)';
156
            var cx = nmodulo(zm.pt_list[i]['xc'] - ox, zm.im.width);
157
            var cy = zm.pt_list[i]['yc'] - oy;
158
            cntext.beginPath();
159
            cntext.arc(cx, cy, 20, 0, 2*Math.PI, true);
160
            cntext.fill();
161
        }
162
    }
163

    
164
    //cntext.font = "20pt Arial";
165
    //cntext.fillRect(0, 0, 200, 20);
166
    //cntext.fillStyle = "rgb(255,0,0)";
167
    //cntext.fillText(od.toFixed(2), 5, 20);
168
    //for (i=0; i<canvas.width/wgrd; i++) {
169
        //cntext.strokeRect(i*wgrd, 0, wgrd, 20);
170
    //}
171
    if (twidth) {
172
        cntext.restore();
173
    }
174
    
175
}
176

    
177
function insert_drawn_point(lat, lon, alt) {
178
    var rt = 6371;  // Rayon de la terre
179
    var alt1 = pt_alt;
180
    var lat1 = pt_lat*Math.PI/180;
181
    var lon1 = pt_lon*Math.PI/180;
182
    var alt2 = alt;
183
    var lat2 = lat*Math.PI/180;
184
    var lon2 = lon*Math.PI/180;
185
    
186
    var dLat = lat2-lat1;
187
    var dLon = lon2-lon1; 
188
   
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
   
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
    
198
    return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI};   // les résultats sont en degrés
199
}
200

    
201
function localate_point() {
202
    var lat = document.getElementById("loca_latitude").value;
203
    var lon = document.getElementById("loca_longitude").value;
204
    var alt = document.getElementById("loca_altitude").value;
205
    if (lat == '' || isNaN(lat) || lat <= -90 || lat > 90) {
206
        alert("La latitude "+lat+"n'est pas correcte");
207
        return;
208
    }
209
    if (lat == '' || isNaN(lon) || lon <= -180 || lon > 180) {
210
        alert("La longitude "+lon+"n'est pas correcte");
211
        return;
212
    }
213
    if (lat == '' || isNaN(alt) || alt < -400 || alt > 10000000) {
214
        alert("l'altitude "+alt+"n'est pas correcte");
215
        return;
216
    }
217
    var opt_ced = new Array();
218
    opt_dce = insert_drawn_point(lat, lon, alt);
219
    display_temp(opt_dce.d, opt_dce.cap, opt_dce.ele);
220
}
221

    
222
function display_temp(d,cap,ele) {  
223
    point_list[point_list.length] = new Array("point temporaire", d,cap,ele, "temporary");
224
    reset_zooms();
225
    putImage(last.x, last.y);
226
}
227

    
228
function arrayUnset(array, value){
229
    array.splice(array.indexOf(value), 1);
230
}
231

    
232
function erase_point() {        
233
        for (var i=0; i<point_list.length; i++) {
234
                if(point_list[i][0] == "point temporaire"){
235
                        arrayUnset(point_list,point_list[i]);
236
                        loop = erase_point();
237
                }        
238
        }        
239
        reset_zooms();
240
    putImage(last.x, last.y);   
241
}
242

    
243
function get_file_name(x, y, z) { // recherche du fichier correspondant au zoom et à la position
244
    var prm = [z, x, y];
245
    var fname = img_prefix;
246
    for (var i = 0; i < prm.length; i++) {
247
        fname += '_';
248
        if (prm[i] < 10) fname += '00';
249
        else if (prm[i] < 100) fname += '0';
250
        fname += prm[i];
251
    }
252
    fname += '.jpg';
253
    return fname;
254
}
255

    
256
function keys(key) {
257
        
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 66: // b
270
        alert(key.pageX);
271
        test.x=tile.width*(ntiles.x-3);
272
        test.y=0;
273
        putImage(test.x, test.y);
274
        return;
275
    case 67: // c
276
        test.x=0;
277
        test.y=tile.height*(ntiles.y-3);
278
        putImage(test.x, test.y);
279
        return;*/
280
    case 36: // home
281
        putImage(0, zm.im.height/2);
282
        return;
283
    case 35: // end
284
        putImage(last.x+zm.im.width/2, last.y);
285
        return;
286
    case 39: // left
287
        putImage(last.x+40, last.y);
288
        return;
289
    case 40: // up
290
        putImage(last.x, last.y+20);
291
        return;
292
    case 37: // right
293
        putImage(last.x-40, last.y);
294
        return;
295
    case 38: // down
296
        putImage(last.x, last.y-20);
297
        return;
298
    case 33: // pageup
299
        zoom_control.value--;
300
        change_zoom()
301
        return;
302
    case 34: // pagedown
303
        zoom_control.value++;
304
        change_zoom()
305
        return;
306
    default:
307
//        alert(key.which)
308
        return;
309
    }
310
}
311

    
312
function onImageClick(e) {
313
    hide_contextmenu();
314
    shift.x = last.x;
315
    shift.y = last.y;
316
    speed.x = 0;
317
    speed.y = 0;
318
    mouse.x = e.pageX;
319
    mouse.y = e.pageY;
320
    clearTimeout(tmt);  //arrêt de l'éffet eventuel d'amorti en cours.
321
    canvas.addEventListener('mousemove', stickImage, false);
322
    canvas.addEventListener('mouseup', launchImage, false);
323
    //canvas.addEventListener('mouseout', launchImage, false);
324
    canvas.style.cursor='move';
325
    //document.onmousemove = stickImage;
326
    document.onmouseup = launchImage;
327
    hide_links();
328
}
329

    
330

    
331
function stickImage(e) {
332
    var xs = mouse.x - e.pageX + shift.x;
333
    var ys = mouse.y - e.pageY + shift.y;
334
    speed.x = xs - last.x;  //mémorisation des vitesses horizontales
335
    speed.y = ys - last.y;  //et verticales lors de ce déplacement
336
    putImage(xs, ys);
337
}
338

    
339
function launchImage(e) {
340
    distort_canvas(0);
341
    canvas.removeEventListener('mousemove', stickImage, false);
342
    //document.onmousemove = null;
343
    shift.x = e.pageX - mouse.x + shift.x;
344
    shift.y = e.pageY - mouse.y + shift.y;
345
    tmt = setTimeout(inertialImage, 100);
346
}
347

    
348
function putImage(x, y) { // est destiné à permettre l'effet d'amortissement par la mémorisation de la position courante.
349
    if (!zm.is_updated) return; 
350
    if (x >= zm.im.width) {   // rebouclage horizontal
351
        shift.x -= zm.im.width;
352
        x -= zm.im.width;
353
    } else if (x < 0) {
354
        shift.x += zm.im.width;
355
        x += zm.im.width;
356
    }
357
    if (y >= zm.im.height) {   // pas de rebouclage vertical mais blocage
358
        //distort_canvas(1, x, y);
359
        shift.y = zm.im.height-1;
360
        y = zm.im.height-1;
361
    } else if (y < 0) {
362
        //distort_canvas(-1, x, y);
363
        shift.y = 0;
364
        y = 0;
365
    }
366

    
367
    last.x = x;
368
    last.y = y;
369
    draw_image(x, y);
370
}
371

    
372
function inertialImage() {
373
    speed.x *= 0.9;
374
    speed.y *= 0.9;
375
    if (Math.abs(speed.x) > 2 || Math.abs(speed.y) > 2) {
376
        putImage(last.x+speed.x, last.y+speed.y);
377
        tmt = setTimeout(inertialImage, 100);
378
    } else {
379
        show_links();
380
    }
381
}
382

    
383
function tri_ref_points(v1, v2) {
384
    return v1['x'] - v2['x'];
385
}
386

    
387

    
388

    
389
function tzoom(zv) {
390
    this.value = zv;
391
    this.ntiles = {x:0,y:0};
392
    this.tile = {width:0,height:0};
393
    this.last_tile = {width:0,height:0};
394
    this.max_tile = {width:0,height:0};
395
    this.im = {width:0,height:0};
396
    this.is_updated = false;
397

    
398
    this.refresh = function() {
399
        this.im.visible_width = this.tile.width*(this.ntiles.x-1)+this.last_tile.width;
400
        this.is_updated = true;
401

    
402
        this.im.width = this.im.visible_width;
403
        this.im.height = this.tile.height*(this.ntiles.y-1)+this.last_tile.height;
404
        if (this.last_tile.width > this.tile.width) this.max_tile.width = this.im.last_tile.width;
405
        else this.max_tile.width = this.tile.width;
406
        if (this.last_tile.height > this.tile.height) this.max_tile.height = this.im.last_tile.height;
407
        else this.max_tile.height = this.tile.height;
408

    
409
        var ord_pts = new Array();
410
        i=0;
411
        for(var label in ref_points) {
412
            ord_pts[i++] = ref_points[label]
413
        }
414
        ord_pts = ord_pts.sort(tri_ref_points);
415
        is_located = i > 1 || image_loop && i > 0;
416

    
417
        var alpha_domain = {start:0, end:360}; 
418
        this.pixel_y_ratio = this.im.width/360;
419
        if (is_located) {
420
            this.ref_pixels = new Array;
421
            this.ref_pixels[0] = new Array();    // Attention il faut compter un intervalle de plus !
422
            for (var i=0; i < ord_pts.length; i++) { // premier parcours pour les paramètres cap/x
423
                this.ref_pixels[i+1] = new Array();
424
                this.ref_pixels[i+1].x = Math.floor(ord_pts[i].x*this.im.width);
425
                this.ref_pixels[i+1].cap = fmodulo(ord_pts[i].cap, 360);
426
                if (i != ord_pts.length-1) {
427
                    this.ref_pixels[i+1].ratio_x = (ord_pts[i+1].x - ord_pts[i].x)/fmodulo(ord_pts[i+1].cap - ord_pts[i].cap, 360)*this.im.width;
428
                }
429
            }
430
            if (image_loop == true) {
431
                var dpix = this.im.width;
432
                var dangle = 360;
433
                if (ord_pts.length > 1) {
434
                    dpix = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
435
                    dangle = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[this.ref_pixels.length-1].cap, 360);
436
                }
437
                this.ref_pixels[0].ratio_x = dpix/dangle;
438
                this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[0].ratio_x;
439
                dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
440
                this.ref_pixels[0].cap = fmodulo(this.ref_pixels[ord_pts.length].cap + dpix / this.ref_pixels[0].ratio_x, 360);
441
            } else {
442
                this.ref_pixels[0].ratio_x = this.ref_pixels[1].ratio_x;
443
                this.ref_pixels[ord_pts.length].ratio_x = this.ref_pixels[ord_pts.length-1].ratio_x;
444
                this.ref_pixels[0].cap = fmodulo(this.ref_pixels[1].cap - this.ref_pixels[1].x / this.ref_pixels[1].ratio_x, 360);
445
                alpha_domain.start = this.ref_pixels[0].cap;
446
                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);
447
                this.pixel_y_ratio = this.im.width/fmodulo(alpha_domain.end-alpha_domain.start, 360);
448
            }
449
            this.ref_pixels[0].x = 0;
450

    
451
            for (var i=0; i < ord_pts.length; i++) { // second parcours pour les paramètres elevation/y
452
                this.ref_pixels[i+1].shift_y = Math.floor(this.pixel_y_ratio*ord_pts[i].ele - ord_pts[i].y*this.im.height);
453
                if (i != ord_pts.length-1) {
454
                    var next_shift = Math.floor(this.pixel_y_ratio*ord_pts[i+1].ele - ord_pts[i+1].y*this.im.height);
455
                    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);
456
                }
457
            }
458

    
459
            if (image_loop == true) {
460
                var dpix  = this.im.width;
461
                var delt = 0;
462
                if (ord_pts.length > 1) {
463
                    dpix  = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
464
                    delt = this.ref_pixels[this.ref_pixels.length-1].shift_y - this.ref_pixels[1].shift_y;
465
                }
466
                this.ref_pixels[0].dshft_y = delt/dpix;
467
                this.ref_pixels[ord_pts.length].dshft_y = this.ref_pixels[0].dshft_y;
468
                dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
469
                this.ref_pixels[0].shift_y = this.ref_pixels[ord_pts.length].shift_y - dpix*this.ref_pixels[0].dshft_y;
470
            } else {
471
                this.ref_pixels[0].shift_y = this.ref_pixels[1].shift_y;
472
                this.ref_pixels[0].dshft_y = 0;
473
                this.ref_pixels[ord_pts.length].dshft_y = 0;
474
            }
475

    
476
            if (debug_mode) {
477
                var res = document.getElementById('res');
478
                res.innerHTML = 'liste des '+this.ref_pixels.length+' valeurs de correction (image = '+this.im.width+'x'+this.im.height+') zoom = '+this.value+':<br/>';
479
                for (var i=0; i < this.ref_pixels.length; i++) { // pour le debug
480
                    res.innerHTML += '<p>point '+i+' :</p><ul>';
481
                    for (var key in this.ref_pixels[i]) { // pour le debug
482
                        res.innerHTML += '<li>'+key + '['+i+'] = '+this.ref_pixels[i][key]+'</li>';
483
                    }
484
                    if (i != this.ref_pixels.length-1) {
485
                        var tx0 = this.ref_pixels[i+1].x-1;
486
                        //var ty0 = this.ref_pixels[i+1].shift_y;
487
                        var ty0 = 0;
488
                    } else {
489
                        var tx0 = this.im.width-1;
490
                        var ty0 = 0;
491
                    }
492
                    res.innerHTML += '</ul><p>test sur : '+tx0+','+ty0+'</p>';
493
                    var tst = this.get_cap_ele(tx0, ty0);
494
                    res.innerHTML += '<p>cap:'+tst.cap+', shift:'+tst.ele+'</p>';
495
                    var tst2 = this.get_pos_xy(tst.cap, tst.ele);
496
                    res.innerHTML += '</ul><p>x:'+tst2.x+', y:'+tst2.y+'</p>';
497
                }
498
            }
499
        }
500

    
501
        this.pt_list = new Array();
502
        for (var i=0; i<point_list.length; i++) {
503
            var lbl = point_list[i][0];
504
            var dst = point_list[i][1];
505
            var cap = point_list[i][2];
506
            var ele = point_list[i][3];
507
            var lnk = point_list[i][4];
508
            var typ = 'unlocated';
509
            var rxy = this.get_pos_xy(cap, ele);
510
            var is_visible = fmodulo(cap - alpha_domain.start, 360) <= fmodulo(alpha_domain.end - alpha_domain.start -0.0001, 360)+0.0001 && is_located;
511

    
512
            this.pt_list[i] = new Array();
513
            if (ref_points[lbl] != undefined) {
514
                typ = 'ref_point';
515
                if (!is_located) rxy = {x:ref_points[lbl].x*this.im.width, y:ref_points[lbl].y*this.im.height}
516
            } else if(lnk == '' && is_visible && lbl != 'point temporaire') {
517
                typ = 'loc_point';
518
            }else if(is_visible && lbl =='point temporaire') {
519
            typ = 'temporary';
520
            
521
            } else if(is_visible) {
522
                typ = 'pano_point';
523
                lnk += '&to_zoom='+this.value;
524
            } 
525
            this.pt_list[i]['type'] = typ;
526
            this.pt_list[i]['cap'] = cap;
527
            this.pt_list[i]['ele'] = ele;
528
            this.pt_list[i]['dist'] = dst;
529
            this.pt_list[i]['label'] = lbl;
530
            this.pt_list[i]['lnk'] = lnk;
531
            this.pt_list[i]['xc'] = rxy.x;
532
            this.pt_list[i]['yc'] = Math.floor(this.im.height/2 - rxy.y);
533
        }
534
    }
535
    
536
    this.get_tile_size = function(nx, ny) {
537
        var res = {width:0, height:0};
538
        if (nx == this.ntiles.x-1) res.width = this.last_tile.width;
539
        else res.width = this.tile.width;
540
        if (ny == this.ntiles.y-1) res.height = this.last_tile.height;
541
        else res.height = this.tile.height;
542
        return res;
543
    }
544
    
545
    this.get_cap_ele = function(px, py) {               // recherche d'un cap et d'une élévation à partir d'un pixel X,Y.
546
        if (is_located) {
547
            for (var i=0; i < this.ref_pixels.length; i++) {
548
                if (i == this.ref_pixels.length - 1 || px < this.ref_pixels[i+1].x) {
549
                    var dpix = px-this.ref_pixels[i].x;
550
                    var cp = fmodulo(this.ref_pixels[i].cap + dpix/this.ref_pixels[i].ratio_x, 360);
551
                    var el = (py+this.ref_pixels[i].shift_y+this.ref_pixels[i].dshft_y*dpix)/this.pixel_y_ratio;
552
                    return {cap:cp, ele:el};
553
                }
554
            }
555
        } else {
556
            var cp = 360*px/this.im.width;
557
            var el = 360*py/this.im.height;
558
            return {cap:cp, ele:el};
559
        }
560
    }
561
    
562
    this.get_pos_xy = function(cap, ele) {                  // recherche des coordonnées pixel à partir d'un cap et d'une élévation.
563
        if (is_located) {
564
            var dcap = fmodulo(cap-this.ref_pixels[0].cap, 360);
565
            for (var i=0; i < this.ref_pixels.length; i++) {
566
                if (i == this.ref_pixels.length - 1 || dcap < fmodulo(this.ref_pixels[i+1].cap-this.ref_pixels[0].cap, 360)) {
567
                    var px = this.ref_pixels[i].x + this.ref_pixels[i].ratio_x*fmodulo(cap - this.ref_pixels[i].cap, 360);
568
                    var dpix = px-this.ref_pixels[i].x;
569
                    var py = this.pixel_y_ratio*ele - this.ref_pixels[i].shift_y - this.ref_pixels[i].dshft_y*dpix;
570
                    return {x:px, y:py};
571
                }
572
            }
573
        } else {
574
            var px = fmodulo(cap, 360)/360*this.im.width;
575
            var py = ele/360*this.im.height;
576
            return {x:px, y:py};
577
        }
578
    }
579
}
580

    
581
function reset_zooms () {
582
    for(i=0; i<zooms.length; i++) zooms[i].is_updated = false;
583
    zm.refresh();
584
}
585

    
586
function wheel_zoom (event) {
587
    var zshift = {x:0, y:0};
588
    if (event.pageX != undefined && event.pageX != undefined) {
589
        zshift.x = event.pageX-canvas.width/2-canvas_pos.x;
590
        zshift.y = event.pageY-canvas.height/2-canvas_pos.y;
591
    }
592
    event.preventDefault();
593
    if (event.wheelDelta < 0 && zoom_control.value < zoom_control.max) {
594
        zoom_control.value++;
595
        change_zoom(zshift.x, zshift.y);
596
    } else if (event.wheelDelta > 0 && zoom_control.value > zoom_control.min) {
597
        zoom_control.value--; 
598
        change_zoom(zshift.x, zshift.y);
599
    }
600
}
601

    
602
function change_zoom(shx, shy) {
603
    var zoom_control = document.getElementById("zoom_ctrl");
604
    var v = zoom_control.value;
605

    
606
    prev_zm = zm;
607

    
608
    if (zooms[v]) {
609
        if (!zooms[v].is_updated) zooms[v].refresh();
610
    } else {
611
        zooms[v] = new tzoom(v);
612
    }
613

    
614
    if (zooms[v].is_updated) {
615
        if (shx == undefined || shy == undefined) {
616
            shx=0;
617
            shy=0;
618
        }
619
        zm = zooms[v];
620
        var px = (last.x+shx)*zm.im.width/prev_zm.im.width - shx;
621
        var py = (last.y+shy)*zm.im.height/prev_zm.im.height - shy;
622
        if (py < zm.im.height && py >= 0) {
623
            zoom = zm.value;
624
            tile = zm.tile;
625
            ntiles = zm.ntiles;
626
            putImage(px, py);
627
        } else {
628
            zm = prev_zm;
629
            zoom_control.value = zm.value;
630
        }
631
    }
632
}
633

    
634
function change_angle() {
635
    var elvtn_control = document.getElementById('elvtn_ctrl');
636
    var angle_control = document.getElementById('angle_ctrl');
637
    var resxy = zm.get_pos_xy(angle_control.value, elvtn_control.value);
638
    var pos_x = resxy.x;
639
    var pos_y = Math.floor(zm.im.height/2 - resxy.y);
640
    putImage(pos_x, pos_y);
641
}
642

    
643
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. 
644
    return Math.sqrt(x*x + y*y) < r;
645
}
646

    
647
function check_links(e) {
648
    var mouse_x = e.pageX-canvas_pos.x;
649
    var mouse_y = e.pageY-canvas_pos.y;
650
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
651
    var pos_y = last.y + mouse_y - canvas.height/2;
652
    for(var i = 0; i < zm.pt_list.length; i++) {
653
        if (is_located && zm.pt_list[i]['type'] == 'pano_point') {
654
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
655
                if (zm.pt_list[i]['lnk'] != '') window.location = zm.pt_list[i]['lnk'];
656
                break;
657
            }
658
        }
659
    }
660
}
661

    
662
function display_links(e) {
663
    var info = document.getElementById('info');
664
    var mouse_x = e.pageX-canvas_pos.x;
665
    var mouse_y = e.pageY-canvas_pos.y;
666
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
667
    var pos_y = last.y + mouse_y - canvas.height/2;
668
    //var cap = ((pos_x/zm.im.width)*360).toFixed(2);
669
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
670
    //var ele = ((zm.im.height/2 - pos_y)/zm.im.width)*360;
671
    info.innerHTML = 'élévation : '+res.ele.toFixed(2)+'<br/>cap : '+res.cap.toFixed(2);
672
    info.style.top = e.pageY+'px';
673
    info.style.left = e.pageX+'px';
674
    info.style.backgroundColor = '#FFC';
675
    info.style.display = 'block';
676
    canvas.style.cursor='crosshair';
677
    for(var i = 0; i < zm.pt_list.length; i++) {
678
        if (is_located || zm.pt_list[i]['type'] == 'ref_point') {
679
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
680
                info.innerHTML = zm.pt_list[i]['label'];
681
                if (zm.pt_list[i]['dist'] < 10) var dst = Math.round(zm.pt_list[i]['dist']*1000)+' m';
682
                else var dst = zm.pt_list[i]['dist'].toFixed(1)+' kms';
683
                info.innerHTML += '<br/> à ' + dst;
684
                info.style.backgroundColor = 'rgb('+point_colors[zm.pt_list[i]['type']]+')';
685
                canvas.style.cursor='auto';
686
                break;
687
            }
688
        }
689
    }
690
}
691

    
692
function hide_links() {
693
    canvas.removeEventListener( "mousemove", display_links, false);
694
    var info = document.getElementById('info');
695
    info.style.display = 'none';
696
}
697

    
698
function show_links() {
699
    canvas.addEventListener( "mousemove", display_links, false);
700
//    var info = document.getElementById('info');
701
//    info.style.display = 'block';
702
}
703

    
704
function hide_contextmenu() {
705
    document.getElementById('insert').style.display = 'none';
706
}
707

    
708
function manage_ref_points(e) {
709
    //event.preventDefault();
710
    //event.stopPropagation();
711
    var insrt = document.getElementById('insert');
712
    document.getElementById('do-cancel').onclick = hide_contextmenu;
713
    insrt.style.left = e.pageX+'px';
714
    insrt.style.top = e.pageY+'px';
715
    insrt.style.display = 'block';
716
    var sel_pt = document.getElementById('sel_point');
717
    var do_insert = document.getElementById('do-insert');
718
    var do_delete = document.getElementById('do-delete');
719
    var show_cap = document.getElementById('show-cap');
720
    var pos_x = nmodulo(last.x + e.pageX - canvas_pos.x - canvas.width/2, zm.im.width);
721
    var pos_y = last.y + e.pageY - canvas_pos.y - canvas.height/2;
722
    for(var i = 0; i < zm.pt_list.length; i++) {
723
        if (zm.pt_list[i]['type'] == 'ref_point') {
724
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
725
                sel_pt.value = zm.pt_list[i]['label'];
726
            }
727
        }
728
    }
729
    do_delete.onclick = function() {delete_ref_point(insrt)};
730
    do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
731
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
732
    show_cap.onclick = function() {window.open("show_capline.php?cap="+res.cap+'&org_lat='+pt_lat+'&org_lon='+pt_lon)};
733
    return false;
734
}
735

    
736
function insert_ref_point(el, x, y) {
737
    var label;
738
    el.style.display = 'none';
739
    for(var i = 0; i < zm.pt_list.length; i++) {
740
        label = zm.pt_list[i]['label'];
741
        if(label == document.getElementById('sel_point').value) {
742
            var posx = nmodulo(last.x + x - canvas.width/2, zm.im.width)/zm.im.width;
743
            var posy = 0.5 - (last.y + y - canvas.height/2)/zm.im.height;
744
            var pval = {x:posx, y:posy, cap:zm.pt_list[i]['cap'], ele:zm.pt_list[i]['ele'], label:label};
745
            ref_points[label] = pval;
746
            document.getElementById('res').innerHTML = '<h4>Dernier point entré</h4>';
747
            document.getElementById('res').innerHTML += '<p>reference["'+label+'"] = '+posx.toFixed(5)+','+posy.toFixed(5)+'</p>';
748
            reset_zooms();
749
             putImage(last.x, last.y);
750
            break;
751
        }
752
    }
753
    show_result();
754
}
755

    
756
function show_result(clear_before) {
757
    var res = document.getElementById('res');
758
    var strg = '';
759
    for (var lbl in ref_points) {
760
        strg += '<li>reference["'+lbl+'"] = '+ref_points[lbl].x.toFixed(5)+','+ref_points[lbl].y.toFixed(5)+'</li>';
761
    }
762
    if (strg) strg = '<h3>Liste de tous les points de référence</h3>\n<ul>' + strg + '</ul>';
763
    if (clear_before) res.innerHTML = strg;
764
    else res.innerHTML += strg;
765
}
766

    
767
function delete_ref_point(el) {
768
    el.style.display = 'none';
769
    delete ref_points[document.getElementById('sel_point').value];
770
    reset_zooms();
771
    putImage(last.x, last.y);
772
    show_result(true);
773
}
774

    
775
function clean_canvas_events(e) {
776
    canvas.removeEventListener('mousemove', stickImage, false);
777
    document.getElementById('info').style.display = 'none';
778
    speed.x = 0;
779
    speed.y = 0;
780
}
781

    
782
canvas_set_size = function() {
783
    canvas.style.border = border_width+"px solid red";
784
    canvas.width = window.innerWidth-2*border_width;
785
    canvas.height = window.innerHeight-2*border_width;
786
    canvas_pos.x = canvas.offsetLeft+border_width;
787
    canvas_pos.y = canvas.offsetTop+border_width;
788
}
789

    
790
canvas_resize = function() {
791
    canvas_set_size();
792
    putImage(last.x, last.y);
793
}
794

    
795
function paramIn(e) {
796
    e = e || window.event; 
797
    var relatedTarget = e.relatedTarget || e.fromElement; 
798
    
799
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
800
        relatedTarget = relatedTarget.parentNode;
801
    }
802
    
803
    if (relatedTarget != adding && relatedTarget != localisation) {
804
        document.removeEventListener('keydown', keys, false);
805
    }
806
}
807

    
808
function paramOut(e) {
809
         
810
    e = e || window.event; 
811
    var relatedTarget = e.relatedTarget || e.toElement; 
812
 
813
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
814
        relatedTarget = relatedTarget.parentNode;
815
    }
816
 
817
    if (relatedTarget != adding && relatedTarget != localisation) {
818
            document.addEventListener('keydown', keys, false);
819
    }
820
 
821
}
822

    
823
window.onload = function() {        
824
    pt_alt = document.getElementById('pos_alt').childNodes[0].nodeValue;
825
    pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
826
    pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
827

    
828
    localisation = document.getElementById("locadraw");
829
    adding = document.getElementById("adding");
830
    canvas = document.getElementById("mon-canvas");
831
    cntext = canvas.getContext("2d");
832
    canvas_set_size();
833
    canvas.addEventListener("click", check_links, false);
834
    //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
835
    canvas.oncontextmenu = manage_ref_points;
836
    canvas.addEventListener("mouseout" , clean_canvas_events, false);
837
    show_links();
838

    
839
    var max_zoom = zooms.length - 1;
840
    zoom_control = document.getElementById("zoom_ctrl");
841
    zoom_control.onchange = change_zoom;
842
    zoom_control.max = max_zoom;
843
    if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
844
    zm = zooms[to_zoom];
845
    zoom_control.value = to_zoom;
846
    zm.refresh();
847

    
848
    zoom = zm.value;
849
    tile = zm.tile;
850
    ntiles = zm.ntiles;
851

    
852
    angle_control = document.getElementById("angle_ctrl");
853
    angle_control.value = to_cap;
854
    angle_control.onchange = change_angle;
855
    angle_control.onclick = change_angle;
856
    elvtn_control = document.getElementById("elvtn_ctrl");
857
    elvtn_control.value = to_ele;
858
    elvtn_control.onchange = change_angle;
859
    elvtn_control.onclick = change_angle;
860

    
861
    change_angle();
862
    loca_temp = document.getElementById("loca_show");
863
    if (loca_temp) {
864
        loca_temp.onclick = showLoca;
865
        loca_temp = document.getElementById("loca_hide");
866
        loca_temp.onclick = hideLoca;
867
        loca_temp = document.getElementById("loca_button");
868
        loca_temp.onclick = localate_point;
869
        loca_erase = document.getElementById("loca_erase");
870
        loca_erase.onclick = erase_point;
871
        localisation.addEventListener('mouseover',paramIn,false);
872
        localisation.addEventListener('mouseout',paramOut,false);
873
    }
874
    canvas.addEventListener('mousedown', onImageClick, false);
875
    document.addEventListener('keydown', keys, false);
876
    canvas.addEventListener('mousewheel', wheel_zoom, false);
877
    window.onresize = canvas_resize;
878
    if (adding) {
879
        document.getElementById("paramFormHide").onclick = hideForm;
880
        document.getElementById("paramFormShow").onclick = showForm;
881
        adding.addEventListener('mouseover', paramIn, false);
882
        adding.addEventListener('mouseout', paramOut, false);
883
    }
884
};