imageMaskFix.js 1.3 KB
Newer Older
1 2 3 4 5 6
/**
 * temporary fix for https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/668
 * @see https://github.com/gradio-app/gradio/issues/1721
 */
function imageMaskResize() {
    const canvases = gradioApp().querySelectorAll('#img2maskimg .touch-none canvas');
7 8 9
    if (!canvases.length) {
        window.removeEventListener('resize', imageMaskResize);
        return;
10 11 12 13 14
    }

    const wrapper = canvases[0].closest('.touch-none');
    const previewImage = wrapper.previousElementSibling;

15 16
    if (!previewImage.complete) {
        previewImage.addEventListener('load', imageMaskResize);
17 18 19 20 21 22 23 24 25
        return;
    }

    const w = previewImage.width;
    const h = previewImage.height;
    const nw = previewImage.naturalWidth;
    const nh = previewImage.naturalHeight;
    const portrait = nh > nw;

26 27
    const wW = Math.min(w, portrait ? h / nh * nw : w / nw * nw);
    const wH = Math.min(h, portrait ? h / nh * nh : w / nw * nh);
28 29 30

    wrapper.style.width = `${wW}px`;
    wrapper.style.height = `${wH}px`;
F
fortypercnt 已提交
31 32
    wrapper.style.left = `0px`;
    wrapper.style.top = `0px`;
33

34
    canvases.forEach(c => {
35 36 37 38 39
        c.style.width = c.style.height = '';
        c.style.maxWidth = '100%';
        c.style.maxHeight = '100%';
        c.style.objectFit = 'contain';
    });
40 41
}

42
onAfterUiUpdate(imageMaskResize);
43
window.addEventListener('resize', imageMaskResize);