Projet

Général

Profil

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

root / js / pano.js @ 862d44f8

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
    var rt = 6371;  // Rayon de la terre
176
    var alt1 = document.getElementById('pos_alt').childNodes[0].nodeValue;
177
    var lat1 = document.getElementById('pos_lat').childNodes[0].nodeValue*Math.PI/180;
178
    var lon1 = document.getElementById('pos_lon').childNodes[0].nodeValue*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[ord_pts.length].shift_y = this.ref_pixels[ord_pts.length-1].shift_y;
470
                this.ref_pixels[0].dshft_y = 0;
471
                this.ref_pixels[ord_pts.length].dshft_y = 0;
472
            }
473

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

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

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

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

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

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

    
604
    prev_zm = zm;
605

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

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

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

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

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

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

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

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

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

    
706
function manage_ref_points(e) {
707
    //event.preventDefault();
708
    //event.stopPropagation();
709
    var insrt = document.getElementById('insert');
710
    document.getElementById('do-cancel').onclick = hide_contextmenu;
711
    insrt.style.left = e.pageX+'px';
712
    insrt.style.top = e.pageY+'px';
713
    insrt.style.display = 'block';
714
    var sel_pt = document.getElementById('sel_point');
715
    var do_insert = document.getElementById('do-insert');
716
    var do_delete = document.getElementById('do-delete');
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
    for(var i = 0; i < zm.pt_list.length; i++) {
720
        if (zm.pt_list[i]['type'] == 'ref_point') {
721
            if (check_prox(zm.pt_list[i]['xc']-pos_x, zm.pt_list[i]['yc']-pos_y, 20)) {
722
                sel_pt.value = zm.pt_list[i]['label'];
723
            }
724
        }
725
    }
726
    do_delete.onclick = function() {delete_ref_point(insrt)};
727
    do_insert.onclick = function() {insert_ref_point(insrt, e.pageX-canvas_pos.x, e.pageY-canvas_pos.y)};
728
    return false;
729
}
730

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

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

    
762
function delete_ref_point(el) {
763
    el.style.display = 'none';
764
    delete ref_points[document.getElementById('sel_point').value];
765
    reset_zooms();
766
    putImage(last.x, last.y);
767
    show_result(true);
768
}
769

    
770
function clean_canvas_events(e) {
771
    canvas.removeEventListener('mousemove', stickImage, false);
772
    document.getElementById('info').style.display = 'none';
773
    speed.x = 0;
774
    speed.y = 0;
775
}
776

    
777
canvas_set_size = function() {
778
    canvas.style.border = border_width+"px solid red";
779
    canvas.width = window.innerWidth-2*border_width;
780
    canvas.height = window.innerHeight-2*border_width;
781
    canvas_pos.x = canvas.offsetLeft+border_width;
782
    canvas_pos.y = canvas.offsetTop+border_width;
783
}
784

    
785
canvas_resize = function() {
786
    canvas_set_size();
787
    putImage(last.x, last.y);
788
}
789

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

    
803
function paramOut(e) {
804
         
805
    e = e || window.event; 
806
    var relatedTarget = e.relatedTarget || e.toElement; 
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.addEventListener('keydown', keys, false);
814
    }
815
 
816
}
817

    
818
window.onload = function() {        
819
    localisation = document.getElementById("locadraw");
820
    adding = document.getElementById("adding");
821
    canvas = document.getElementById("mon-canvas");
822
    cntext = canvas.getContext("2d");
823
    canvas_set_size();
824
    canvas.addEventListener("click", check_links, false);
825
    //canvas.addEventListener("oncontextmenu", manage_ref_points, false);
826
    canvas.oncontextmenu = manage_ref_points;
827
    canvas.addEventListener("mouseout" , clean_canvas_events, false);
828
    show_links();
829

    
830
    var max_zoom = zooms.length - 1;
831
    zoom_control = document.getElementById("zoom_ctrl");
832
    zoom_control.onchange = change_zoom;
833
    zoom_control.max = max_zoom;
834
    if (to_zoom > max_zoom) to_zoom = Math.floor(max_zoom/2);
835
    zm = zooms[to_zoom];
836
    zoom_control.value = to_zoom;
837
    zm.refresh();
838

    
839
    zoom = zm.value;
840
    tile = zm.tile;
841
    ntiles = zm.ntiles;
842

    
843
    angle_control = document.getElementById("angle_ctrl");
844
    angle_control.value = to_cap;
845
    angle_control.onchange = change_angle;
846
    angle_control.onclick = change_angle;
847
    elvtn_control = document.getElementById("elvtn_ctrl");
848
    elvtn_control.value = to_ele;
849
    elvtn_control.onchange = change_angle;
850
    elvtn_control.onclick = change_angle;
851

    
852
    change_angle();
853
    loca_temp = document.getElementById("loca_show");
854
    if (loca_temp) {
855
        loca_temp.onclick = showLoca;
856
        loca_temp = document.getElementById("loca_hide");
857
        loca_temp.onclick = hideLoca;
858
        loca_temp = document.getElementById("loca_button");
859
        loca_temp.onclick = localate_point;
860
        loca_erase = document.getElementById("loca_erase");
861
        loca_erase.onclick = erase_point;
862
        localisation.addEventListener('mouseover',paramIn,false);
863
        localisation.addEventListener('mouseout',paramOut,false);
864
    }
865
    canvas.addEventListener('mousedown', onImageClick, false);
866
    document.addEventListener('keydown', keys, false);
867
    canvas.addEventListener('mousewheel', wheel_zoom, false);
868
    window.onresize = canvas_resize;
869
    if (adding) {
870
        document.getElementById("paramFormHide").onclick = hideForm;
871
        document.getElementById("paramFormShow").onclick = showForm;
872
        adding.addEventListener('mouseover', paramIn, false);
873
        adding.addEventListener('mouseout', paramOut, false);
874
    }
875
};