~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to lib/SunViewer/SunCoordsDisplay.js

  • Committer: Michael Lynch
  • Date: 2008-02-01 06:21:07 UTC
  • Revision ID: mike@mike-desktop-20080201062107-uhqip0rcpsfc91mq
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @author Patrick Schmiedel patrick.schmiedel@gmx.net
 
3
 */
 
4
 
 
5
/**
 
6
 * @classDescription A user interface element that shows the latitude/longitude value of the location under the mouse cursor.
 
7
 */
 
8
var SunCoordsDisplay = Class.create();
 
9
 
 
10
SunCoordsDisplay.prototype = Object.extend(new SunViewerWidget(), {
 
11
        defaultOptions: $H({
 
12
                decimals: 0
 
13
        }),
 
14
        
 
15
        /**
 
16
         * @constructor
 
17
         * @param {Sring} elementId                                     The ID of the SunCoordsDisplay HTML element.
 
18
         * @param {TileLayerProvider} layerProvider     The corresponding TileLayerProvider.
 
19
         * @param {Hash} options                                        Available options: decimals
 
20
         */
 
21
        initialize: function(elementId, layerProvider, options) {
 
22
                this.domNode = $(elementId);
 
23
                this.layerProvider = layerProvider;
 
24
                
 
25
                Object.extend(this, this.defaultOptions);
 
26
                Object.extend(this, options);
 
27
                
 
28
                this.createElements();
 
29
                
 
30
                Event.observe(this.layerProvider.tileContainer.viewport.domNode, 'mousemove', this.updateCoords.bindAsEventListener(this));
 
31
        },
 
32
        
 
33
        /**
 
34
         * @method createElements       Creates the user interface HTML elements.
 
35
         */
 
36
        createElements: function() {
 
37
                var table = document.createElement('table');
 
38
                table.className = 'sunCoordsDisplay';
 
39
 
 
40
                var tr = document.createElement('tr')
 
41
                this.latText = document.createElement('td');
 
42
                this.latText.width = 15;
 
43
                this.latValue = document.createElement('td');
 
44
                this.latValue.width = 25;
 
45
                this.lonText = document.createElement('td');
 
46
                this.lonText.width = 15;
 
47
                this.lonValue = document.createElement('td');
 
48
                this.lonValue.width = 25;
 
49
                tr.appendChild(this.latText);
 
50
                tr.appendChild(this.latValue);
 
51
                tr.appendChild(this.lonText);
 
52
                tr.appendChild(this.lonValue);
 
53
                table.appendChild(tr);
 
54
                this.domNode.appendChild(table);
 
55
        },
 
56
        
 
57
        /**
 
58
         * TODO: Move to LatLon.fromXY()
 
59
         * @method XYToRad              Converts (x,y) pixel coordinates in the viewport to latitude/longitude sun coordinates.
 
60
         * @param {Number} x    The x coordinate in the viewport in pixels.
 
61
         * @param {Number} y    The y coordinate in the viewport in pixels.
 
62
         * @return {Hash}               The latitude/longitude coordinates or null if out of bounds.
 
63
         */
 
64
        XYToRad: function (x, y) {
 
65
                if (!this.layerProvider.sunImage) return null;
 
66
                var sunRadius = this.layerProvider.sunImage.sunRadius * this.layerProvider.getFullSize();
 
67
                var viewerPosition = this.layerProvider.tileContainer.viewport.position;
 
68
                var y1 = (y - viewerPosition.y) / sunRadius;
 
69
                if (-1 <= y1 && y1 <= 1) {
 
70
                        var latitudeRad = Math.asin(y1);
 
71
 
 
72
                        var x1 = (x - viewerPosition.x) / sunRadius;
 
73
                        var x2 = x1 / Math.cos(latitudeRad);
 
74
                        if (-1 <= x2 && x2 <= 1) {
 
75
                                var longitudeRad = Math.asin(x2);
 
76
 
 
77
                                return { latitude: latitudeRad, longitude: longitudeRad };
 
78
                        }
 
79
                }
 
80
                return null;
 
81
        },
 
82
        
 
83
        /**
 
84
         * @method displayCoords                Shows the latitude/longitude values in the user interface element.
 
85
         * @param {Number} latitude             The latitude value.
 
86
         * @param {Number} longitude    The longitude value.
 
87
         */
 
88
        displayCoords: function(latitude, longitude) {
 
89
                var showCoords = (latitude != null && longitude != null);
 
90
                this.latValue.innerHTML = showCoords ? Math.abs(latitude.toFixed(this.decimals)) : '';
 
91
                this.latText.innerHTML = showCoords ? (latitude < 0 ? 'N' : 'S') : '';
 
92
                this.lonValue.innerHTML = showCoords ? Math.abs(longitude.toFixed(this.decimals)) : '';
 
93
                this.lonText.innerHTML = showCoords ? (longitude < 0 ? 'E' : 'W') : '';
 
94
        },
 
95
        
 
96
        /**
 
97
         * @method updateCoords                 Calculates the sun coordinates and shows them in the user interface.
 
98
         * @param {DOM MouseEvent} e    The DOM MouseEvent.
 
99
         */
 
100
        updateCoords: function(e) {
 
101
                if (this.layerProvider.tileContainer.viewport.surface.moving) return;
 
102
                var viewportOffset = this.layerProvider.tileContainer.viewport.offset;
 
103
                var screenCoords = {
 
104
                        x: Event.pointerX(e) - viewportOffset[0],
 
105
                        y: Event.pointerY(e) - viewportOffset[1]
 
106
                };
 
107
                var screenSunCoords = this.XYToRad(screenCoords.x, screenCoords.y);
 
108
                if (screenSunCoords == null) {
 
109
                        this.displayCoords();
 
110
                } else {
 
111
                        var realSunCoordsDeg = {
 
112
                                latitude: screenSunCoords.latitude + this.layerProvider.sunImage.date.getObliquity(),
 
113
                                longitude: screenSunCoords.longitude
 
114
                        };
 
115
                        this.displayCoords(realSunCoordsDeg.latitude.toDeg(), realSunCoordsDeg.longitude.toDeg());
 
116
                }
 
117
        }
 
118
});
 
 
b'\\ No newline at end of file'