imageMaskFix.js 1.3 KB

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