2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
let currentWidth = null;
|
|
|
|
let currentHeight = null;
|
|
|
|
let arFrameTimeout = setTimeout(function() {}, 0);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-21 08:11:42 -06:00
|
|
|
function dimensionChange(e, is_width, is_height) {
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-21 08:11:42 -06:00
|
|
|
if (is_width) {
|
2022-09-21 18:51:49 -06:00
|
|
|
currentWidth = e.target.value * 1.0;
|
|
|
|
}
|
2022-10-21 08:11:42 -06:00
|
|
|
if (is_height) {
|
2022-09-21 18:51:49 -06:00
|
|
|
currentHeight = e.target.value * 1.0;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-03-25 12:44:41 -06:00
|
|
|
var inImg2img = gradioApp().querySelector("#tab_img2img").style.display == "block";
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
if (!inImg2img) {
|
|
|
|
return;
|
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
var targetElement = null;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-10-21 08:41:47 -06:00
|
|
|
var tabIndex = get_tab_index('mode_img2img');
|
2023-01-14 15:45:08 -07:00
|
|
|
if (tabIndex == 0) { // img2img
|
2023-03-25 12:44:41 -06:00
|
|
|
targetElement = gradioApp().querySelector('#img2img_image div[data-testid=image] img');
|
2023-01-14 15:45:08 -07:00
|
|
|
} else if (tabIndex == 1) { //Sketch
|
|
|
|
targetElement = gradioApp().querySelector('#img2img_sketch div[data-testid=image] img');
|
|
|
|
} else if (tabIndex == 2) { // Inpaint
|
2022-10-21 08:41:47 -06:00
|
|
|
targetElement = gradioApp().querySelector('#img2maskimg div[data-testid=image] img');
|
2023-01-14 15:45:08 -07:00
|
|
|
} else if (tabIndex == 3) { // Inpaint sketch
|
|
|
|
targetElement = gradioApp().querySelector('#inpaint_sketch div[data-testid=image] img');
|
2022-09-21 18:51:49 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
if (targetElement) {
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
var arPreviewRect = gradioApp().querySelector('#imageARPreview');
|
|
|
|
if (!arPreviewRect) {
|
|
|
|
arPreviewRect = document.createElement('div');
|
|
|
|
arPreviewRect.id = "imageARPreview";
|
2023-03-25 12:44:41 -06:00
|
|
|
gradioApp().appendChild(arPreviewRect);
|
2022-09-21 18:51:49 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
var viewportOffset = targetElement.getBoundingClientRect();
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-30 13:08:52 -06:00
|
|
|
var viewportscale = Math.min(targetElement.clientWidth / targetElement.naturalWidth, targetElement.clientHeight / targetElement.naturalHeight);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-30 13:08:52 -06:00
|
|
|
var scaledx = targetElement.naturalWidth * viewportscale;
|
|
|
|
var scaledy = targetElement.naturalHeight * viewportscale;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-05-18 00:59:10 -06:00
|
|
|
var cleintRectTop = (viewportOffset.top + window.scrollY);
|
|
|
|
var cleintRectLeft = (viewportOffset.left + window.scrollX);
|
|
|
|
var cleintRectCentreY = cleintRectTop + (targetElement.clientHeight / 2);
|
2023-04-30 13:08:52 -06:00
|
|
|
var cleintRectCentreX = cleintRectLeft + (targetElement.clientWidth / 2);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-04-30 13:12:24 -06:00
|
|
|
var arscale = Math.min(scaledx / currentWidth, scaledy / currentHeight);
|
2023-04-30 13:08:52 -06:00
|
|
|
var arscaledx = currentWidth * arscale;
|
|
|
|
var arscaledy = currentHeight * arscale;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-05-18 00:59:10 -06:00
|
|
|
var arRectTop = cleintRectCentreY - (arscaledy / 2);
|
|
|
|
var arRectLeft = cleintRectCentreX - (arscaledx / 2);
|
|
|
|
var arRectWidth = arscaledx;
|
2023-04-30 13:12:24 -06:00
|
|
|
var arRectHeight = arscaledy;
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-05-18 00:59:10 -06:00
|
|
|
arPreviewRect.style.top = arRectTop + 'px';
|
2022-09-21 18:51:49 -06:00
|
|
|
arPreviewRect.style.left = arRectLeft + 'px';
|
|
|
|
arPreviewRect.style.width = arRectWidth + 'px';
|
|
|
|
arPreviewRect.style.height = arRectHeight + 'px';
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
clearTimeout(arFrameTimeout);
|
|
|
|
arFrameTimeout = setTimeout(function() {
|
|
|
|
arPreviewRect.style.display = 'none';
|
|
|
|
}, 2000);
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
arPreviewRect.style.display = 'block';
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2022-09-21 18:51:49 -06:00
|
|
|
}
|
2023-05-17 06:46:58 -06:00
|
|
|
|
|
|
|
|
2023-05-25 00:09:13 -06:00
|
|
|
onAfterUiUpdate(function() {
|
2022-09-21 18:51:49 -06:00
|
|
|
var arPreviewRect = gradioApp().querySelector('#imageARPreview');
|
|
|
|
if (arPreviewRect) {
|
|
|
|
arPreviewRect.style.display = 'none';
|
|
|
|
}
|
2023-03-25 12:44:41 -06:00
|
|
|
var tabImg2img = gradioApp().querySelector("#tab_img2img");
|
|
|
|
if (tabImg2img) {
|
|
|
|
var inImg2img = tabImg2img.style.display == "block";
|
|
|
|
if (inImg2img) {
|
|
|
|
let inputs = gradioApp().querySelectorAll('input');
|
|
|
|
inputs.forEach(function(e) {
|
|
|
|
var is_width = e.parentElement.id == "img2img_width";
|
|
|
|
var is_height = e.parentElement.id == "img2img_height";
|
2023-05-17 06:46:58 -06:00
|
|
|
|
2023-03-25 12:44:41 -06:00
|
|
|
if ((is_width || is_height) && !e.classList.contains('scrollwatch')) {
|
|
|
|
e.addEventListener('input', function(e) {
|
|
|
|
dimensionChange(e, is_width, is_height);
|
2023-05-17 06:46:58 -06:00
|
|
|
});
|
2023-03-25 12:44:41 -06:00
|
|
|
e.classList.add('scrollwatch');
|
|
|
|
}
|
|
|
|
if (is_width) {
|
|
|
|
currentWidth = e.value * 1.0;
|
|
|
|
}
|
|
|
|
if (is_height) {
|
|
|
|
currentHeight = e.value * 1.0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-09-21 18:51:49 -06:00
|
|
|
});
|