-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Expand file tree
/
Copy pathaspectRatioOverlay.js
More file actions
78 lines (62 loc) · 2.98 KB
/
aspectRatioOverlay.js
File metadata and controls
78 lines (62 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
let currentWidth = null;
let currentHeight = null;
let arFrameTimeout = null;
const gradio = gradioApp(); // cache gradioApp reference
// Cache AR preview div globally
let arPreviewRect = gradio.querySelector('#imageARPreview');
if (!arPreviewRect) {
arPreviewRect = document.createElement('div');
arPreviewRect.id = "imageARPreview";
gradio.appendChild(arPreviewRect);
}
function dimensionChange(e, isWidth, isHeight) {
if (isWidth) currentWidth = +e.target.value;
if (isHeight) currentHeight = +e.target.value;
if (gradio.querySelector("#tab_img2img").style.display !== "block") return;
const tabIndex = get_tab_index('mode_img2img');
const tabSelectors = [
'#img2img_image div[data-testid=image] img',
'#img2img_sketch div[data-testid=image] img',
'#img2maskimg div[data-testid=image] img',
'#inpaint_sketch div[data-testid=image] img'
];
const targetElement = gradio.querySelector(tabSelectors[tabIndex]);
if (!targetElement || !currentWidth || !currentHeight) return;
const viewportRect = targetElement.getBoundingClientRect();
const viewportScale = Math.min(targetElement.clientWidth / targetElement.naturalWidth,
targetElement.clientHeight / targetElement.naturalHeight);
const scaledX = targetElement.naturalWidth * viewportScale;
const scaledY = targetElement.naturalHeight * viewportScale;
const centreX = viewportRect.left + window.scrollX + targetElement.clientWidth / 2;
const centreY = viewportRect.top + window.scrollY + targetElement.clientHeight / 2;
const arScale = Math.min(scaledX / currentWidth, scaledY / currentHeight);
const arWidth = currentWidth * arScale;
const arHeight = currentHeight * arScale;
arPreviewRect.style.top = (centreY - arHeight / 2) + 'px';
arPreviewRect.style.left = (centreX - arWidth / 2) + 'px';
arPreviewRect.style.width = arWidth + 'px';
arPreviewRect.style.height = arHeight + 'px';
arPreviewRect.style.display = 'block';
if (arFrameTimeout) cancelAnimationFrame(arFrameTimeout);
arFrameTimeout = requestAnimationFrame(() => {
setTimeout(() => {
arPreviewRect.style.display = 'none';
}, 2000);
});
}
onAfterUiUpdate(function() {
arPreviewRect.style.display = 'none';
const tabImg2img = gradio.querySelector("#tab_img2img");
if (tabImg2img && tabImg2img.style.display === "block") {
gradio.querySelectorAll('input').forEach(input => {
const isWidth = input.parentElement.id === "img2img_width";
const isHeight = input.parentElement.id === "img2img_height";
if ((isWidth || isHeight) && !input.classList.contains('scrollwatch')) {
input.addEventListener('input', e => dimensionChange(e, isWidth, isHeight));
input.classList.add('scrollwatch');
}
if (isWidth) currentWidth = +input.value;
if (isHeight) currentHeight = +input.value;
});
}
});