imageMaskFix.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. canvases_fixed = false; // TODO: this is unused..?
  9. window.removeEventListener( 'resize', imageMaskResize );
  10. return;
  11. }
  12. const wrapper = canvases[0].closest('.touch-none');
  13. const previewImage = wrapper.previousElementSibling;
  14. if ( ! previewImage.complete ) {
  15. previewImage.addEventListener( 'load', imageMaskResize);
  16. return;
  17. }
  18. const w = previewImage.width;
  19. const h = previewImage.height;
  20. const nw = previewImage.naturalWidth;
  21. const nh = previewImage.naturalHeight;
  22. const portrait = nh > nw;
  23. const wW = Math.min(w, portrait ? h/nh*nw : w/nw*nw);
  24. const wH = Math.min(h, portrait ? h/nh*nh : w/nw*nh);
  25. wrapper.style.width = `${wW}px`;
  26. wrapper.style.height = `${wH}px`;
  27. wrapper.style.left = `0px`;
  28. wrapper.style.top = `0px`;
  29. canvases.forEach( c => {
  30. c.style.width = c.style.height = '';
  31. c.style.maxWidth = '100%';
  32. c.style.maxHeight = '100%';
  33. c.style.objectFit = 'contain';
  34. });
  35. }
  36. onUiUpdate(imageMaskResize);
  37. window.addEventListener( 'resize', imageMaskResize);