Image item refactor and redesign

This commit is contained in:
Haka 2022-09-25 02:55:11 +03:00
parent 489aae7a46
commit 62048c68f0
2 changed files with 88 additions and 124 deletions

View File

@ -69,38 +69,28 @@ label {
font-size: 8pt; font-size: 8pt;
} }
.imgSeedLabel { .imgSeedLabel {
position: absolute; font-size: 1.3em;
transform: translateX(-100%);
margin-top: 5pt;
margin-left: -5pt;
font-size: 10pt;
background-color: #333;
opacity: 0.8;
color: #ddd;
border-radius: 3pt;
padding: 1pt 3pt;
}
.imgUseBtn {
position: absolute;
transform: translateX(-100%);
margin-top: 30pt;
margin-left: -5pt;
}
.imgSaveBtn {
position: absolute;
transform: translateX(-100%);
margin-top: 55pt;
margin-left: -5pt;
} }
.imgItem { .imgItem {
display: inline; display: inline-block;
padding-right: 10px; width: fit-content;
height: fit-content;
padding: 0.5em;
border: 1px solid rgb(255 255 255 / 25%);
margin-top: 1em;
margin-right: 1em;
} }
.imgItemInfo { .imgItemInfo {
opacity: 0.5; width: 100%;
height: 2em;
padding-bottom: 0.5em;
display: flex;
align-items: center;
justify-content: space-between;
}
.imgInfoLeft {
margin-right: 1em;
} }
#container { #container {
width: 90%; width: 90%;
margin-left: auto; margin-left: auto;

View File

@ -251,18 +251,74 @@ async function healthCheck() {
} }
} }
function makeImageElement(width, height) { function showImages(res, req) {
let imgItem = document.createElement('div') res.output.forEach(result => {
imgItem.className = 'imgItem' if(typeof res != 'object') return;
let img = document.createElement('img') const imageData = result?.data,
img.width = parseInt(width) imageSeed = result?.seed,
img.height = parseInt(height) imageWidth = req.width,
imageHeight = req.height;
imgItem.appendChild(img) // don't continue if data is incorrect
imagesContainer.appendChild(imgItem) if (!(imageData && imageSeed))
{
setStatus('request', 'invalid image', 'error');
console.log(imageData);
return;
}
return imgItem const imageItemElem = document.createElement('div');
imageItemElem.className = 'imgItem';
imageItemElem.innerHTML = `
<div class="imgItemInfo">
<div class="imgInfoLeft">
<span class="imgSeedLabel"></span>
</div>
<div>
<button class="imgUseBtn">Use as Input</button>
<button class="imgSaveBtn">Download</button>
</div>
</div>
<img/>
`;
const imageElem = imageItemElem.querySelector('img'),
imageSeedLabel = imageItemElem.querySelector('.imgSeedLabel'),
useAsInputBtn = imageItemElem.querySelector('.imgUseBtn'),
saveImageBtn = imageItemElem.querySelector('.imgSaveBtn');
imageElem.src = imageData;
imageElem.width = parseInt(imageWidth);
imageElem.height = parseInt(imageHeight);
imageSeedLabel.innerText = imageSeed;
useAsInputBtn.addEventListener('click', () => {
initImageSelector.value = null;
initImagePreview.src = imageData;
initImagePreviewContainer.style.display = 'block';
inpaintingEditorContainer.style.display = 'none';
promptStrengthContainer.style.display = 'block';
maskSetting.checked = false;
// maskSetting.style.display = 'block';
randomSeedField.checked = false;
seedField.value = imageSeed;
seedField.disabled = false;
});
saveImageBtn.addEventListener('click', () => {
const imgDownload = document.createElement('a');
imgDownload.download = createFileName(imageSeed);
imgDownload.href = imageData;
imgDownload.click();
});
imagesContainer.appendChild(imageItemElem);
});
} }
// makes a single image. don't call this directly, use makeImage() instead // makes a single image. don't call this directly, use makeImage() instead
@ -275,14 +331,6 @@ async function doMakeImage(reqBody, batchCount) {
let seed = reqBody['seed'] let seed = reqBody['seed']
let numOutputs = parseInt(reqBody['num_outputs']) let numOutputs = parseInt(reqBody['num_outputs'])
let images = []
function makeImageContainers(numImages) {
for (let i = images.length; i < numImages; i++) {
images.push(makeImageElement(reqBody.width, reqBody.height))
}
}
try { try {
res = await fetch('/image', { res = await fetch('/image', {
method: 'POST', method: 'POST',
@ -411,86 +459,13 @@ async function doMakeImage(reqBody, batchCount) {
res = undefined res = undefined
} }
if (!res) { if (!res) return false;
return false
}
lastPromptUsed = reqBody['prompt'] lastPromptUsed = reqBody['prompt'];
makeImageContainers(res.output.length) showImages(res, reqBody);
for (let idx in res.output) { return true;
let imgBody = ''
let seed = 0
try {
let imgData = res.output[idx]
imgBody = imgData.data
seed = imgData.seed
} catch (e) {
console.log(imgBody)
setStatus('request', 'invalid image', 'error')
continue
}
let imgItem = images[idx]
let img = imgItem.firstChild
img.src = imgBody
let imgItemInfo = document.createElement('span')
imgItemInfo.className = 'imgItemInfo'
let imgSeedLabel = document.createElement('span')
imgSeedLabel.className = 'imgSeedLabel'
imgSeedLabel.innerText = 'Seed: ' + seed
let imgUseBtn = document.createElement('button')
imgUseBtn.className = 'imgUseBtn'
imgUseBtn.innerText = 'Use as Input'
let imgSaveBtn = document.createElement('button')
imgSaveBtn.className = 'imgSaveBtn'
imgSaveBtn.innerText = 'Download'
imgItem.appendChild(imgItemInfo)
imgItemInfo.appendChild(imgSeedLabel)
imgItemInfo.appendChild(imgUseBtn)
imgItemInfo.appendChild(imgSaveBtn)
imgUseBtn.addEventListener('click', function() {
initImageSelector.value = null
initImagePreview.src = imgBody
initImagePreviewContainer.style.display = 'block'
inpaintingEditorContainer.style.display = 'none'
promptStrengthContainer.style.display = 'block'
maskSetting.checked = false
// maskSetting.style.display = 'block'
randomSeedField.checked = false
seedField.value = seed
seedField.disabled = false
})
imgSaveBtn.addEventListener('click', function() {
let imgDownload = document.createElement('a')
imgDownload.download = createFileName();
imgDownload.href = imgBody
imgDownload.click()
})
imgItem.addEventListener('mouseenter', function() {
imgItemInfo.style.opacity = 1
})
imgItem.addEventListener('mouseleave', function() {
imgItemInfo.style.opacity = 0.5
})
}
return true
} }
function validateInput() { function validateInput() {
@ -639,12 +614,11 @@ async function makeImage() {
// create a file name with embedded prompt and metadata // create a file name with embedded prompt and metadata
// for easier cateloging and comparison // for easier cateloging and comparison
function createFileName() { function createFileName(seed) {
// Most important information is the prompt // Most important information is the prompt
let underscoreName = lastPromptUsed.replace(/[^a-zA-Z0-9]/g, '_') let underscoreName = lastPromptUsed.replace(/[^a-zA-Z0-9]/g, '_')
underscoreName = underscoreName.substring(0, 100) underscoreName = underscoreName.substring(0, 100)
const seed = seedField.value
const steps = numInferenceStepsField.value const steps = numInferenceStepsField.value
const guidance = guidanceScaleField.value const guidance = guidanceScaleField.value
@ -1215,4 +1189,4 @@ async function loadModifiers() {
} catch (e) { } catch (e) {
console.log('error fetching modifiers', e) console.log('error fetching modifiers', e)
} }
} }