AwesomeUserWOW commited on
Commit
fd941c3
·
verified ·
1 Parent(s): 268362e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +467 -17
index.html CHANGED
@@ -1,19 +1,469 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8"/>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1"/>
6
+ <title>Auto-Scaling Video Reconstructor — Full</title>
7
+ <style>
8
+ :root{--bg:#080808;--fg:#e8e8e8;--muted:#9aa0a6}
9
+ html,body{height:100%;margin:0;background:var(--bg);color:var(--fg);font-family:system-ui,Segoe UI,Roboto,Arial}
10
+ .ui{display:flex;flex-wrap:wrap;gap:8px;padding:10px;align-items:center}
11
+ label{font-size:13px;color:var(--muted)}
12
+ input,select,button{padding:6px 10px;font-size:13px}
13
+ canvas{display:block;margin:8px auto;border:1px solid #222;image-rendering:pixelated;max-width:calc(100% - 20px)}
14
+ .right{margin-left:auto;color:var(--muted);font-size:13px}
15
+ .small{padding:5px 8px;font-size:12px}
16
+ </style>
17
+ </head>
18
+ <body>
19
+ <div class="ui">
20
+ <label>Target <input id="upload" type="file" accept="image/*"></label>
21
+ <label>Feed <select id="feedMode"><option value="display">Screen/Tab</option><option value="camera">Camera</option></select></label>
22
+ <button id="start">Start Feed</button>
23
+
24
+ <label>Auto-scale <input id="autoScale" type="checkbox" checked></label>
25
+ <label>Max W <input id="maxW" type="number" value="900" style="width:80px"></label>
26
+ <label>Max H <input id="maxH" type="number" value="600" style="width:80px"></label>
27
+
28
+ <label>Block
29
+ <select id="blockSize"><option>32</option><option>16</option><option selected>8</option><option>4</option><option>2</option></select>
30
+ </label>
31
+
32
+ <label>Speed <input id="speed" type="range" min="10" max="2000" value="300"></label>
33
+ <label>Candidates <input id="candidates" type="range" min="1" max="12" value="5"></label>
34
+
35
+ <label>Repass every <input id="repassFrames" type="number" value="60" style="width:72px"> frames</label>
36
+
37
+ <label><input id="heat" type="checkbox"> Heatmap</label>
38
+ <button id="debugBtn" class="small">Open Debug</button>
39
+ <button id="export" class="small">Export PNG</button>
40
+ <button id="reset" class="small">Reset</button>
41
+
42
+ <div class="right">
43
+ Avg: <span id="avgscore">0</span>% &nbsp;|&nbsp; Blocks: <span id="nblocks">0</span>
44
+ </div>
45
+ </div>
46
+
47
+ <canvas id="canvas"></canvas>
48
+
49
+ <script>
50
+ /* --- Reconstructor Full — Single File --- */
51
+ /* Features: auto-scale, tournament, repass, buckets, early bail, adaptive blocks, debug window */
52
+
53
+ const upload = document.getElementById('upload');
54
+ const startBtn = document.getElementById('start');
55
+ const feedMode = document.getElementById('feedMode');
56
+ const autoScaleBox = document.getElementById('autoScale');
57
+ const maxWInput = document.getElementById('maxW');
58
+ const maxHInput = document.getElementById('maxH');
59
+ const blockSelect = document.getElementById('blockSize');
60
+ const speedSlider = document.getElementById('speed');
61
+ const candSlider = document.getElementById('candidates');
62
+ const heatToggle = document.getElementById('heat');
63
+ const debugBtn = document.getElementById('debugBtn');
64
+ const exportBtn = document.getElementById('export');
65
+ const resetBtn = document.getElementById('reset');
66
+ const repassFramesInput = document.getElementById('repassFrames');
67
+ const avgScoreEl = document.getElementById('avgscore');
68
+ const nblocksEl = document.getElementById('nblocks');
69
+
70
+ const canvas = document.getElementById('canvas');
71
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
72
+
73
+ // offscreen temp canvas for video frames and resizing
74
+ const tmp = document.createElement('canvas');
75
+ const tctx = tmp.getContext('2d', { willReadFrequently: true });
76
+
77
+ // state
78
+ let targetImageData = null;
79
+ let outputData = null;
80
+ let blocks = []; // array of {x,y,index}
81
+ let BLOCK = Number(blockSelect.value);
82
+ let scoreMap = null; // Float32Array per-block
83
+ let bestPos = null; // Int32Array per-block [sx,sy]
84
+ let video = null;
85
+ let feedStream = null;
86
+ let running = false;
87
+ let frameCounter = 0;
88
+ let pixelMode = false;
89
+ let tmpFrame = null;
90
+ let debugWin = null;
91
+
92
+ // perceptual-ish color weighting & luminance
93
+ const luminance = (r,g,b) => 0.2126*r + 0.7152*g + 0.0722*b;
94
+ const colorDistanceWeighted = (r1,g1,b1, r2,g2,b2) => {
95
+ const dr = r1-r2, dg = g1-g2, db = b1-b2;
96
+ return 2*dr*dr + 4*dg*dg + 3*db*db; // tuned weights
97
+ };
98
+
99
+ // AUTO-SCALE when loading image
100
+ function computeScaledSize(w,h) {
101
+ if(!autoScaleBox.checked) return {w,h};
102
+ const maxW = Math.max(100, Number(maxWInput.value) || 900);
103
+ const maxH = Math.max(80, Number(maxHInput.value) || 600);
104
+ const scale = Math.min(maxW / w, maxH / h, 1);
105
+ return { w: Math.max(1, Math.round(w*scale)), h: Math.max(1, Math.round(h*scale)) };
106
+ }
107
+
108
+ // Build block grid and arrays
109
+ function buildBlocks(blockSize) {
110
+ BLOCK = blockSize;
111
+ blocks = [];
112
+ let idx = 0;
113
+ for(let y=0; y < canvas.height; y += BLOCK){
114
+ for(let x=0; x < canvas.width; x += BLOCK){
115
+ blocks.push({x,y,index:idx++});
116
+ }
117
+ }
118
+ scoreMap = new Float32Array(blocks.length).fill(0);
119
+ bestPos = new Int32Array(blocks.length * 2).fill(-1);
120
+ nblocksEl.textContent = blocks.length;
121
+ if(!outputData) outputData = ctx.createImageData(canvas.width, canvas.height);
122
+ }
123
+
124
+ // load target image
125
+ upload.addEventListener('change', e => {
126
+ const f = e.target.files && e.target.files[0];
127
+ if(!f) return;
128
+ const img = new Image();
129
+ img.onload = () => {
130
+ const scaled = computeScaledSize(img.width, img.height);
131
+ canvas.width = tmp.width = scaled.w;
132
+ canvas.height = tmp.height = scaled.h;
133
+ tctx.clearRect(0,0,tmp.width,tmp.height);
134
+ tctx.drawImage(img,0,0,tmp.width,tmp.height);
135
+ targetImageData = tctx.getImageData(0,0,tmp.width,tmp.height);
136
+ outputData = ctx.createImageData(tmp.width, tmp.height);
137
+ // initialize output to neutral (black)
138
+ for(let i=0;i<outputData.data.length;i+=4){
139
+ outputData.data[i]=0; outputData.data[i+1]=0; outputData.data[i+2]=0; outputData.data[i+3]=255;
140
+ }
141
+ buildBlocks(Number(blockSelect.value));
142
+ avgScoreEl.textContent = '0';
143
+ pixelMode = false;
144
+ drawOutput();
145
+ };
146
+ img.src = URL.createObjectURL(f);
147
+ });
148
+
149
+ // start feed
150
+ startBtn.addEventListener('click', async () => {
151
+ if(!targetImageData){ alert('Upload a target image first.'); return; }
152
+ if(feedStream){
153
+ feedStream.getTracks().forEach(t=>t.stop());
154
+ feedStream = null;
155
+ }
156
+ try {
157
+ if(feedMode.value === 'camera'){
158
+ feedStream = await navigator.mediaDevices.getUserMedia({video:true});
159
+ } else {
160
+ feedStream = await navigator.mediaDevices.getDisplayMedia({video:true});
161
+ }
162
+ } catch(err){
163
+ alert('Could not start feed: ' + (err.message||err));
164
+ return;
165
+ }
166
+ video = document.createElement('video');
167
+ video.srcObject = feedStream;
168
+ video.muted = true; video.playsInline = true;
169
+ await video.play();
170
+ running = true;
171
+ frameCounter = 0;
172
+ requestAnimationFrame(mainLoop);
173
+ });
174
+
175
+ // reset
176
+ resetBtn.addEventListener('click', () => {
177
+ if(!targetImageData) return;
178
+ buildBlocks(Number(blockSelect.value));
179
+ for(let i=0;i<outputData.data.length;i+=4){
180
+ outputData.data[i]=0; outputData.data[i+1]=0; outputData.data[i+2]=0; outputData.data[i+3]=255;
181
+ }
182
+ drawOutput();
183
+ });
184
+
185
+ // export
186
+ exportBtn.addEventListener('click', () => {
187
+ if(!outputData) return;
188
+ ctx.putImageData(outputData,0,0);
189
+ const a=document.createElement('a');
190
+ a.href=canvas.toDataURL('image/png');
191
+ a.download='reconstruction.png';
192
+ a.click();
193
+ });
194
+
195
+ // change block size
196
+ blockSelect.addEventListener('change', () => {
197
+ if(!targetImageData) return;
198
+ buildBlocks(Number(blockSelect.value));
199
+ });
200
+
201
+ // Debug window open
202
+ debugBtn.addEventListener('click', () => {
203
+ if(debugWin && !debugWin.closed){ debugWin.focus(); return; }
204
+ debugWin = window.open('', '', 'width=600,height=600');
205
+ if(!debugWin) { alert('Popup blocked. Allow popups for debug window.'); return; }
206
+ debugWin.document.title = 'Reconstructor Debug';
207
+ debugWin.document.body.style.background = '#111';
208
+ debugWin.document.body.style.color = '#ddd';
209
+ debugWin.document.body.style.fontFamily = 'system-ui,monospace';
210
+ debugWin.document.body.innerHTML = `
211
+ <h3 style="margin:6px">Debug — Scoreboard & Heatmap</h3>
212
+ <div id="dbgStats" style="font-size:13px;margin:6px"></div>
213
+ <canvas id="dbgCanvas" width="320" height="200" style="border:1px solid #333;display:block;margin:6px"></canvas>
214
+ <div id="dbgList" style="max-height:250px;overflow:auto;font-size:12px;margin:6px"></div>
215
+ `;
216
+ });
217
+
218
+ // some helpers for random biased picks
219
+ function pickBlockBiased(){
220
+ // skewed random towards low scores: pick r in [0,1] then map to threshold
221
+ // using power to bias: lower power strengthens bias; tune = 2.2
222
+ const power = 2.2;
223
+ const r = Math.pow(Math.random(), 1/power) * 100;
224
+ // linear scan to find a block with score < r (fast enough). If none, fallback random.
225
+ for(let i=0;i<blocks.length;i++){
226
+ if(scoreMap[i] < r) return blocks[i];
227
+ }
228
+ return blocks[Math.random()*blocks.length|0];
229
+ }
230
+
231
+ // evaluate one candidate sample for block with early bail (returns score 0..100 or -Infinity for early bail)
232
+ function evaluateCandidateBlock(block, sx, sy, currentBestScore, tmpFrameData){
233
+ const pixels = BLOCK * BLOCK;
234
+ // use precomputed constants
235
+ const maxColorPerPixel = 195075; // 255^2 * 3 approximate for rgb distances
236
+ const maxLumDiffPerPixel = 255;
237
+ const maxTotal = pixels * (maxColorPerPixel + maxLumDiffPerPixel * 6); // same scale as scoring
238
+ const cutoff = maxTotal * (1 - currentBestScore / 100);
239
+
240
+ let diff = 0;
241
+ // iterate block pixels
242
+ for(let dy=0; dy < BLOCK; dy++){
243
+ const ty = block.y + dy;
244
+ const syi = sy + dy;
245
+ for(let dx=0; dx < BLOCK; dx++){
246
+ const tx = block.x + dx;
247
+ const sxi = sx + dx;
248
+ const ti = (ty * canvas.width + tx) * 4;
249
+ const vi = (syi * tmp.width + sxi) * 4;
250
+ const tr = targetImageData.data[ti], tg = targetImageData.data[ti+1], tb = targetImageData.data[ti+2];
251
+ const vr = tmpFrameData[vi], vg = tmpFrameData[vi+1], vb = tmpFrameData[vi+2];
252
+ diff += colorDistanceWeighted(tr,tg,tb, vr,vg,vb);
253
+ diff += Math.abs(luminance(tr,tg,tb) - luminance(vr,vg,vb)) * 6;
254
+ if(diff > cutoff) return -Infinity; // can't beat current best
255
+ }
256
+ }
257
+ const score = 100 * (1 - diff / maxTotal);
258
+ return Math.max(0, Math.min(100, score));
259
+ }
260
+
261
+ // evaluate multiple candidates and return best {score,x,y} or null
262
+ function evaluateCandidatesForBlock(block, numCandidates, currentBestScore, buckets, tmpFrameData){
263
+ let localBestScore = -Infinity;
264
+ let localBestXY = null;
265
+ const bi = block.index;
266
+
267
+ // try temporal memory first
268
+ const rx = bestPos[bi*2], ry = bestPos[bi*2+1];
269
+ if(rx >= 0 && ry >= 0){
270
+ const s = evaluateCandidateBlock(block, rx, ry, currentBestScore, tmpFrameData);
271
+ if(s !== -Infinity){ localBestScore = s; localBestXY = {x:rx,y:ry}; }
272
+ }
273
+
274
+ // pick candidates from chosen bucket based on target luminance
275
+ const cx = block.x + (BLOCK>>1), cy = block.y + (BLOCK>>1);
276
+ const tidx = (cy * tmp.width + cx) * 4;
277
+ const tR = targetImageData.data[tidx], tG = targetImageData.data[tidx+1], tB = targetImageData.data[tidx+2];
278
+ const tLum = luminance(tR,tG,tB);
279
+ let bucket = buckets.mid;
280
+ if(tLum < 85 && buckets.dark.length) bucket = buckets.dark;
281
+ else if(tLum > 170 && buckets.light.length) bucket = buckets.light;
282
+ else if(buckets.mid.length === 0) bucket = buckets.dark.length ? buckets.dark : buckets.light;
283
+
284
+ if(!bucket || bucket.length === 0) return localBestXY ? {bestScore: localBestScore, bestX: localBestXY.x, bestY: localBestXY.y} : null;
285
+
286
+ for(let c=0;c<numCandidates;c++){
287
+ const pick = bucket[Math.random()*bucket.length|0];
288
+ if(!pick) continue;
289
+ const sx = pick[0], sy = pick[1];
290
+ const s = evaluateCandidateBlock(block, sx, sy, Math.max(currentBestScore, localBestScore), tmpFrameData);
291
+ if(s === -Infinity) continue;
292
+ if(s > localBestScore){
293
+ localBestScore = s;
294
+ localBestXY = {x:sx,y:sy};
295
+ }
296
+ }
297
+
298
+ return localBestXY ? {bestScore: localBestScore, bestX: localBestXY.x, bestY: localBestXY.y} : null;
299
+ }
300
+
301
+ // copy block from tmpFrame into outputData
302
+ function copySampleToOutput(block, sx, sy, tmpFrameData){
303
+ for(let dy=0; dy < BLOCK; dy++){
304
+ const ty = block.y + dy;
305
+ const syi = sy + dy;
306
+ for(let dx=0; dx < BLOCK; dx++){
307
+ const tx = block.x + dx;
308
+ const sxi = sx + dx;
309
+ const oi = (ty * canvas.width + tx) * 4;
310
+ const vi = (syi * tmp.width + sxi) * 4;
311
+ outputData.data[oi] = tmpFrameData[vi];
312
+ outputData.data[oi+1] = tmpFrameData[vi+1];
313
+ outputData.data[oi+2] = tmpFrameData[vi+2];
314
+ outputData.data[oi+3] = 255;
315
+ }
316
+ }
317
+ }
318
+
319
+ // draw the current output to screen
320
+ function drawOutput(){
321
+ ctx.putImageData(outputData, 0, 0);
322
+ }
323
+
324
+ // Debug window updater
325
+ function updateDebugWindow(){
326
+ if(!debugWin || debugWin.closed) return;
327
+ try {
328
+ const doc = debugWin.document;
329
+ const stats = doc.getElementById('dbgStats');
330
+ const dbgCanvas = doc.getElementById('dbgCanvas');
331
+ const dbgList = doc.getElementById('dbgList');
332
+ if(!stats || !dbgCanvas || !dbgList) return;
333
+ // summary
334
+ let avg = 0;
335
+ for(let i=0;i<scoreMap.length;i++) avg += scoreMap[i];
336
+ avg = scoreMap.length ? avg/scoreMap.length : 0;
337
+ stats.innerHTML = `Avg score: ${avg.toFixed(1)}%<br>Blocks: ${blocks.length}<br>Block size: ${BLOCK}px<br>Frame: ${frameCounter}`;
338
+ // draw heatmap scaled to dbgCanvas
339
+ const dctx = dbgCanvas.getContext('2d');
340
+ const W = dbgCanvas.width, H = dbgCanvas.height;
341
+ dctx.clearRect(0,0,W,H);
342
+ // one rectangle per block (approx)
343
+ const cols = Math.ceil(Math.sqrt(blocks.length));
344
+ const cellW = Math.max(1, Math.floor(W / cols));
345
+ let i = 0;
346
+ for(const b of blocks){
347
+ const s = Math.max(0, Math.min(100, scoreMap[b.index]));
348
+ const h = Math.round(120 * (s/100)); // 0..120 greenish
349
+ dctx.fillStyle = `hsl(${h},80%,50%)`;
350
+ const cx = (i % cols) * cellW;
351
+ const cy = Math.floor(i/cols) * cellW;
352
+ dctx.fillRect(cx, cy, cellW, cellW);
353
+ i++;
354
+ }
355
+ // top 20 lowest-scoring blocks list
356
+ const arr = blocks.map(b=>({i:b.index, x:b.x, y:b.y, s:scoreMap[b.index]}));
357
+ arr.sort((a,b)=>a.s - b.s);
358
+ dbgList.innerHTML = '<b>Lowest scoring blocks</b><br>' + arr.slice(0,40).map(a=>`#${a.i} (${a.x},${a.y}) : ${a.s.toFixed(1)}%`).join('<br>');
359
+ } catch(e){
360
+ // ignore cross-window errors
361
+ }
362
+ }
363
+
364
+ // MAIN LOOP with repass system and adaptive block size
365
+ function mainLoop(){
366
+ if(!running) return;
367
+ requestAnimationFrame(mainLoop);
368
+ if(!video || !targetImageData || video.readyState < 2) return;
369
+
370
+ // draw video to tmp sized to canvas
371
+ tctx.drawImage(video, 0, 0, tmp.width, tmp.height);
372
+ tmpFrame = tctx.getImageData(0,0,tmp.width,tmp.height);
373
+ const tmpData = tmpFrame.data;
374
+
375
+ // build buckets: top-left coords where a BLOCK fits (stepped by BLOCK for performance)
376
+ const buckets = {dark:[], mid:[], light:[]};
377
+ for(let y=0; y <= tmp.height - BLOCK; y += BLOCK){
378
+ for(let x=0; x <= tmp.width - BLOCK; x += BLOCK){
379
+ const cx = x + (BLOCK>>1), cy = y + (BLOCK>>1);
380
+ const idx = (cy * tmp.width + cx) * 4;
381
+ const L = luminance(tmpData[idx], tmpData[idx+1], tmpData[idx+2]);
382
+ if(L < 85) buckets.dark.push([x,y]);
383
+ else if(L > 170) buckets.light.push([x,y]);
384
+ else buckets.mid.push([x,y]);
385
+ }
386
+ }
387
+
388
+ const attempts = Number(speedSlider.value);
389
+ const candidatesPerBlock = Number(candSlider.value);
390
+
391
+ // main improvement attempts biased toward low-score
392
+ for(let a=0; a<attempts; a++){
393
+ const block = pickBlockBiased();
394
+ if(!block) continue;
395
+ const bi = block.index;
396
+ const currentBest = scoreMap[bi];
397
+ const result = evaluateCandidatesForBlock(block, candidatesPerBlock, currentBest, buckets, tmpData);
398
+ if(result && result.bestScore > currentBest){
399
+ scoreMap[bi] = result.bestScore;
400
+ bestPos[bi*2] = result.bestX;
401
+ bestPos[bi*2+1] = result.bestY;
402
+ copySampleToOutput(block, result.bestX, result.bestY, tmpData);
403
+ }
404
+ }
405
+
406
+ // repass: every N frames perform a re-evaluation of some or all blocks to see if better matches have appeared
407
+ frameCounter++;
408
+ const repassEvery = Math.max(1, Number(repassFramesInput.value) || 60);
409
+ if(frameCounter % repassEvery === 0){
410
+ // do a lighter global repass: iterate all blocks but with fewer candidates
411
+ const repassCandidates = Math.max(1, Math.floor(candidatesPerBlock/2));
412
+ for(let i=0;i<blocks.length;i++){
413
+ const block = blocks[i];
414
+ const cur = scoreMap[i];
415
+ const res = evaluateCandidatesForBlock(block, repassCandidates, cur, buckets, tmpData);
416
+ if(res && res.bestScore > cur){
417
+ scoreMap[i] = res.bestScore;
418
+ bestPos[i*2] = res.bestX; bestPos[i*2+1] = res.bestY;
419
+ copySampleToOutput(block, res.bestX, res.bestY, tmpData);
420
+ }
421
+ }
422
+ }
423
+
424
+ // adaptive block-size refinement (coarse -> fine)
425
+ let avg = 0;
426
+ for(let i=0;i<scoreMap.length;i++) avg += scoreMap[i];
427
+ avg = scoreMap.length ? (avg / scoreMap.length) : 0;
428
+ avgScoreEl.textContent = Math.round(avg);
429
+
430
+ // thresholds tunable
431
+ if(!pixelMode){
432
+ if(avg > 80 && BLOCK > 4){
433
+ buildBlocks(Math.max(1, BLOCK >> 1));
434
+ } else if(avg > 94 && BLOCK > 1){
435
+ buildBlocks(1); // final pixel mode
436
+ pixelMode = true;
437
+ }
438
+ }
439
+
440
+ // draw output and optional heatmap
441
+ drawOutput();
442
+ if(heatToggle.checked){
443
+ ctx.save();
444
+ for(const b of blocks){
445
+ const s = Math.max(0, Math.min(100, scoreMap[b.index]));
446
+ const hue = Math.round(120 * (s/100));
447
+ ctx.fillStyle = `hsla(${hue},80%,50%,0.13)`;
448
+ ctx.fillRect(b.x, b.y, BLOCK, BLOCK);
449
+ }
450
+ ctx.restore();
451
+ }
452
+
453
+ // update debug window periodically
454
+ if(debugWin && !debugWin.closed && (frameCounter % 10 === 0)) updateDebugWindow();
455
+ }
456
+
457
+ // initial minimal canvas
458
+ (function init(){
459
+ canvas.width = 640; canvas.height = 360;
460
+ tmp.width = 640; tmp.height = 360;
461
+ outputData = ctx.createImageData(canvas.width, canvas.height);
462
+ for(let i=0;i<outputData.data.length;i+=4){ outputData.data[i]=0; outputData.data[i+1]=0; outputData.data[i+2]=0; outputData.data[i+3]=255; }
463
+ ctx.putImageData(outputData,0,0);
464
+ buildBlocks(Number(blockSelect.value));
465
+ })();
466
+
467
+ </script>
468
+ </body>
469
  </html>