(function () { if (!globalThis.LLuL) globalThis.LLuL = {}; const LLuL = globalThis.LLuL; function id(type, s) { return `llul-${type}-${s}`; } function isDark() { return gradioApp().querySelector('.dark') !== null; } const M = 2; function setSize(canvas, width, height) { width = Math.floor(+width / M); height = Math.floor(+height / M); if (canvas.width != width) canvas.width = width; if (canvas.height != height) canvas.height = height; } function updateXY(canvas) { let x = +canvas.dataset.x, y = +canvas.dataset.y, m = +canvas.dataset.m, mm = Math.pow(2, m), w = +canvas.width * M, h = +canvas.height * M; if (x < 0) x = 0; if (w < x + w / mm) x = Math.floor(w - w / mm); if (y < 0) y = 0; if (h < y + h / mm) y = Math.floor(h - h / mm); canvas.dataset.x = x; canvas.dataset.y = y; canvas.dataset.m = m; canvas.parentNode.querySelector('.llul-pos-x').value = x; canvas.parentNode.querySelector('.llul-pos-y').value = y; } let last_image = new Image(); let hide_image = true; async function draw(canvas) { const x = +canvas.dataset.x, y = +canvas.dataset.y, m = +canvas.dataset.m, mm = Math.pow(2, m), w = +canvas.width, h = +canvas.height, bg = canvas.dataset.bg; const ctx = canvas.getContext('2d'); if (bg) { if (last_image?.src === bg) { // do nothing } else { await (new Promise(resolve => { last_image.onload = () => resolve(); last_image.src = bg; })); } hide_image = false; } else { last_image.src = ''; hide_image = true; } if (last_image.src && !hide_image) { ctx.drawImage(last_image, 0, 0, +last_image.width, +last_image.height, 0, 0, +canvas.width, +canvas.height); } else { const bgcolor = isDark() ? 'black' : 'white'; ctx.fillStyle = bgcolor; ctx.fillRect(0, 0, +canvas.width, +canvas.height); } // Координаты и размеры для рисования const rectX = x / M; const rectY = y / M; const rectW = Math.floor(w / mm); const rectH = Math.floor(h / mm); // 1. Рисуем полупрозрачную заливку (чтобы видеть область, но и видеть, что под ней) ctx.fillStyle = 'rgba(255, 255, 255, 0.2)'; // Белый, прозрачность 20% ctx.fillRect(rectX, rectY, rectW, rectH); // 2. Рисуем яркую рамку (чтобы видеть границы) ctx.strokeStyle = '#ff0000'; // Красная рамка ctx.lineWidth = 2; ctx.strokeRect(rectX, rectY, rectW, rectH); // 3. (Опционально) Крестик в центре для точного прицеливания ctx.beginPath(); ctx.moveTo(rectX + rectW / 2, rectY + rectH / 2 - 5); ctx.lineTo(rectX + rectW / 2, rectY + rectH / 2 + 5); ctx.moveTo(rectX + rectW / 2 - 5, rectY + rectH / 2); ctx.lineTo(rectX + rectW / 2 + 5, rectY + rectH / 2); ctx.stroke(); } async function update_gradio(type, canvas) { await LLuL.js2py(type, 'x', +canvas.dataset.x); await LLuL.js2py(type, 'y', +canvas.dataset.y); } function init(type) { const $$ = (x,n) => Array.from(gradioApp().querySelectorAll(x)).at(n); const $ = x => $$(x, -1); if (!$('#' + id(type, 'accordion'))) return false; const cont = $('#' + id(type, 'container')); const x = $('#' + id(type, 'x')); const y = $('#' + id(type, 'y')); const m = $(`#${id(type, 'm')} input[type=number]`); const ms = $(`#${id(type, 'm')} input[type=range]`); if (!cont || !x || !y || !m || !ms) return false; if (cont.querySelector('canvas')) return true; // already called const width = $$(`#${type}_width input[type=number]`, 0); const height = $$(`#${type}_height input[type=number]`, 0); const width2 = $$(`#${type}_width input[type=range]`, 0); const height2 = $$(`#${type}_height input[type=range]`, 0); const pos_x = Math.floor(+width.value / 4); const pos_y = Math.floor(+height.value / 4); const pos_cont = document.createElement('div'); pos_cont.innerHTML = `