Loading...
Searching...
No Matches
src
js
image_value_tracker.h
1
3
const
char
* image_value_tracker = R
"JS(/* src/js/image_value_tracker.js */
4
function RadarDataEncoding(encoding){
5
6
this.scale = 1.0;
7
this.offset = 0.0;
8
this.nodata = null;
9
this.undetect = null;
10
11
// Could be hidden, for now
12
this.precision = 2; // digits
13
14
if (!encoding)
15
return;
16
17
if (typeof(encoding) === "string"){
18
//console.info(elem);
19
encoding = encoding.split(',');
20
switch (encoding.length){
21
case 4:
22
this.undetect = parseFloat(encoding[3]);
23
case 3:
24
this.nodata = parseFloat(encoding[2]);
25
case 2:
26
this.offset = parseFloat(encoding[1]);
27
case 1:
28
this.scale = parseFloat(encoding[0]);
29
break;
30
default:
31
console.warn("data has extra 'encoding' attributes: ", encoding);
32
//console.warn(elem);
33
}
34
console.info('encoding: ' + this);
35
}
36
37
}
38
39
RadarDataEncoding.prototype.toString = function(){ return `${this.scale},${this.offset},${this.nodata},${this.undetect}`}
40
41
RadarDataEncoding.prototype.decode = function(value){
42
43
if (value === this.nodata){
44
return 'nodata';
45
}
46
else if (value === this.undetect){
47
return 'undetect';
48
}
49
else {
50
return (this.scale*value + this.offset).toFixed(this.precision);
51
}
52
53
}
54
55
56
async function set_image_value_tracker(imgElem, encoding, coordMonitorElem, valueMonitorElem){
57
58
await imgElem.decode();
59
const bbox = imgElem.getBoundingClientRect();
60
console.log('bbox: ', bbox);
61
// const w = imgElem.naturalWidth | 1;
62
// const h = imgElem.naturalHeight | 1;
63
const w = bbox.width;
64
const h = bbox.height;
65
const canvas = new OffscreenCanvas(w, h);
66
console.info(canvas)
67
68
const ctx = canvas.getContext("2d", { willReadFrequently: true });
69
70
// Expose for debugging
71
window.rackster = { imgElem, w, h, canvas, ctx };
72
73
// Draw and read
74
ctx.clearRect(0, 0, w, h);
75
ctx.drawImage(imgElem, 0, 0);
76
77
78
// critical:
79
const imageData = ctx.getImageData(0, 0, w, h);
80
const data = imageData.data;
81
82
function grayAt(x, y) {
83
84
const e = encoding;
85
86
if (x < 0 || y < 0 || x >= w || y >= h) return "";
87
const i = (y * w + x) * 4;
88
const v = data[i] + (data[i+1]<<8);
89
return e.decode(v);
90
91
}
92
93
imgElem.addEventListener("mousemove", (ev) => {
94
var my_bbox = imgElem.getBoundingClientRect();
95
var x = ev.clientX - my_bbox.left;
96
var y = ev.clientY - my_bbox.top;
97
coordMonitorElem.textContent = '('+x+','+y+')'; //.toFixed(2);
98
valueMonitorElem.textContent = grayAt(x,y); //.toFixed(2);
99
})
100
101
window.rackdata = data
102
103
}
104
105
function image_value_tracker(){
106
107
// const elems = document.querySelectorAll("metadata[data-base64]");
108
const elems = document.querySelectorAll(".MOUSE_VALUE");
109
110
elems.forEach(elem => {
111
112
const dataElem = elem.querySelector(".MOUSE_VALUE_DATA");
113
window.metadata = dataElem;
114
var encoding = new RadarDataEncoding(dataElem.getAttribute("data-encoding"))
115
116
const coordMonitorElem = elem.querySelector(".COORD_MONITOR");
117
const valueMonitorElem = elem.querySelector(".VALUE_MONITOR");
118
119
set_image_value_tracker(dataElem, encoding, coordMonitorElem, valueMonitorElem);
120
// var type = dataElem.getAttribute("data-basetype");
121
122
});
123
124
}
125
)JS";
Generated by
1.9.8