File size: 7,021 Bytes
0690950 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
fabric.Object.prototype.transparentCorners = false;
fabric.Object.prototype.cornerColor = '#108ce6';
fabric.Object.prototype.borderColor = '#108ce6';
fabric.Object.prototype.cornerSize = 10;
fabric.Object.prototype.lockRotation = false;
fabric.Object.prototype.perPixelTargetFind = true;
let depth_obj = {
// width, height
resolution: [512, 512]
}
let depth_executed = false;
let depth_addOpacity = 100;
let depth_bgColor = "#000";
function depth_gradioApp() {
const elems = document.getElementsByTagName('gradio-app')
const gradioShadowRoot = elems.length == 0 ? null : elems[0].shadowRoot
root = !!gradioShadowRoot ? gradioShadowRoot : document;
let style = document.createElement("style");
return root;
}
function depth_calcResolution(resolution) {
const width = resolution[0]
const height = resolution[1]
const viewportWidth = window.innerWidth / 2.25;
const viewportHeight = window.innerHeight * 0.75;
const ratio = Math.min(viewportWidth / width, viewportHeight / height);
return { width: width * ratio, height: height * ratio }
}
function depth_resizeCanvas(width, height) {
const elem = depth_lib_elem;
let resolution = depth_calcResolution([width, height])
depth_lib_canvas.setWidth(width);
depth_lib_canvas.setHeight(height);
elem.style.width = resolution["width"] + "px"
elem.style.height = resolution["height"] + "px"
elem.nextElementSibling.style.width = resolution["width"] + "px"
elem.nextElementSibling.style.height = resolution["height"] + "px"
elem.parentElement.style.width = resolution["width"] + "px"
elem.parentElement.style.height = resolution["height"] + "px"
}
function depth_addImg(path) {
fabric.Image.fromURL(path, function (oImg) {
depth_lib_canvas.add(oImg);
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.setActiveObject(oImg);
oImg.set({
opacity: depth_addOpacity
});
});
depth_lib_canvas.requestRenderAll();
}
function depth_initCanvas(elem) {
window.depth_lib_canvas = new fabric.Canvas(elem, {
backgroundColor: '#000',
// selection: false,
preserveObjectStacking: true
});
window.depth_lib_elem = elem
depth_resizeCanvas(...depth_obj.resolution)
}
function depth_resetCanvas() {
depth_lib_canvas.clear();
depth_lib_canvas.backgroundColor = depth_bgColor;
}
function depth_savePNG() {
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.renderAll()
depth_lib_elem.toBlob((blob) => {
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = "pose.png";
a.click();
URL.revokeObjectURL(a.href);
});
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0.5
depth_lib_canvas.renderAll()
return
}
function depth_addBackground() {
const input = document.createElement("input");
input.type = "file"
input.accept = "image/*"
input.addEventListener("change", function (e) {
const file = e.target.files[0];
var fileReader = new FileReader();
fileReader.onload = function () {
var dataUri = this.result;
depth_lib_canvas.setBackgroundImage(dataUri, depth_lib_canvas.renderAll.bind(depth_lib_canvas), {
opacity: 0.5
});
}
fileReader.readAsDataURL(file);
})
input.click()
}
function depth_removeBackground() {
depth_lib_canvas.setBackgroundImage(0, depth_lib_canvas.renderAll.bind(depth_lib_canvas));
}
function depth_sendImage() {
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0;
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.renderAll();
depth_lib_elem.toBlob(async (blob) => {
const file = new File(([blob]), "pose.png");
const dt = new DataTransfer();
dt.items.add(file);
const list = dt.files;
const divControlNet = depth_gradioApp().querySelector("#txt2img_controlnet");
if (divControlNet) {
switch_to_txt2img();
// open the ControlNet accordion if it's not already open
// but give up if it takes longer than 5 secs
labelControlNet = divControlNet.querySelector("div.label-wrap");
if (!labelControlNet.classList.contains("open")) {
labelControlNet.click();
let waitUntilHasClassOpenCount = 0;
const waitUntilHasClassOpen = async () => {
waitUntilHasClassOpenCount++;
if (waitUntilHasClassOpenCount > 50) {
return false;
} else if (labelControlNet.classList.contains("open")) {
return true;
} else {
setTimeout(() => waitUntilHasClassOpen(), 100)
}
};
await waitUntilHasClassOpen();
}
const input = divControlNet.querySelector("input[type='file']");
const button = divControlNet.querySelector("button[aria-label='Clear']")
button && button.click();
input.value = "";
input.files = list;
const event = new Event('change', { 'bubbles': true, "composed": true });
input.dispatchEvent(event);
}
});
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0.5
depth_lib_canvas.renderAll()
}
function depth_setBrightness(br) {
hex = br.toString(16).padStart(2, "0");
depth_bgColor = "#" + hex + hex + hex;
depth_lib_canvas.backgroundColor = depth_bgColor;
depth_lib_canvas.requestRenderAll();
}
function depth_setOpacity(op) {
depth_addOpacity = op;
if (!depth_lib_canvas.getActiveObject()) {
return;
}
for (let i = 0; i < depth_lib_canvas.getActiveObjects().length; i++) {
const element = depth_lib_canvas.getActiveObjects()[i];
element.opacity = depth_addOpacity;
}
depth_lib_canvas.requestRenderAll();
}
function depth_removeSelection() {
depth_lib_canvas.remove(...depth_lib_canvas.getActiveObjects());
depth_lib_canvas.discardActiveObject().renderAll();
}
window.addEventListener('DOMContentLoaded', () => {
const observer = new MutationObserver((m) => {
if (!depth_executed && depth_gradioApp().querySelector('#depth_lib_canvas')) {
depth_executed = true;
depth_initCanvas(depth_gradioApp().querySelector('#depth_lib_canvas'))
// depth_gradioApp().querySelectorAll("#tabs > div > button").forEach((elem) => {
// if (elem.innerText === "OpenPose Editor") elem.click()
// })
observer.disconnect();
}
})
observer.observe(depth_gradioApp(), { childList: true, subtree: true })
})
|