diff options
author | Robin Fernandes <robin@soal.org> | 2022-09-23 00:54:32 +0000 |
---|---|---|
committer | Robin Fernandes <robin@soal.org> | 2022-09-23 00:54:32 +0000 |
commit | 03738668c08852d717b0564fc0c3da89877e52b8 (patch) | |
tree | 975bd8f2f610901ee45a5c3d1885d93074cdddc2 /javascript/imageMaskFix.js | |
parent | 25eb1e3d9072a060a253abce6889e7fef440dfe1 (diff) | |
parent | 19fc3e82794245b663467a9be076271f6ae33f26 (diff) | |
download | stable-diffusion-webui-gfx803-03738668c08852d717b0564fc0c3da89877e52b8.tar.gz stable-diffusion-webui-gfx803-03738668c08852d717b0564fc0c3da89877e52b8.tar.bz2 stable-diffusion-webui-gfx803-03738668c08852d717b0564fc0c3da89877e52b8.zip |
Merge from master
Diffstat (limited to 'javascript/imageMaskFix.js')
-rw-r--r-- | javascript/imageMaskFix.js | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/javascript/imageMaskFix.js b/javascript/imageMaskFix.js new file mode 100644 index 00000000..3d77bfe9 --- /dev/null +++ b/javascript/imageMaskFix.js @@ -0,0 +1,45 @@ +/** + * temporary fix for https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/668 + * @see https://github.com/gradio-app/gradio/issues/1721 + */ +window.addEventListener( 'resize', () => imageMaskResize()); +function imageMaskResize() { + const canvases = gradioApp().querySelectorAll('#img2maskimg .touch-none canvas'); + if ( ! canvases.length ) { + canvases_fixed = false; + window.removeEventListener( 'resize', imageMaskResize ); + return; + } + + const wrapper = canvases[0].closest('.touch-none'); + const previewImage = wrapper.previousElementSibling; + + if ( ! previewImage.complete ) { + previewImage.addEventListener( 'load', () => imageMaskResize()); + return; + } + + const w = previewImage.width; + const h = previewImage.height; + const nw = previewImage.naturalWidth; + const nh = previewImage.naturalHeight; + const portrait = nh > nw; + const factor = portrait; + + const wW = Math.min(w, portrait ? h/nh*nw : w/nw*nw); + const wH = Math.min(h, portrait ? h/nh*nh : w/nw*nh); + + wrapper.style.width = `${wW}px`; + wrapper.style.height = `${wH}px`; + wrapper.style.left = `${(w-wW)/2}px`; + wrapper.style.top = `${(h-wH)/2}px`; + + canvases.forEach( c => { + c.style.width = c.style.height = ''; + c.style.maxWidth = '100%'; + c.style.maxHeight = '100%'; + c.style.objectFit = 'contain'; + }); + } + + onUiUpdate(() => imageMaskResize());
\ No newline at end of file |