Projet

Général

Profil

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

root / js / pano.js @ 7f9aab46

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

    
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
}
122

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

    
130
function draw_tile(idx, ox, oy) {
131
    var img = imageObj[idx];
132
    cntext.drawImage(img, ox, oy);
133
}
134

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

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

    
174
function insert_drawn_point(lat,lon,alt) {
175
        
176
        var rt = 6371;  // Rayon de la terre
177
    var alt1 = document.getElementById('pos_alt').childNodes[0].nodeValue;
178
    var lat1 = document.getElementById('pos_lat').childNodes[0].nodeValue*Math.PI/180;
179
    var lon1 = document.getElementById('pos_lon').childNodes[0].nodeValue*Math.PI/180;
180
    var alt2 = alt;
181
    var lat2 = lat*Math.PI/180;
182
    var lon2 = lon*Math.PI/180;
183
    
184
    var dLat = lat2-lat1;
185
    var dLon = lon2-lon1; 
186
   
187
    var a = Math.sin(dLat/2)*Math.sin(dLat/2) + Math.sin(dLon/2)*Math.sin(dLon/2)*Math.cos(lat1)*Math.cos(lat2);  // 
188
    var angle = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
189
    var d = angle*rt;                    // distance du point en Kms
190
   
191
    var y = Math.sin(dLon) * Math.cos(lat2);
192
    var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
193
    var cap = Math.atan2(y,x);                 // cap pour atteindre le point en radians
194
    var e = Math.atan2((alt2 - alt1)/1000 - d*d/(2*rt),d);  // angle de l'élévation en radians
195
    
196
    return {d:d, cap:cap*180/Math.PI, ele:e*180/Math.PI};   // les résultats sont en degrés
197
}
198

    
199
localate_point = function () {
200
        
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) {
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
            // -----Première solution : afficher dynamiquement le point !
219
            var d = opt_dce.d;
220
            var cap = opt_dce.cap;
221
            var ele = opt_dce.ele;
222
            
223
            display_temp(d, cap, ele);
224
           
225
}
226

    
227
function display_temp(d,cap,ele) {
228
   
229
    point_list[point_list.length] = new Array("point temporaire", d,cap,ele, "temporary");
230
    reset_zooms();
231
    putImage(last.x, last.y);
232
}
233

    
234
function arrayUnset(array, value){
235
    array.splice(array.indexOf(value), 1);
236
}
237

    
238
erase_point = function() {
239
        
240
        for (var i=0; i<point_list.length; i++) {
241
                if(point_list[i][0] == "point temporaire"){
242
                        arrayUnset(point_list,point_list[i]);
243
                        loop = erase_point();
244
                }        
245
        }        
246
        reset_zooms();
247
    putImage(last.x, last.y);   
248
}
249

    
250
function get_file_name(x, y, z) { // recherche du fichier correspondant au zoom et à la position
251
    var prm = [z, x, y];
252
    var fname = img_prefix;
253
    for (var i = 0; i < prm.length; i++) {
254
        fname += '_';
255
        if (prm[i] < 10) fname += '00';
256
        else if (prm[i] < 100) fname += '0';
257
        fname += prm[i];
258
    }
259
    fname += '.jpg';
260
    return fname;
261
}
262

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

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

    
337

    
338
function stickImage(e) {
339
    var xs = mouse.x - e.pageX + shift.x;
340
    var ys = mouse.y - e.pageY + shift.y;
341
    speed.x = xs - last.x;  //mémorisation des vitesses horizontales
342
    speed.y = ys - last.y;  //et verticales lors de ce déplacement
343
    putImage(xs, ys);
344
}
345

    
346
function launchImage(e) {
347
    distort_canvas(0);
348
    canvas.removeEventListener('mousemove', stickImage, false);
349
    //document.onmousemove = null;
350
    shift.x = e.pageX - mouse.x + shift.x;
351
    shift.y = e.pageY - mouse.y + shift.y;
352
    tmt = setTimeout(inertialImage, 100);
353
}
354

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

    
374
    last.x = x;
375
    last.y = y;
376
    draw_image(x, y);
377
}
378

    
379
function inertialImage() {
380
    speed.x *= 0.9;
381
    speed.y *= 0.9;
382
    if (Math.abs(speed.x) > 2 || Math.abs(speed.y) > 2) {
383
        putImage(last.x+speed.x, last.y+speed.y);
384
        tmt = setTimeout(inertialImage, 100);
385
    } else {
386
        show_links();
387
    }
388
}
389

    
390
function tri_ref_points(v1, v2) {
391
    return v1['x'] - v2['x'];
392
}
393

    
394

    
395

    
396
function tzoom(zv) {
397
    this.value = zv;
398
    this.ntiles = {x:0,y:0};
399
    this.tile = {width:0,height:0};
400
    this.last_tile = {width:0,height:0};
401
    this.max_tile = {width:0,height:0};
402
    this.im = {width:0,height:0};
403
    this.is_updated = false;
404

    
405
    this.refresh = function() {
406
        this.im.visible_width = this.tile.width*(this.ntiles.x-1)+this.last_tile.width;
407
        this.is_updated = true;
408

    
409
        this.im.width = this.im.visible_width;
410
        this.im.height = this.tile.height*(this.ntiles.y-1)+this.last_tile.height;
411
        if (this.last_tile.width > this.tile.width) this.max_tile.width = this.im.last_tile.width;
412
        else this.max_tile.width = this.tile.width;
413
        if (this.last_tile.height > this.tile.height) this.max_tile.height = this.im.last_tile.height;
414
        else this.max_tile.height = this.tile.height;
415

    
416
        var ord_pts = new Array();
417
        i=0;
418
        for(var label in ref_points) {
419
            ord_pts[i++] = ref_points[label]
420
        }
421
        ord_pts = ord_pts.sort(tri_ref_points);
422
        is_located = i > 1 || image_loop && i > 0;
423

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

    
458
            for (var i=0; i < ord_pts.length; i++) { // second parcours pour les paramètres elevation/y
459
                this.ref_pixels[i+1].shift_y = Math.floor(this.pixel_y_ratio*ord_pts[i].ele - ord_pts[i].y*this.im.height);
460
                if (i != ord_pts.length-1) {
461
                    var next_shift = Math.floor(this.pixel_y_ratio*ord_pts[i+1].ele - ord_pts[i+1].y*this.im.height);
462
                    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);
463
                }
464
            }
465

    
466
            if (image_loop == true) {
467
                var dpix  = this.im.width;
468
                var delt = 0;
469
                if (ord_pts.length > 1) {
470
                    dpix  = this.im.width - this.ref_pixels[this.ref_pixels.length-1].x + this.ref_pixels[1].x;
471
                    delt = this.ref_pixels[this.ref_pixels.length-1].shift_y - this.ref_pixels[1].shift_y;
472
                }
473
                this.ref_pixels[0].dshft_y = delt/dpix;
474
                this.ref_pixels[ord_pts.length].dshft_y = this.ref_pixels[0].dshft_y;
475
                dpix = this.im.width - this.ref_pixels[ord_pts.length].x;
476
                this.ref_pixels[0].shift_y = this.ref_pixels[ord_pts.length].shift_y - dpix*this.ref_pixels[0].dshft_y;
477
            } else {
478
                this.ref_pixels[0].shift_y = this.ref_pixels[1].shift_y;
479
                this.ref_pixels[ord_pts.length].shift_y = this.ref_pixels[ord_pts.length-1].shift_y;
480
                this.ref_pixels[0].dshft_y = 0;
481
                this.ref_pixels[ord_pts.length].dshft_y = 0;
482
            }
483

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

    
509
        this.pt_list = new Array();
510
        for (var i=0; i<point_list.length; i++) {
511
            var lbl = point_list[i][0];
512
            var dst = point_list[i][1];
513
            var cap = point_list[i][2];
514
            var ele = point_list[i][3];
515
            var lnk = point_list[i][4];
516
            var typ = 'unlocated';
517
            var rxy = this.get_pos_xy(cap, ele);
518
            var is_visible = fmodulo(cap - alpha_domain.start, 360) <= fmodulo(alpha_domain.end - alpha_domain.start -0.0001, 360)+0.0001 && is_located;
519

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

    
589
function reset_zooms () {
590
    for(i=0; i<zooms.length; i++) zooms[i].is_updated = false;
591
    zm.refresh();
592
}
593

    
594
function wheel_zoom (event) {
595
    var zshift = {x:0, y:0};
596
    if (event.pageX != undefined && event.pageX != undefined) {
597
        zshift.x = event.pageX-canvas.width/2-canvas_pos.x;
598
        zshift.y = event.pageY-canvas.height/2-canvas_pos.y;
599
    }
600
    event.preventDefault();
601
    if (event.wheelDelta < 0 && zoom_control.value < zoom_control.max) {
602
        zoom_control.value++;
603
        change_zoom(zshift.x, zshift.y);
604
    } else if (event.wheelDelta > 0 && zoom_control.value > zoom_control.min) {
605
        zoom_control.value--; 
606
        change_zoom(zshift.x, zshift.y);
607
    }
608
}
609

    
610
function change_zoom(shx, shy) {
611
    var zoom_control = document.getElementById("zoom_ctrl");
612
    var v = zoom_control.value;
613

    
614
    prev_zm = zm;
615

    
616
    if (zooms[v]) {
617
        if (!zooms[v].is_updated) zooms[v].refresh();
618
    } else {
619
        zooms[v] = new tzoom(v);
620
    }
621

    
622
    if (zooms[v].is_updated) {
623
        if (shx == undefined || shy == undefined) {
624
            shx=0;
625
            shy=0;
626
        }
627
        zm = zooms[v];
628
        var px = (last.x+shx)*zm.im.width/prev_zm.im.width - shx;
629
        var py = (last.y+shy)*zm.im.height/prev_zm.im.height - shy;
630
        if (py < zm.im.height && py >= 0) {
631
            zoom = zm.value;
632
            tile = zm.tile;
633
            ntiles = zm.ntiles;
634
            putImage(px, py);
635
        } else {
636
            zm = prev_zm;
637
            zoom_control.value = zm.value;
638
        }
639
    }
640
}
641

    
642
function change_angle() {
643
    var elvtn_control = document.getElementById('elvtn_ctrl');
644
    var angle_control = document.getElementById('angle_ctrl');
645
    var resxy = zm.get_pos_xy(angle_control.value, elvtn_control.value);
646
    var pos_x = resxy.x;
647
    var pos_y = Math.floor(zm.im.height/2 - resxy.y);
648
    putImage(pos_x, pos_y);
649
}
650

    
651
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. 
652
    return Math.sqrt(x*x + y*y) < r;
653
}
654

    
655
function check_links(e) {
656
    var mouse_x = e.pageX-canvas_pos.x;
657
    var mouse_y = e.pageY-canvas_pos.y;
658
    var pos_x = nmodulo(last.x + mouse_x - canvas.width/2, zm.im.width);
659
    var pos_y = last.y + mouse_y - canvas.height/2;
660
    for(var i = 0; i < zm.pt_list.length; i++) {
661
        if (is_located && zm.pt_list[i]['type'] == 'pano_point') {
662
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
663
                if (zm.pt_list[i]['lnk'] != '') window.location = zm.pt_list[i]['lnk'];
664
                break;
665
            }
666
        }
667
    }
668
}
669

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

    
700
function hide_links() {
701
    canvas.removeEventListener( "mousemove", display_links, false);
702
    var info = document.getElementById('info');
703
    info.style.display = 'none';
704
}
705

    
706
function show_links() {
707
    canvas.addEventListener( "mousemove", display_links, false);
708
//    var info = document.getElementById('info');
709
//    info.style.display = 'block';
710
}
711

    
712
function hide_contextmenu() {
713
    document.getElementById('insert').style.display = 'none';
714
}
715

    
716
function manage_ref_points(e) {
717
    //event.preventDefault();
718
    //event.stopPropagation();
719
    var insrt = document.getElementById('insert');
720
    document.getElementById('do-cancel').onclick = hide_contextmenu;
721
    insrt.style.left = e.pageX+'px';
722
    insrt.style.top = e.pageY+'px';
723
    insrt.style.display = 'block';
724
    var sel_pt = document.getElementById('sel_point');
725
    var do_insert = document.getElementById('do-insert');
726
    var do_delete = document.getElementById('do-delete');
727
    var pos_x = nmodulo(last.x + e.pageX - canvas_pos.x - canvas.width/2, zm.im.width);
728
    var pos_y = last.y + e.pageY - canvas_pos.y - canvas.height/2;
729
    for(var i = 0; i < zm.pt_list.length; i++) {
730
        if (zm.pt_list[i]['type'] == 'ref_point') {
731
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
732
                sel_pt.value = zm.pt_list[i]['label'];
733
            }
734
        }
735
    }
736
    do_delete.onclick = function() {delete_ref_point(insrt)};
737
    do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
738
    return false;
739
}
740

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

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

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

    
780
function clean_canvas_events(e) {
781
    canvas.removeEventListener('mousemove', stickImage, false);
782
    document.getElementById('info').style.display = 'none';
783
    speed.x = 0;
784
    speed.y = 0;
785
}
786

    
787

    
788

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

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

    
802

    
803

    
804
function paramIn(e) {
805
        
806
         e = e || window.event; 
807
         var relatedTarget = e.relatedTarget || e.fromElement; 
808
         
809
         while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
810
                relatedTarget = relatedTarget.parentNode;
811
         }
812
         
813
         if (relatedTarget != adding && relatedTarget != localisation) {
814
                 document.removeEventListener('keydown', keys, false);
815
         }
816
}
817

    
818
function paramOut(e) {
819
         
820
    e = e || window.event; 
821
    var relatedTarget = e.relatedTarget || e.toElement; 
822
 
823
    while (relatedTarget != adding && relatedTarget.nodeName != 'BODY' && relatedTarget != document && relatedTarget != localisation) {
824
        relatedTarget = relatedTarget.parentNode;
825
    }
826
 
827
    if (relatedTarget != adding && relatedTarget != localisation) {
828
            document.addEventListener('keydown', keys, false);
829
    }
830
 
831
}
832

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

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

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

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

    
868
    change_angle();
869
    loca_temp = document.getElementById("loca_button");
870
    loca_temp.onclick = localate_point;
871
    loca_erase = document.getElementById("loca_erase");
872
    loca_erase.onclick = erase_point;
873
    canvas.addEventListener('mousedown', onImageClick, false);
874
    document.addEventListener('keydown', keys, false);
875
    canvas.addEventListener('mousewheel', wheel_zoom, false);
876
    window.onresize = canvas_resize;
877
    adding.addEventListener('mouseover',paramIn,false);
878
    adding.addEventListener('mouseout',paramOut,false);
879
    localisation.addEventListener('mouseover',paramIn,false);
880
    localisation.addEventListener('mouseout',paramOut,false);
881
      
882
};