Projet

Général

Profil

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

root / js / pano.js @ cb527757

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

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

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

    
39

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

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

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

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

    
70
    cntext.clearRect(0, 0, canvas.width, canvas.height);
71
    cntext.fillStyle = "rgba(128,128,128,0.8)";
72

    
73
    if (canvas.height > zm.im.height) {
74
        var fy = Math.floor((oy+canvas.height/2-zm.im.height/2)/(tile.height*zm.ntiles.y))*zm.ntiles.y;
75
        if (fy < 0) fy = 0;
76
        var ly = fy + zm.ntiles.y;
77
    } else {
78
        var fy = Math.floor(oy/tile.height);
79
        var ly = Math.floor((oy+canvas.height+tile.height-1)/tile.height+1);
80
        if (fy < 0) fy = 0;
81
        if (ly > zm.ntiles.y) ly = zm.ntiles.y;
82
    }
83

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

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

    
168
}
169

    
170
function insert_drawn_point(lat, lon, alt) {
171
    var rt = 6371;  // Rayon de la terre
172
    var pt_alt = document.getElementById('pos_alt').childNodes[0].nodeValue;
173
    var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
174
    var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
175

    
176
    var alt1 = pt_alt;
177
    var lat1 = pt_lat*Math.PI/180;
178
    var lon1 = pt_lon*Math.PI/180;
179
    var alt2 = alt;
180
    var lat2 = lat*Math.PI/180;
181
    var lon2 = lon*Math.PI/180;
182

    
183
    var dLat = lat2-lat1;
184
    var dLon = lon2-lon1;
185

    
186
    var a = Math.sin(dLat/2)*Math.sin(dLat/2) + Math.sin(dLon/2)*Math.sin(dLon/2)*Math.cos(lat1)*Math.cos(lat2);  //
187
    var angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
188
    var d = angle*rt;                    // distance du point en Kms
189

    
190
    var y = Math.sin(dLon) * Math.cos(lat2);
191
    var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
192
    var cap = Math.atan2(y,x);                 // cap pour atteindre le point en radians
193
    var e = Math.atan2((alt2 - alt1)/1000 - d*d/(2*rt),d);  // angle de l'élévation en radians
194

    
195
    return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI};   // les résultats sont en degrés
196
}
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

    
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

    
225
function arrayUnset(array, value){
226
    array.splice(array.indexOf(value), 1);
227
}
228

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

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

    
253
function keys(key) {
254

    
255
    hide_links();
256
    evt = key || event;
257
    //evt.preventDefault();
258
    //evt.stopPropagation();
259
    if (!key) {
260
        key = window.event;
261
        key.which = key.keyCode;
262
    }
263
//    alert(key);
264
//    if (!evt.shiftKey) return;
265
    switch (key.which) {
266
    /*case 66: // b
267
        alert(key.pageX);
268
        test.x=tile.width*(ntiles.x-3);
269
        test.y=0;
270
        putImage(test.x, test.y);
271
        return;
272
    case 67: // c
273
        test.x=0;
274
        test.y=tile.height*(ntiles.y-3);
275
        putImage(test.x, test.y);
276
        return;*/
277
    case 36: // home
278
        putImage(0, zm.im.height/2);
279
        return;
280
    case 35: // end
281
        putImage(last.x+zm.im.width/2, last.y);
282
        return;
283
    case 39: // left
284
        putImage(last.x+40, last.y);
285
        return;
286
    case 40: // up
287
        putImage(last.x, last.y+20);
288
        return;
289
    case 37: // right
290
        putImage(last.x-40, last.y);
291
        return;
292
    case 38: // down
293
        putImage(last.x, last.y-20);
294
        return;
295
    case 33: // pageup
296
        zoom_control.value--;
297
        change_zoom()
298
        return;
299
    case 34: // pagedown
300
        zoom_control.value++;
301
        change_zoom()
302
        return;
303
    default:
304
//        alert(key.which)
305
        return;
306
    }
307
}
308

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

    
327

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

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

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

    
364
    last.x = x;
365
    last.y = y;
366
    draw_image(x, y);
367
}
368

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

    
380
function tri_ref_points(v1, v2) {
381
    return v1['x'] - v2['x'];
382
}
383

    
384

    
385

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

    
395
    this.refresh = function() {
396
        this.im.visible_width = this.tile.width*(this.ntiles.x-1)+this.last_tile.width;
397
        this.is_updated = true;
398

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

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

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

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

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

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

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

    
509
            this.pt_list[i] = new Array();
510
            if (ref_points[lbl] != undefined) {
511
                typ = 'ref_point';
512
                if (!is_located) rxy = {x:ref_points[lbl].x*this.im.width, y:ref_points[lbl].y*this.im.height}
513
            } else if(lnk == '' && is_visible && lbl != 'point temporaire') {
514
                typ = 'loc_point';
515
            }else if(is_visible && lbl =='point temporaire') {
516
            typ = 'temporary';
517

    
518
            } else if(is_visible) {
519
                typ = 'pano_point';
520
                lnk += '&to_zoom='+this.value;
521
            }
522
            this.pt_list[i]['type'] = typ;
523
            this.pt_list[i]['cap'] = cap;
524
            this.pt_list[i]['ele'] = ele;
525
            this.pt_list[i]['dist'] = dst;
526
            this.pt_list[i]['label'] = lbl;
527
            this.pt_list[i]['lnk'] = lnk;
528
            this.pt_list[i]['xc'] = rxy.x;
529
            this.pt_list[i]['yc'] = Math.floor(this.im.height/2 - rxy.y);
530
        }
531
    }
532

    
533
    this.get_tile_size = function(nx, ny) {
534
        var res = {width:0, height:0};
535
        if (nx == this.ntiles.x-1) res.width = this.last_tile.width;
536
        else res.width = this.tile.width;
537
        if (ny == this.ntiles.y-1) res.height = this.last_tile.height;
538
        else res.height = this.tile.height;
539
        return res;
540
    }
541

    
542
    this.get_cap_ele = function(px, py) {               // recherche d'un cap et d'une élévation à partir d'un pixel X,Y.
543
        if (is_located) {
544
            for (var i=0; i < this.ref_pixels.length; i++) {
545
                if (i == this.ref_pixels.length - 1 || px < this.ref_pixels[i+1].x) {
546
                    var dpix = px-this.ref_pixels[i].x;
547
                    var cp = fmodulo(this.ref_pixels[i].cap + dpix/this.ref_pixels[i].ratio_x, 360);
548
                    var el = (py+this.ref_pixels[i].shift_y+this.ref_pixels[i].dshft_y*dpix)/this.pixel_y_ratio;
549
                    return {cap:cp, ele:el};
550
                }
551
            }
552
        } else {
553
            var cp = 360*px/this.im.width;
554
            var el = 360*py/this.im.height;
555
            return {cap:cp, ele:el};
556
        }
557
    }
558

    
559
    this.get_pos_xy = function(cap, ele) {                  // recherche des coordonnées pixel à partir d'un cap et d'une élévation.
560
        if (is_located) {
561
            var dcap = fmodulo(cap-this.ref_pixels[0].cap, 360);
562
            for (var i=0; i < this.ref_pixels.length; i++) {
563
                if (i == this.ref_pixels.length - 1 || dcap < fmodulo(this.ref_pixels[i+1].cap-this.ref_pixels[0].cap, 360)) {
564
                    var px = this.ref_pixels[i].x + this.ref_pixels[i].ratio_x*fmodulo(cap - this.ref_pixels[i].cap, 360);
565
                    var dpix = px-this.ref_pixels[i].x;
566
                    var py = this.pixel_y_ratio*ele - this.ref_pixels[i].shift_y - this.ref_pixels[i].dshft_y*dpix;
567
                    return {x:px, y:py};
568
                }
569
            }
570
        } else {
571
            var px = fmodulo(cap, 360)/360*this.im.width;
572
            var py = ele/360*this.im.height;
573
            return {x:px, y:py};
574
        }
575
    }
576
}
577

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

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

    
599
function change_zoom(shx, shy) {
600
    var zoom_control = document.getElementById("zoom_ctrl");
601
    var v = zoom_control.value;
602

    
603
    prev_zm = zm;
604

    
605
    if (zooms[v]) {
606
        if (!zooms[v].is_updated) zooms[v].refresh();
607
    } else {
608
        zooms[v] = new tzoom(v);
609
    }
610

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

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

    
640
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.
641
    return Math.sqrt(x*x + y*y) < r;
642
}
643

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

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

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

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

    
701
function hide_contextmenu() {
702
    document.getElementById('insert').style.display = 'none';
703
}
704

    
705
function manage_ref_points(e) {
706
    //event.preventDefault();
707
    //event.stopPropagation();
708
        var sel_pt = document.getElementById('sel_point');
709
    var do_insert = document.getElementById('do-insert');
710
        var do_delete = document.getElementById('do-delete');
711
        var do_cancel = document.getElementById('do-cancel');
712
        var show_cap = document.getElementById('show-cap');
713
        var insrt = document.getElementById('insert');
714

    
715
    var pos_x = nmodulo(last.x + e.pageX - canvas_pos.x - canvas.width/2, zm.im.width);
716
        var pos_y = last.y + e.pageY - canvas_pos.y - canvas.height/2;
717

    
718
        insrt.style.left = e.pageX+'px';
719
        insrt.style.top = e.pageY+'px';
720
        insrt.style.display = 'block';
721

    
722
        if (do_insert) {// true if there are ref points
723
                    for(var i = 0; i < zm.pt_list.length; i++) {
724
                            if (zm.pt_list[i]['type'] == 'ref_point') {
725
                                    if (check_prox(zm.pt_list[i]['xc']-pos_x,
726
                                                   zm.pt_list[i]['yc']-pos_y, 20)) {
727
                                            sel_pt.value = zm.pt_list[i]['label'];
728
                                    }
729
                            }
730
                    }
731
                do_delete.onclick = function() {delete_ref_point(insrt)};
732
                do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
733
                show_cap.onclick = function() {
734
                        window.open('show_capline.php?title='+encodeURIComponent(btoa(title))+'&cap='+res.cap+'&org_lat='+pt_lat+'&org_lon='+pt_lon+'&dist=120000');
735
                };
736
        }
737

    
738
        do_cancel.onclick = hide_contextmenu;
739
    var res = zm.get_cap_ele(pos_x, zm.im.height/2 - pos_y);
740
    var pt_lat = document.getElementById('pos_lat').childNodes[0].nodeValue;
741
    var pt_lon = document.getElementById('pos_lon').childNodes[0].nodeValue;
742
    return false;
743
}
744

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

    
765
function show_result(clear_before) {
766
    var res = document.getElementById('res');
767
    var strg = '';
768
    for (var lbl in ref_points) {
769
        strg += '<li>reference["'+lbl+'"] = '+ref_points[lbl].x.toFixed(5)+','+ref_points[lbl].y.toFixed(5)+'</li>';
770
    }
771
    if (strg) strg = '<h3>Liste de tous les points de référence</h3>\n<ul>' + strg + '</ul>';
772
    if (clear_before) res.innerHTML = strg;
773
    else res.innerHTML += strg;
774
}
775

    
776
function delete_ref_point(el) {
777
    el.style.display = 'none';
778
    delete ref_points[document.getElementById('sel_point').value];
779
    reset_zooms();
780
    putImage(last.x, last.y);
781
    show_result(true);
782
}
783

    
784
function clean_canvas_events(e) {
785
    canvas.removeEventListener('mousemove', stickImage, false);
786
    document.getElementById('info').style.display = 'none';
787
    speed.x = 0;
788
    speed.y = 0;
789
}
790

    
791
canvas_set_size = function() {
792
    canvas.style.border = border_width+"px solid red";
793
    canvas.width = window.innerWidth-2*border_width;
794
    canvas.height = window.innerHeight-2*border_width;
795
    canvas_pos.x = canvas.offsetLeft+border_width;
796
    canvas_pos.y = canvas.offsetTop+border_width;
797
}
798

    
799
canvas_resize = function() {
800
    canvas_set_size();
801
    putImage(last.x, last.y);
802
}
803

    
804
function paramIn(e) {
805
    e = e || window.event;
806
    var relatedTarget = e.relatedTarget || e.fromElement;
807

    
808
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
809
        relatedTarget = relatedTarget.parentNode;
810
    }
811

    
812
    if (relatedTarget != adding && relatedTarget != localisation) {
813
        document.removeEventListener('keydown', keys, false);
814
    }
815
}
816

    
817
function paramOut(e) {
818

    
819
    e = e || window.event;
820
    var relatedTarget = e.relatedTarget || e.toElement;
821

    
822
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
823
        relatedTarget = relatedTarget.parentNode;
824
    }
825

    
826
    if (relatedTarget != adding && relatedTarget != localisation) {
827
            document.addEventListener('keydown', keys, false);
828
    }
829

    
830
}
831

    
832
window.onload = function() {
833
    localisation = document.getElementById("locadraw");
834
    adding = document.getElementById("adding");
835
    canvas = document.getElementById("mon-canvas");
836
    cntext = canvas.getContext("2d");
837
    canvas_set_size();
838
    canvas.addEventListener("click", check_links, false);
839
    //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
840
    canvas.oncontextmenu = manage_ref_points;
841
    canvas.addEventListener("mouseout" , clean_canvas_events, false);
842
    show_links();
843

    
844
    var max_zoom = zooms.length - 1;
845
    zoom_control = document.getElementById("zoom_ctrl");
846
    zoom_control.onchange = change_zoom;
847
    zoom_control.max = max_zoom;
848
    if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
849
    zm = zooms[to_zoom];
850
    zoom_control.value = to_zoom;
851
    zm.refresh();
852

    
853
    zoom = zm.value;
854
    tile = zm.tile;
855
    ntiles = zm.ntiles;
856

    
857
    angle_control = document.getElementById("angle_ctrl");
858
    angle_control.value = to_cap;
859
    angle_control.onchange = change_angle;
860
    angle_control.onclick = change_angle;
861
    elvtn_control = document.getElementById("elvtn_ctrl");
862
    elvtn_control.value = to_ele;
863
    elvtn_control.onchange = change_angle;
864
    elvtn_control.onclick = change_angle;
865

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