'Use for Controlnet', Drag'n'Drop (#1501)

* Drop area for Controlnet

* 'Use for Controlnet', DND
This commit is contained in:
JeLuF 2023-08-16 06:53:12 +02:00 committed by GitHub
parent 074c566826
commit a1854d3734
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 23 deletions

View File

@ -517,6 +517,7 @@ function showImages(reqBody, res, outputContainer, livePreview) {
const imageRedoBuffer = [] const imageRedoBuffer = []
let buttons = [ let buttons = [
{ text: "Use as Input", on_click: onUseAsInputClick }, { text: "Use as Input", on_click: onUseAsInputClick },
{ text: "Use for Controlnet", on_click: onUseForControlnetClick },
[ [
{ {
html: '<i class="fa-solid fa-download"></i> Download Image', html: '<i class="fa-solid fa-download"></i> Download Image',
@ -627,6 +628,10 @@ function onUseAsInputClick(req, img) {
maskSetting.checked = false maskSetting.checked = false
} }
function onUseForControlnetClick(req, img) {
controlImagePreview.src = img.src
}
function getDownloadFilename(img, suffix) { function getDownloadFilename(img, suffix) {
const imageSeed = img.getAttribute("data-seed") const imageSeed = img.getAttribute("data-seed")
const imagePrompt = img.getAttribute("data-prompt") const imagePrompt = img.getAttribute("data-prompt")

View File

@ -191,19 +191,32 @@
function createDropAreas(container) { function createDropAreas(container) {
// Create two drop areas // Create two drop areas
const dropAreaI2I = document.createElement("div") const dropAreaI2I = createElement("div", {id: "drop-area-I2I"}, ["drop-area"], "Use as Image2Image source")
dropAreaI2I.setAttribute("id", "drop-area-I2I")
dropAreaI2I.setAttribute("class", "drop-area")
dropAreaI2I.innerHTML = "Use as Image2Image source"
container.appendChild(dropAreaI2I) container.appendChild(dropAreaI2I)
const dropAreaMD = document.createElement("div") const dropAreaMD = createElement("div", {id: "drop-area-MD"}, ["drop-area"], "Extract embedded metadata")
dropAreaMD.setAttribute("id", "drop-area-MD")
dropAreaMD.setAttribute("class", "drop-area")
dropAreaMD.innerHTML = "Extract embedded metadata"
container.appendChild(dropAreaMD) container.appendChild(dropAreaMD)
const dropAreaCN = createElement("div", {id: "drop-area-CN"}, ["drop-area"], "Use as Controlnet image")
container.appendChild(dropAreaCN)
// Add event listeners to drop areas // Add event listeners to drop areas
dropAreaCN.addEventListener("dragenter", function(event) {
event.preventDefault()
dropAreaCN.style.backgroundColor = 'darkGreen'
})
dropAreaCN.addEventListener("dragleave", function(event) {
event.preventDefault()
dropAreaCN.style.backgroundColor = ''
})
dropAreaCN.addEventListener("drop", function(event) {
event.stopPropagation()
event.preventDefault()
hideDropAreas()
getImageFromDropEvent(event, e => controlImagePreview.src=e)
})
dropAreaI2I.addEventListener("dragenter", function(event) { dropAreaI2I.addEventListener("dragenter", function(event) {
event.preventDefault() event.preventDefault()
dropAreaI2I.style.backgroundColor = 'darkGreen' dropAreaI2I.style.backgroundColor = 'darkGreen'
@ -212,16 +225,12 @@
event.preventDefault() event.preventDefault()
dropAreaI2I.style.backgroundColor = '' dropAreaI2I.style.backgroundColor = ''
}) })
dropAreaI2I.addEventListener("drop", function(event) { function getImageFromDropEvent(event, callback) {
event.stopPropagation();
event.preventDefault();
hideDropAreas()
// Find the first image file, uri, or moz-url in the items list // Find the first image file, uri, or moz-url in the items list
let imageItem = null; let imageItem = null
for (let i = 0; i < event.dataTransfer.items.length; i++) { for (let i = 0; i < event.dataTransfer.items.length; i++) {
let item = event.dataTransfer.items[i]; let item = event.dataTransfer.items[i]
if (item.kind === 'file' && item.type.startsWith('image/')) { if (item.kind === 'file' && item.type.startsWith('image/')) {
imageItem = item; imageItem = item;
break; break;
@ -258,18 +267,22 @@
// Create a FileReader object to read the dropped file as a data URL // Create a FileReader object to read the dropped file as a data URL
let reader = new FileReader(); let reader = new FileReader();
reader.onload = function(e) { reader.onload = function(e) {
// Set the src attribute of the img element to the data URL callback(e.target.result)
imageObj.src = e.target.result;
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
} else { } else {
// If the item is a URL, retrieve it and use it to load the image // If the item is a URL, retrieve it and use it to load the image
imageItem.getAsString(function(url) { imageItem.getAsString(callback)
// Set the src attribute of the img element to the URL
imageObj.src = url;
});
} }
} }
}
dropAreaI2I.addEventListener("drop", function(event) {
event.stopPropagation()
event.preventDefault()
hideDropAreas()
getImageFromDropEvent(event, e => imageObj.src=e)
}) })
dropAreaMD.addEventListener("dragenter", function(event) { dropAreaMD.addEventListener("dragenter", function(event) {