Loading...
Searching...
No Matches
src
js
set_image_value_tracker.h
1
3
const
char
* set_image_value_tracker = R
"JS(<![CDATA[
4
5
async function readGrayscaleFromImg(href) {
6
// Ensure the image is loaded
7
/*
8
if (!imgEl.complete || imgEl.naturalWidth === 0) {
9
await new Promise((resolve, reject) => {
10
imgEl.onload = resolve;
11
imgEl.onerror = reject;
12
});
13
}*/
14
15
const imgEl = new Image();
16
imgEl.crossOrigin = "anonymous"; // required if the PNG is cross-origin + CORS-enabled
17
imgEl.src = href;
18
19
// const bbox = imgEl.getBBox()
20
// const w = bbox.width | 1;
21
// const h = bbox.height | 1;
22
const w = imgEl.naturalWidth | 1;
23
const h = imgEl.naturalHeight | 1;
24
25
const canvas = new OffscreenCanvas(w, h); // document.getElementById("c");
26
//canvas.width = w;
27
//canvas.height = h;
28
29
const ctx = canvas.getContext("2d", { willReadFrequently: true });
30
31
window.racklet = { imgEl, w, h, canvas, ctx };
32
33
ctx.drawImage(imgEl, 0, 0);
34
35
// If the image is cross-origin and not CORS-enabled, this will throw (tainted canvas)
36
const { data } = ctx.getImageData(0, 0, w, h);
37
38
// Build a grayscale array (0..255). For true grayscale PNG, R==G==B.
39
const gray = new Uint8Array(w * h);
40
for (let i = 0, p = 0; i < data.length; i += 4, p++) {
41
gray[p] = data[i]; // R channel
42
}
43
44
return { width: w, height: h, gray };
45
}
46
47
async function marko(href){
48
// const img = document.getElementById("src");
49
const { width, height, gray } = await readGrayscaleFromImg(href);
50
51
// Example: print a few pixels
52
console.log("size:", width, height);
53
console.log("top-left pixel:", gray[0]);
54
console.log("pixel (10, 5):", gray[5 * width + 10]);
55
};
56
57
58
function set_image_value_tracker(img, func){
59
60
const href = img.getAttribute("href") ||
61
img.getAttributeNS("http://www.w3.org/1999/xlink", "href");
62
63
console.info("href="+href)
64
65
marko(href);
66
//await img.decode();
67
return false
68
69
70
// var img = document.getElementById('rgb');
71
const w = img.naturalWidth | 0;
72
const h = img.naturalHeight | 0;
73
const canvas = new OffscreenCanvas(w, h);
74
console.info(canvas)
75
76
77
78
const ctx = canvas.getContext("2d", { willReadFrequently: true });
79
80
// Expose for debugging
81
window.rackster = { img, w, h, canvas, ctx };
82
83
84
// Draw and read
85
ctx.clearRect(0, 0, w, h);
86
ctx.drawImage(img, 0, 0);
87
const imageData = ctx.getImageData(0, 0, w, h);
88
const data = imageData.data; // Uint8ClampedArray, RGBA
89
90
// Grayscale value at (x,y): for grayscale PNG, R==G==B
91
function grayAt(x, y) {
92
if (x < 0 || y < 0 || x >= w || y >= h) return null;
93
const i = (y * w + x) * 4;
94
return data[i]; // R
95
}
96
console.info('adding image value listener to ', img)
97
98
const bbox = img.getBoundingClientRect();
99
img.addEventListener("mousemove", (ev) => {
100
const x = ev.clientX - bbox.left;
101
const y = ev.clientY - bbox.top;
102
console.info(grayAt(x, y))
103
// const i = x / bbox.width;
104
// const j = y / bbox.height;
105
//func(i,j)
106
})
107
};
108
109
110
function add_image_value_trackers(){
111
112
const elems = document.querySelectorAll(".MOUSE_VALUE");
113
114
elems.forEach(elem => {
115
// set_image_value_tracker(elem)
116
var metadata = elem.querySelector("metadata");
117
console.info(metadata)
118
window.metadata = metadata
119
var b64 = metadata.getAttribute("data-base64")
120
console.info(b64)
121
var arr = base64ToArrayLE(b64, Float32Array)
122
//base64ToFloat32ArrayLE(b64)
123
console.info(arr)
124
})
125
}
126
//]]>)JS";
Generated by
1.9.8