Loading...
Searching...
No Matches
coord_handler.h
1
3const char* coord_handler = R"JS(/* src/js/coord_handler.js */
5
6function BBox(bbox){
7 if (typeof(bbox) === 'string'){
8 bbox = bbox.split(',')
9 }
10 this.left = parseFloat(bbox[0]);
11 this.top = parseFloat(bbox[3]);
12 this.width = parseFloat(bbox[2]) - this.left;
13 this.height = parseFloat(bbox[1]) - this.top;
14}
15
16function Coord2D(){
17 this.x = 0;
18 this.y = 0;
19}
20
21/*
22Coord2D.prototype.readEvent = function(ev){
23 this.cbox = ev.target.getBoundingClientRect();
24 this.x = ev.clientX - this.cbox.left;
25 this.y = ev.clientY - this.cbox.top;
26 }
27*/
28
29
30function CoordHandler(bboxGeo, bboxFrame){
31 this.bboxFrame = bboxFrame; // coordTracker.getBoundingClientRect();
32 this.bboxGeo = new BBox(bboxGeo);
33 this.drag = false;
34 this.curr = new Coord2D()
35 this.start = new Coord2D()
36}
37
38CoordHandler.prototype.readEvent = function(ev, coords){
39 this.bboxFrame = ev.target.getBoundingClientRect();
40 coords.x = ev.clientX - this.bboxFrame.left;
41 coords.y = ev.clientY - this.bboxFrame.top;
42 if (this.precisionFocus){
43 coords.x = this.precisionFocus*Math.round(coords.x/this.precisionFocus);
44 coords.y = this.precisionFocus*Math.round(coords.y/this.precisionFocus);
45 }
46}
47
48CoordHandler.prototype.update = function(coord, elem){};
49CoordHandler.prototype.updateSpanElem = function(elem, x,y,width,height){};
50
51CoordHandler.prototype.updateSpan = function(elem){
52
53 var width = Math.abs(this.start.x - this.curr.x);
54 var height = Math.abs(this.start.y - this.curr.y);
55
56 if (this.precisionWidth){
57 width = this.precisionWidth*Math.round(width / this.precisionWidth);
58 height = this.precisionWidth*Math.round(height / this.precisionWidth);
59 }
60
61 this.updateSpanElem(elem,
62 Math.min(this.start.x, this.curr.x),
63 Math.min(this.start.y, this.curr.y),
64 width,
65 height);
66
67 //elem.textContent = this.getPosString(coord.x, coord.y);
68};
69
70CoordHandler.prototype.setPrecision = function(precision){
71 if (typeof(precision) === 'number'){
72 this.precisionFocus = precision;
73 this.precisionWidth = precision;
74 }
75 else if (typeof(precision) === 'string'){
76 precision = precision.split(RegExp('[,:]'))
77 }
78
79 console.log("precision: ", precision, "px")
80
81 switch (precision.length) {
82 case 2:
83 this.precisionFocus = parseInt(precision[0]);
84 this.precisionWidth = parseInt(precision[1]);
85 break;
86 case 1:
87 this.precisionFocus = parseInt(precision[0]);
88 this.precisionWidth = this.precisionFocus
89 break;
90 default:
91 this.precisionFocus = parseInt(precision[1]);
92 this.precisionWidth = precision;
93 }
94}
95
96CoordHandler.prototype.getRelativeX = function(x){
97 //return (x - this.bboxFrame.left)/this.bboxFrame.width;
98 return x/this.bboxFrame.width;
99}
100
101CoordHandler.prototype.getRelativeY = function(y){
102 // return (y - this.bboxFrame.top)/this.bboxFrame.height;
103 return y/this.bboxFrame.height;
104}
105
106CoordHandler.prototype.getLongitude = function(xRel){
107 return this.bboxGeo.left + xRel*this.bboxGeo.width;
108}
109
110CoordHandler.prototype.getLatitude = function(yRel){
111 return this.bboxGeo.top + (1.0-yRel)*this.bboxGeo.height;
112}
113
115CoordHandler.prototype.getGeoPos = function(x,y){
116 return [ Math.round(this.getLongitude(this.getRelativeX(x))),
117 Math.round(this.getLatitude(this.getRelativeY(y))) ]
118}
119
120CoordHandler.prototype.getGeoBBOX = function(){
121 var x0 = Math.min(this.start.x, this.curr.x)
122 var x = Math.max(this.start.x, this.curr.x)
123 var y0 = Math.min(this.start.y, this.curr.y)
124 var y = Math.max(this.start.y, this.curr.y)
125
126 return [].concat(this.getGeoPos(x0, y0 ),
127 this.getGeoPos(x, y ))
128}
129
130CoordHandler.prototype.getPosString = function(x,y){
131 var a = [x, y];
132 var xRel = this.getRelativeX(x);
133 var yRel = this.getRelativeY(y);
134 x = Math.round(this.getLongitude(xRel));
135 y = Math.round(this.getLatitude(yRel));
136 a.push(x);
137 a.push(y);
138 // a.push(this.drag);
139 return a.join(',')
140}
141
142
143
144
146
156)JS";