Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
automatic1111
GitHub Repository: automatic1111/stable-diffusion-webui
Path: blob/master/javascript/imageMaskFix.js
3055 views
1
/**
2
* temporary fix for https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/668
3
* @see https://github.com/gradio-app/gradio/issues/1721
4
*/
5
function imageMaskResize() {
6
const canvases = gradioApp().querySelectorAll('#img2maskimg .touch-none canvas');
7
if (!canvases.length) {
8
window.removeEventListener('resize', imageMaskResize);
9
return;
10
}
11
12
const wrapper = canvases[0].closest('.touch-none');
13
const previewImage = wrapper.previousElementSibling;
14
15
if (!previewImage.complete) {
16
previewImage.addEventListener('load', imageMaskResize);
17
return;
18
}
19
20
const w = previewImage.width;
21
const h = previewImage.height;
22
const nw = previewImage.naturalWidth;
23
const nh = previewImage.naturalHeight;
24
const portrait = nh > nw;
25
26
const wW = Math.min(w, portrait ? h / nh * nw : w / nw * nw);
27
const wH = Math.min(h, portrait ? h / nh * nh : w / nw * nh);
28
29
wrapper.style.width = `${wW}px`;
30
wrapper.style.height = `${wH}px`;
31
wrapper.style.left = `0px`;
32
wrapper.style.top = `0px`;
33
34
canvases.forEach(c => {
35
c.style.width = c.style.height = '';
36
c.style.maxWidth = '100%';
37
c.style.maxHeight = '100%';
38
c.style.objectFit = 'contain';
39
});
40
}
41
42
onAfterUiUpdate(imageMaskResize);
43
window.addEventListener('resize', imageMaskResize);
44
45