Projet

Général

Profil

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

root / js / pano.js @ d9ee446a

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 = {
33
        'pano_point' : '255,128,128', // red
34
        'ref_point'  : '128,128,255', // blue
35
        'loc_point'  : '128,255,128', // green
36
        'temporary'  : '255,255,128', // yellow
37
        'unlocated'  : '255,255,255'  // white
38
};
39
var test = {x:0, y:0, i:100};
40

    
41

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

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

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

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

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

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

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

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

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

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

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

    
166
    if (twidth) {
167
        cntext.restore();
168
    }
169

    
170
}
171

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

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

    
185
    var dLat = lat2-lat1;
186
    var dLon = lon2-lon1;
187

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

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

    
197
    return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI};   // les résultats sont en degrés
198
}
199

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

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

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

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

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

    
255
function keys(key) {
256

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

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

    
329

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

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

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

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

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

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

    
386

    
387

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
605
    prev_zm = zm;
606

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
819
function paramOut(e) {
820

    
821
    e = e || window.event;
822
    var relatedTarget = e.relatedTarget || e.toElement;
823

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

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

    
832
}
833

    
834

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

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

    
856
    zoom = zm.value;
857
    tile = zm.tile;
858
    ntiles = zm.ntiles;
859

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

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