2022-11-29 14:55:25 -06:00
|
|
|
let blockNewImages = false;
|
2022-12-06 15:28:34 -06:00
|
|
|
let generating = false;
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts progress monitoring bar
|
|
|
|
*
|
|
|
|
* @param {BoundingBox} bb Bouding Box to draw progress to
|
2022-12-06 15:28:34 -06:00
|
|
|
* @param {(data: object) => void} [oncheck] Callback function for when a progress check returns
|
2022-11-29 14:55:25 -06:00
|
|
|
* @returns {() => void}
|
|
|
|
*/
|
2022-12-06 15:28:34 -06:00
|
|
|
const _monitorProgress = (bb, oncheck = null) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
const minDelay = 1000;
|
|
|
|
|
|
|
|
const apiURL = `${host}${url}progress?skip_current_image=true`;
|
|
|
|
|
|
|
|
const expanded = {...bb};
|
|
|
|
expanded.x--;
|
|
|
|
expanded.y--;
|
|
|
|
expanded.w += 2;
|
|
|
|
expanded.h += 2;
|
|
|
|
|
|
|
|
// Get temporary layer to draw progress bar
|
|
|
|
const layer = imageCollection.registerLayer(null, {
|
|
|
|
bb: expanded,
|
|
|
|
});
|
|
|
|
layer.canvas.style.opacity = "70%";
|
|
|
|
|
|
|
|
let running = true;
|
|
|
|
|
|
|
|
const _checkProgress = async () => {
|
|
|
|
const init = performance.now();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch(apiURL);
|
|
|
|
/** @type {StableDiffusionProgressResponse} */
|
|
|
|
const data = await response.json();
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
oncheck && oncheck(data);
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Draw Progress Bar
|
|
|
|
layer.ctx.fillStyle = "#5F5";
|
|
|
|
layer.ctx.fillRect(1, 1, bb.w * data.progress, 10);
|
|
|
|
|
|
|
|
// Draw Progress Text
|
|
|
|
layer.ctx.clearRect(0, 11, expanded.w, 40);
|
|
|
|
layer.ctx.fillStyle = "#FFF";
|
|
|
|
|
|
|
|
layer.ctx.fillRect(0, 15, 60, 25);
|
|
|
|
layer.ctx.fillRect(bb.w - 58, 15, 60, 25);
|
|
|
|
|
|
|
|
layer.ctx.font = "20px Open Sans";
|
|
|
|
layer.ctx.fillStyle = "#000";
|
|
|
|
layer.ctx.textAlign = "right";
|
|
|
|
layer.ctx.fillText(`${Math.round(data.progress * 100)}%`, 55, 35);
|
|
|
|
|
|
|
|
// Draw ETA Text
|
|
|
|
layer.ctx.fillText(`${Math.round(data.eta_relative)}s`, bb.w - 5, 35);
|
|
|
|
} finally {
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeSpent = performance.now() - init;
|
|
|
|
setTimeout(() => {
|
|
|
|
if (running) _checkProgress();
|
|
|
|
}, Math.max(0, minDelay - timeSpent));
|
|
|
|
};
|
|
|
|
|
|
|
|
_checkProgress();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
imageCollection.deleteLayer(layer);
|
|
|
|
running = false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a dream
|
|
|
|
*
|
|
|
|
* @param {"txt2img" | "img2img"} endpoint Endpoint to send the request to
|
|
|
|
* @param {StableDiffusionRequest} request Stable diffusion request
|
|
|
|
* @returns {Promise<string[]>}
|
|
|
|
*/
|
|
|
|
const _dream = async (endpoint, request) => {
|
|
|
|
const apiURL = `${host}${url}${endpoint}`;
|
|
|
|
|
|
|
|
/** @type {StableDiffusionResponse} */
|
|
|
|
let data = null;
|
|
|
|
try {
|
2022-12-06 15:28:34 -06:00
|
|
|
generating = true;
|
2022-11-29 14:55:25 -06:00
|
|
|
const response = await fetch(apiURL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
Accept: "application/json",
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(request),
|
|
|
|
});
|
|
|
|
|
|
|
|
data = await response.json();
|
|
|
|
} finally {
|
2022-12-06 15:28:34 -06:00
|
|
|
generating = false;
|
2022-11-29 14:55:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return data.images;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate and pick an image for placement
|
|
|
|
*
|
|
|
|
* @param {"txt2img" | "img2img"} endpoint Endpoint to send the request to
|
|
|
|
* @param {StableDiffusionRequest} request Stable diffusion request
|
|
|
|
* @param {BoundingBox} bb Generated image placement location
|
2022-12-06 15:28:34 -06:00
|
|
|
* @param {number} [drawEvery=0.2 / request.n_iter] Percentage delta to draw progress at (by default 20% of each iteration)
|
2022-11-29 14:55:25 -06:00
|
|
|
* @returns {Promise<HTMLImageElement | null>}
|
|
|
|
*/
|
2022-12-06 15:28:34 -06:00
|
|
|
const _generate = async (
|
|
|
|
endpoint,
|
|
|
|
request,
|
|
|
|
bb,
|
|
|
|
drawEvery = 0.2 / request.n_iter
|
|
|
|
) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
const requestCopy = {...request};
|
|
|
|
|
|
|
|
// Images to select through
|
|
|
|
let at = 0;
|
|
|
|
/** @type {Image[]} */
|
|
|
|
const images = [];
|
|
|
|
/** @type {HTMLDivElement} */
|
|
|
|
let imageSelectMenu = null;
|
|
|
|
|
|
|
|
// Layer for the images
|
|
|
|
const layer = imageCollection.registerLayer(null, {
|
2022-11-29 16:47:19 -06:00
|
|
|
after: maskPaintLayer,
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
const makeElement = (type, x, y) => {
|
|
|
|
const el = document.createElement(type);
|
|
|
|
el.style.position = "absolute";
|
|
|
|
el.style.left = `${x}px`;
|
|
|
|
el.style.top = `${y}px`;
|
|
|
|
|
|
|
|
// We can use the input element to add interactible html elements in the world
|
|
|
|
imageCollection.inputElement.appendChild(el);
|
|
|
|
|
|
|
|
return el;
|
|
|
|
};
|
|
|
|
|
|
|
|
const redraw = (url = images[at]) => {
|
|
|
|
if (!url) return;
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const image = new Image();
|
2022-12-06 15:28:34 -06:00
|
|
|
image.src = "data:image/png;base64," + url;
|
2022-11-29 14:55:25 -06:00
|
|
|
image.addEventListener("load", () => {
|
|
|
|
layer.ctx.clearRect(0, 0, layer.canvas.width, layer.canvas.height);
|
2022-12-06 15:28:34 -06:00
|
|
|
layer.ctx.drawImage(
|
|
|
|
image,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
image.width,
|
|
|
|
image.height,
|
|
|
|
bb.x,
|
|
|
|
bb.y,
|
|
|
|
bb.w,
|
|
|
|
bb.h
|
|
|
|
);
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
// Add Interrupt Button
|
|
|
|
const interruptButton = makeElement("button", bb.x + bb.w - 100, bb.y + bb.h);
|
|
|
|
interruptButton.classList.add("dream-interrupt-btn");
|
|
|
|
interruptButton.textContent = "Interrupt";
|
|
|
|
interruptButton.addEventListener("click", () => {
|
|
|
|
fetch(`${host}${url}interrupt`, {method: "POST"});
|
|
|
|
interruptButton.disabled = true;
|
|
|
|
});
|
2022-11-29 14:55:25 -06:00
|
|
|
const stopMarchingAnts = march(bb);
|
|
|
|
|
|
|
|
// First Dream Run
|
2022-12-03 06:53:12 -06:00
|
|
|
console.info(`[dream] Generating images for prompt '${request.prompt}'`);
|
|
|
|
console.debug(request);
|
|
|
|
|
2022-12-03 10:20:35 -06:00
|
|
|
let stopProgress = null;
|
|
|
|
try {
|
2022-12-06 15:28:34 -06:00
|
|
|
let stopDrawingStatus = false;
|
|
|
|
let lastProgress = 0;
|
|
|
|
let nextCP = drawEvery;
|
|
|
|
stopProgress = _monitorProgress(bb, (data) => {
|
|
|
|
if (stopDrawingStatus) return;
|
|
|
|
|
|
|
|
if (lastProgress < nextCP && data.progress >= nextCP) {
|
|
|
|
nextCP += drawEvery;
|
|
|
|
fetch(`${host}${url}progress?skip_current_image=false`).then(
|
|
|
|
async (response) => {
|
|
|
|
if (stopDrawingStatus) return;
|
|
|
|
const imagedata = await response.json();
|
|
|
|
redraw(imagedata.current_image);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
lastProgress = data.progress;
|
|
|
|
});
|
|
|
|
|
|
|
|
imageCollection.inputElement.appendChild(interruptButton);
|
2022-12-03 10:20:35 -06:00
|
|
|
images.push(...(await _dream(endpoint, requestCopy)));
|
2022-12-06 15:28:34 -06:00
|
|
|
stopDrawingStatus = true;
|
2022-12-03 10:20:35 -06:00
|
|
|
} catch (e) {
|
|
|
|
alert(
|
|
|
|
`Error generating images. Please try again or see consolde for more details`
|
|
|
|
);
|
|
|
|
console.warn(`[dream] Error generating images:`);
|
|
|
|
console.warn(e);
|
|
|
|
} finally {
|
|
|
|
stopProgress();
|
2022-12-06 15:28:34 -06:00
|
|
|
imageCollection.inputElement.removeChild(interruptButton);
|
2022-12-03 10:20:35 -06:00
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
// Image navigation
|
|
|
|
const prevImg = () => {
|
|
|
|
at--;
|
|
|
|
if (at < 0) at = images.length - 1;
|
|
|
|
|
|
|
|
imageindextxt.textContent = `${at + 1}/${images.length}`;
|
|
|
|
redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
const nextImg = () => {
|
|
|
|
at++;
|
|
|
|
if (at >= images.length) at = 0;
|
|
|
|
|
|
|
|
imageindextxt.textContent = `${at + 1}/${images.length}`;
|
|
|
|
redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
const applyImg = async () => {
|
|
|
|
const img = new Image();
|
|
|
|
// load the image data after defining the closure
|
|
|
|
img.src = "data:image/png;base64," + images[at];
|
|
|
|
img.addEventListener("load", () => {
|
|
|
|
commands.runCommand("drawImage", "Image Dream", {
|
|
|
|
x: bb.x,
|
|
|
|
y: bb.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
w: bb.w,
|
|
|
|
h: bb.h,
|
2022-11-29 18:16:59 -06:00
|
|
|
image: img,
|
|
|
|
});
|
2022-11-30 20:20:57 -06:00
|
|
|
clean(true);
|
2022-11-29 18:16:59 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-30 15:57:40 -06:00
|
|
|
const makeMore = async () => {
|
2022-12-03 10:20:35 -06:00
|
|
|
try {
|
|
|
|
stopProgress = _monitorProgress(bb);
|
2022-12-06 15:28:34 -06:00
|
|
|
interruptButton.disabled = false;
|
|
|
|
imageCollection.inputElement.appendChild(interruptButton);
|
2022-12-03 10:20:35 -06:00
|
|
|
images.push(...(await _dream(endpoint, requestCopy)));
|
|
|
|
imageindextxt.textContent = `${at + 1}/${images.length}`;
|
|
|
|
} catch (e) {
|
|
|
|
alert(
|
|
|
|
`Error generating images. Please try again or see consolde for more details`
|
|
|
|
);
|
|
|
|
console.warn(`[dream] Error generating images:`);
|
|
|
|
console.warn(e);
|
|
|
|
} finally {
|
|
|
|
stopProgress();
|
2022-12-06 15:28:34 -06:00
|
|
|
imageCollection.inputElement.removeChild(interruptButton);
|
2022-12-03 10:20:35 -06:00
|
|
|
}
|
2022-11-30 15:57:40 -06:00
|
|
|
};
|
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
const discardImg = async () => {
|
|
|
|
clean();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Listen for keyboard arrows
|
|
|
|
const onarrow = (evn) => {
|
|
|
|
switch (evn.target.tagName.toLowerCase()) {
|
|
|
|
case "input":
|
|
|
|
case "textarea":
|
|
|
|
case "select":
|
|
|
|
case "button":
|
|
|
|
return; // If in an input field, do not process arrow input
|
|
|
|
default:
|
|
|
|
// Do nothing
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-11-30 15:57:40 -06:00
|
|
|
switch (evn.key) {
|
|
|
|
case "+":
|
|
|
|
makeMore();
|
2022-11-29 18:16:59 -06:00
|
|
|
break;
|
|
|
|
default:
|
2022-11-30 15:57:40 -06:00
|
|
|
switch (evn.code) {
|
|
|
|
case "ArrowRight":
|
|
|
|
nextImg();
|
|
|
|
break;
|
|
|
|
case "ArrowLeft":
|
|
|
|
prevImg();
|
|
|
|
break;
|
2022-11-30 16:18:32 -06:00
|
|
|
case "Enter":
|
2022-11-30 15:57:40 -06:00
|
|
|
applyImg();
|
|
|
|
break;
|
2022-11-30 16:18:32 -06:00
|
|
|
case "Escape":
|
|
|
|
discardImg();
|
|
|
|
break;
|
2022-11-30 15:57:40 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2022-11-29 18:16:59 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
keyboard.listen.onkeyclick.on(onarrow);
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Cleans up
|
2022-11-30 20:20:57 -06:00
|
|
|
const clean = (removeBrushMask = false) => {
|
|
|
|
if (removeBrushMask) {
|
2022-11-30 21:25:06 -06:00
|
|
|
maskPaintCtx.clearRect(bb.x, bb.y, bb.w, bb.h);
|
2022-11-30 20:20:57 -06:00
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
stopMarchingAnts();
|
|
|
|
imageCollection.inputElement.removeChild(imageSelectMenu);
|
|
|
|
imageCollection.deleteLayer(layer);
|
|
|
|
blockNewImages = false;
|
2022-11-29 18:16:59 -06:00
|
|
|
keyboard.listen.onkeyclick.clear(onarrow);
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
|
|
|
|
imageSelectMenu = makeElement("div", bb.x, bb.y + bb.h);
|
|
|
|
|
|
|
|
const imageindextxt = document.createElement("button");
|
|
|
|
imageindextxt.textContent = `${at + 1}/${images.length}`;
|
|
|
|
imageindextxt.addEventListener("click", () => {
|
|
|
|
at = 0;
|
|
|
|
|
|
|
|
imageindextxt.textContent = `${at + 1}/${images.length}`;
|
|
|
|
redraw();
|
|
|
|
});
|
|
|
|
|
|
|
|
const backbtn = document.createElement("button");
|
|
|
|
backbtn.textContent = "<";
|
|
|
|
backbtn.title = "Previous Image";
|
2022-11-29 18:16:59 -06:00
|
|
|
backbtn.addEventListener("click", prevImg);
|
2022-11-29 14:55:25 -06:00
|
|
|
imageSelectMenu.appendChild(backbtn);
|
|
|
|
imageSelectMenu.appendChild(imageindextxt);
|
|
|
|
|
|
|
|
const nextbtn = document.createElement("button");
|
|
|
|
nextbtn.textContent = ">";
|
|
|
|
nextbtn.title = "Next Image";
|
2022-11-29 18:16:59 -06:00
|
|
|
nextbtn.addEventListener("click", nextImg);
|
2022-11-29 14:55:25 -06:00
|
|
|
imageSelectMenu.appendChild(nextbtn);
|
|
|
|
|
|
|
|
const morebtn = document.createElement("button");
|
|
|
|
morebtn.textContent = "+";
|
|
|
|
morebtn.title = "Generate More";
|
2022-11-30 15:57:40 -06:00
|
|
|
morebtn.addEventListener("click", makeMore);
|
2022-11-29 14:55:25 -06:00
|
|
|
imageSelectMenu.appendChild(morebtn);
|
|
|
|
|
|
|
|
const acceptbtn = document.createElement("button");
|
|
|
|
acceptbtn.textContent = "Y";
|
|
|
|
acceptbtn.title = "Apply Current";
|
2022-11-29 18:16:59 -06:00
|
|
|
acceptbtn.addEventListener("click", applyImg);
|
2022-11-29 14:55:25 -06:00
|
|
|
imageSelectMenu.appendChild(acceptbtn);
|
|
|
|
|
|
|
|
const discardbtn = document.createElement("button");
|
|
|
|
discardbtn.textContent = "N";
|
|
|
|
discardbtn.title = "Cancel";
|
2022-11-29 18:16:59 -06:00
|
|
|
discardbtn.addEventListener("click", discardImg);
|
2022-11-29 14:55:25 -06:00
|
|
|
imageSelectMenu.appendChild(discardbtn);
|
|
|
|
|
|
|
|
const resourcebtn = document.createElement("button");
|
|
|
|
resourcebtn.textContent = "R";
|
|
|
|
resourcebtn.title = "Save to Resources";
|
|
|
|
resourcebtn.addEventListener("click", async () => {
|
|
|
|
const img = new Image();
|
|
|
|
// load the image data after defining the closure
|
|
|
|
img.src = "data:image/png;base64," + images[at];
|
|
|
|
img.addEventListener("load", () => {
|
|
|
|
const response = prompt("Enter new resource name", "Dream Resource");
|
2022-11-29 18:16:59 -06:00
|
|
|
if (response) {
|
|
|
|
tools.stamp.state.addResource(response, img);
|
|
|
|
redraw(); // Redraw to avoid strange cursor behavior
|
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
});
|
|
|
|
imageSelectMenu.appendChild(resourcebtn);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for generating a image (dream tool)
|
|
|
|
*
|
|
|
|
* @param {*} evn
|
|
|
|
* @param {*} state
|
|
|
|
*/
|
|
|
|
const dream_generate_callback = async (evn, state) => {
|
|
|
|
if (!blockNewImages) {
|
2022-11-22 16:24:55 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
2022-11-22 16:24:55 -06:00
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
|
|
|
|
// Build request to the API
|
|
|
|
const request = {};
|
|
|
|
Object.assign(request, stableDiffusionData);
|
|
|
|
|
|
|
|
// Load prompt (maybe we should add some events so we don't have to do this)
|
|
|
|
request.prompt = document.getElementById("prompt").value;
|
|
|
|
request.negative_prompt = document.getElementById("negPrompt").value;
|
|
|
|
|
|
|
|
// Don't allow another image until is finished
|
|
|
|
blockNewImages = true;
|
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
// Get visible pixels
|
|
|
|
const visibleCanvas = uil.getVisible(bb);
|
|
|
|
|
2022-11-22 16:24:55 -06:00
|
|
|
// Use txt2img if canvas is blank
|
2022-12-04 13:22:35 -06:00
|
|
|
if (isCanvasBlank(0, 0, bb.w, bb.h, visibleCanvas)) {
|
2022-11-22 16:24:55 -06:00
|
|
|
// Dream
|
2022-11-29 14:55:25 -06:00
|
|
|
_generate("txt2img", request, bb);
|
2022-11-22 16:24:55 -06:00
|
|
|
} else {
|
|
|
|
// Use img2img if not
|
|
|
|
|
|
|
|
// Temporary canvas for init image and mask generation
|
|
|
|
const auxCanvas = document.createElement("canvas");
|
|
|
|
auxCanvas.width = request.width;
|
|
|
|
auxCanvas.height = request.height;
|
|
|
|
const auxCtx = auxCanvas.getContext("2d");
|
|
|
|
|
|
|
|
auxCtx.fillStyle = "#000F";
|
|
|
|
|
|
|
|
// Get init image
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
|
|
|
auxCtx.drawImage(
|
2022-12-04 13:22:35 -06:00
|
|
|
visibleCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-11-22 16:24:55 -06:00
|
|
|
request.init_images = [auxCanvas.toDataURL()];
|
|
|
|
|
|
|
|
// Get mask image
|
2022-11-26 13:34:12 -06:00
|
|
|
auxCtx.fillStyle = "#000F";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
2022-11-26 13:34:12 -06:00
|
|
|
if (state.invertMask) {
|
2022-11-27 13:21:17 -06:00
|
|
|
// overmasking by definition is entirely pointless with an inverted mask outpaint
|
|
|
|
// since it should explicitly avoid brushed masks too, we just won't even bother
|
2022-11-26 13:34:12 -06:00
|
|
|
auxCtx.globalCompositeOperation = "destination-in";
|
|
|
|
auxCtx.drawImage(
|
|
|
|
maskPaintCanvas,
|
|
|
|
bb.x,
|
|
|
|
bb.y,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.width,
|
|
|
|
request.height
|
2022-11-26 13:34:12 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
auxCtx.globalCompositeOperation = "destination-in";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.drawImage(
|
2022-12-04 13:22:35 -06:00
|
|
|
visibleCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-11-26 13:34:12 -06:00
|
|
|
} else {
|
|
|
|
auxCtx.globalCompositeOperation = "destination-in";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.drawImage(
|
2022-12-04 13:22:35 -06:00
|
|
|
visibleCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-11-27 13:24:28 -06:00
|
|
|
// here's where to overmask to avoid including the brushed mask
|
|
|
|
// 99% of my issues were from failing to set source-over for the overmask blotches
|
2022-11-27 13:21:17 -06:00
|
|
|
if (state.overMaskPx > 0) {
|
|
|
|
// transparent to white first
|
|
|
|
auxCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
auxCtx.fillStyle = "#FFFF";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
2022-11-27 13:21:17 -06:00
|
|
|
applyOvermask(auxCanvas, auxCtx, state.overMaskPx);
|
|
|
|
}
|
|
|
|
|
|
|
|
auxCtx.globalCompositeOperation = "destination-out"; // ???
|
2022-11-26 13:34:12 -06:00
|
|
|
auxCtx.drawImage(
|
|
|
|
maskPaintCanvas,
|
|
|
|
bb.x,
|
|
|
|
bb.y,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.width,
|
|
|
|
request.height
|
2022-11-26 13:34:12 -06:00
|
|
|
);
|
|
|
|
}
|
2022-11-22 16:24:55 -06:00
|
|
|
auxCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
auxCtx.fillStyle = "#FFFF";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
2022-11-27 13:21:17 -06:00
|
|
|
request.mask = auxCanvas.toDataURL();
|
2022-11-22 16:24:55 -06:00
|
|
|
// Dream
|
2022-11-29 14:55:25 -06:00
|
|
|
_generate("img2img", request, bb);
|
2022-11-22 16:24:55 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const dream_erase_callback = (evn, state) => {
|
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
2022-11-22 16:24:55 -06:00
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
commands.runCommand("eraseImage", "Erase Area", bb);
|
|
|
|
};
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-11-23 17:40:10 -06:00
|
|
|
function applyOvermask(canvas, ctx, px) {
|
|
|
|
// :badpokerface: look it might be all placebo but i like overmask lol
|
|
|
|
// yes it's crushingly inefficient i knooow :( must fix
|
|
|
|
// https://stackoverflow.com/a/30204783 was instrumental to this working or completely to blame for this disaster depending on your interpretation
|
2022-11-27 13:24:28 -06:00
|
|
|
ctx.globalCompositeOperation = "source-over";
|
2022-11-23 17:40:10 -06:00
|
|
|
var ctxImgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
|
|
|
for (i = 0; i < ctxImgData.data.length; i += 4) {
|
|
|
|
if (ctxImgData.data[i] == 255) {
|
|
|
|
// white pixel?
|
|
|
|
// just blotch all over the thing
|
2022-12-03 06:53:12 -06:00
|
|
|
/**
|
|
|
|
* This should probably have a better randomness profile for the overmasking
|
|
|
|
*
|
|
|
|
* Essentially, we want to have much more smaller values for randomness than big ones,
|
|
|
|
* because big values overshadow smaller circles and kinda ignores their randomness.
|
|
|
|
*
|
|
|
|
* And also, we want the profile to become more extreme the bigger the overmask size,
|
|
|
|
* because bigger px values also make bigger circles ocuppy more horizontal space.
|
|
|
|
*/
|
|
|
|
let lowRandom =
|
|
|
|
Math.atan(Math.random() * 10 - 10) / Math.abs(Math.atan(-10)) + 1;
|
|
|
|
lowRandom = Math.pow(lowRandom, px / 8);
|
|
|
|
|
|
|
|
var rando = Math.floor(lowRandom * px);
|
2022-11-27 13:21:17 -06:00
|
|
|
ctx.beginPath();
|
|
|
|
ctx.arc(
|
|
|
|
(i / 4) % canvas.width,
|
|
|
|
Math.floor(i / 4 / canvas.width),
|
2022-12-03 06:53:12 -06:00
|
|
|
rando, // was 4 * sf + rando, too big, but i think i want it more ... random
|
2022-11-23 17:40:10 -06:00
|
|
|
0,
|
|
|
|
2 * Math.PI,
|
|
|
|
true
|
|
|
|
);
|
2022-11-27 13:21:17 -06:00
|
|
|
ctx.fillStyle = "#FFFF";
|
|
|
|
ctx.fill();
|
2022-11-23 17:40:10 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-22 19:24:04 -06:00
|
|
|
/**
|
|
|
|
* Image to Image
|
|
|
|
*/
|
|
|
|
const dream_img2img_callback = (evn, state) => {
|
2022-11-29 14:55:25 -06:00
|
|
|
if (!blockNewImages) {
|
2022-11-22 19:24:04 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
2022-11-22 19:24:04 -06:00
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
// Get visible pixels
|
|
|
|
const visibleCanvas = uil.getVisible(bb);
|
|
|
|
|
2022-11-22 19:24:04 -06:00
|
|
|
// Do nothing if no image exists
|
2022-12-04 13:22:35 -06:00
|
|
|
if (isCanvasBlank(0, 0, bb.w, bb.h, visibleCanvas)) return;
|
2022-11-22 19:24:04 -06:00
|
|
|
|
|
|
|
// Build request to the API
|
|
|
|
const request = {};
|
|
|
|
Object.assign(request, stableDiffusionData);
|
|
|
|
|
|
|
|
request.denoising_strength = state.denoisingStrength;
|
|
|
|
request.inpainting_fill = 1; // For img2img use original
|
|
|
|
|
|
|
|
// Load prompt (maybe we should add some events so we don't have to do this)
|
|
|
|
request.prompt = document.getElementById("prompt").value;
|
|
|
|
request.negative_prompt = document.getElementById("negPrompt").value;
|
|
|
|
|
|
|
|
// Don't allow another image until is finished
|
|
|
|
blockNewImages = true;
|
|
|
|
|
|
|
|
// Use img2img
|
|
|
|
|
|
|
|
// Temporary canvas for init image and mask generation
|
|
|
|
const auxCanvas = document.createElement("canvas");
|
|
|
|
auxCanvas.width = request.width;
|
|
|
|
auxCanvas.height = request.height;
|
|
|
|
const auxCtx = auxCanvas.getContext("2d");
|
|
|
|
|
|
|
|
auxCtx.fillStyle = "#000F";
|
|
|
|
|
|
|
|
// Get init image
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
|
|
|
auxCtx.drawImage(
|
2022-12-04 13:22:35 -06:00
|
|
|
visibleCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-11-22 19:24:04 -06:00
|
|
|
request.init_images = [auxCanvas.toDataURL()];
|
|
|
|
|
|
|
|
// Get mask image
|
2022-11-26 13:34:12 -06:00
|
|
|
auxCtx.fillStyle = state.invertMask ? "#FFFF" : "#000F";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
2022-11-22 19:24:04 -06:00
|
|
|
auxCtx.globalCompositeOperation = "destination-out";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.drawImage(
|
|
|
|
maskPaintCanvas,
|
|
|
|
bb.x,
|
|
|
|
bb.y,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-11-26 13:34:12 -06:00
|
|
|
auxCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
auxCtx.fillStyle = state.invertMask ? "#000F" : "#FFFF";
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, request.height);
|
2022-11-26 13:34:12 -06:00
|
|
|
|
2022-11-22 19:24:04 -06:00
|
|
|
// Border Mask
|
2022-11-26 13:34:12 -06:00
|
|
|
if (state.keepBorderSize > 0) {
|
|
|
|
auxCtx.globalCompositeOperation = "source-over";
|
2022-11-22 19:24:04 -06:00
|
|
|
auxCtx.fillStyle = "#000F";
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const lg = auxCtx.createLinearGradient(0, 0, state.keepBorderSize, 0);
|
|
|
|
lg.addColorStop(0, "#000F");
|
|
|
|
lg.addColorStop(1, "#0000");
|
|
|
|
auxCtx.fillStyle = lg;
|
|
|
|
}
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, state.keepBorderSize, request.height);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const tg = auxCtx.createLinearGradient(0, 0, 0, state.keepBorderSize);
|
|
|
|
tg.addColorStop(0, "#000F");
|
|
|
|
tg.addColorStop(1, "#0000");
|
|
|
|
auxCtx.fillStyle = tg;
|
|
|
|
}
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, state.keepBorderSize);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const rg = auxCtx.createLinearGradient(
|
|
|
|
request.width,
|
|
|
|
0,
|
|
|
|
request.width - state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
rg.addColorStop(0, "#000F");
|
|
|
|
rg.addColorStop(1, "#0000");
|
|
|
|
auxCtx.fillStyle = rg;
|
|
|
|
}
|
2022-11-22 19:24:04 -06:00
|
|
|
auxCtx.fillRect(
|
2022-12-03 06:53:12 -06:00
|
|
|
request.width - state.keepBorderSize,
|
2022-11-22 19:24:04 -06:00
|
|
|
0,
|
2022-11-26 13:34:12 -06:00
|
|
|
state.keepBorderSize,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.height
|
2022-11-22 19:24:04 -06:00
|
|
|
);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const bg = auxCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
request.height,
|
|
|
|
0,
|
|
|
|
request.height - state.keepBorderSize
|
|
|
|
);
|
|
|
|
bg.addColorStop(0, "#000F");
|
|
|
|
bg.addColorStop(1, "#0000");
|
|
|
|
auxCtx.fillStyle = bg;
|
|
|
|
}
|
2022-11-22 19:24:04 -06:00
|
|
|
auxCtx.fillRect(
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.height - state.keepBorderSize,
|
|
|
|
request.width,
|
2022-11-26 13:34:12 -06:00
|
|
|
state.keepBorderSize
|
2022-11-22 19:24:04 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
request.mask = auxCanvas.toDataURL();
|
2022-11-26 13:59:24 -06:00
|
|
|
request.inpaint_full_res = state.fullResolution;
|
2022-11-22 19:24:04 -06:00
|
|
|
|
|
|
|
// Dream
|
2022-11-29 14:55:25 -06:00
|
|
|
_generate("img2img", request, bb);
|
2022-11-22 19:24:04 -06:00
|
|
|
}
|
|
|
|
};
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
/**
|
|
|
|
* Dream and img2img tools
|
|
|
|
*/
|
2022-12-06 17:08:55 -06:00
|
|
|
const _reticle_draw = (evn, state, tool, style = {}) => {
|
|
|
|
defaultOpt(style, {
|
|
|
|
sizeTextStyle: "#FFF5",
|
|
|
|
toolTextStyle: "#FFF5",
|
|
|
|
reticleWidth: 1,
|
|
|
|
reticleStyle: "#FFF",
|
|
|
|
});
|
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
2022-12-01 15:05:14 -06:00
|
|
|
);
|
2022-12-04 13:22:35 -06:00
|
|
|
const bbvp = {
|
|
|
|
...viewport.canvasToView(bb.x, bb.y),
|
|
|
|
w: viewport.zoom * bb.w,
|
|
|
|
h: viewport.zoom * bb.h,
|
|
|
|
};
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
uiCtx.save();
|
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
// draw targeting square reticle thingy cursor
|
2022-12-06 17:08:55 -06:00
|
|
|
uiCtx.lineWidth = style.reticleWidth;
|
|
|
|
uiCtx.strokeStyle = style.reticleStyle;
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.strokeRect(bbvp.x, bbvp.y, bbvp.w, bbvp.h); //origin is middle of the frame
|
2022-12-01 15:05:14 -06:00
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
uiCtx.font = `bold 20px Open Sans`;
|
|
|
|
|
2022-12-06 17:00:50 -06:00
|
|
|
// Draw Tool Name
|
|
|
|
{
|
|
|
|
const xshrink = Math.min(1, (bbvp.w - 20) / uiCtx.measureText(tool).width);
|
|
|
|
|
|
|
|
uiCtx.font = `bold ${20 * xshrink}px Open Sans`;
|
|
|
|
|
|
|
|
uiCtx.textAlign = "left";
|
2022-12-06 17:08:55 -06:00
|
|
|
uiCtx.fillStyle = style.toolTextStyle;
|
2022-12-06 20:09:30 -06:00
|
|
|
uiCtx.fillText(
|
|
|
|
tool,
|
|
|
|
bbvp.x + 10,
|
|
|
|
bbvp.y + 10 + 20 * xshrink,
|
|
|
|
state.cursorSize
|
|
|
|
);
|
2022-12-06 17:00:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw width and height
|
|
|
|
{
|
|
|
|
uiCtx.textAlign = "center";
|
2022-12-06 17:08:55 -06:00
|
|
|
uiCtx.fillStyle = style.sizeTextStyle;
|
2022-12-06 17:00:50 -06:00
|
|
|
uiCtx.translate(bbvp.x + bbvp.w / 2, bbvp.y + bbvp.h / 2);
|
|
|
|
const xshrink = Math.min(
|
|
|
|
1,
|
|
|
|
(bbvp.w - 30) / uiCtx.measureText(`${state.cursorSize}px`).width
|
|
|
|
);
|
|
|
|
const yshrink = Math.min(
|
|
|
|
1,
|
|
|
|
(bbvp.h - 30) / uiCtx.measureText(`${state.cursorSize}px`).width
|
|
|
|
);
|
|
|
|
uiCtx.font = `bold ${20 * xshrink}px Open Sans`;
|
|
|
|
uiCtx.fillText(
|
|
|
|
`${state.cursorSize}px`,
|
|
|
|
0,
|
|
|
|
bbvp.h / 2 - 10 * xshrink,
|
|
|
|
state.cursorSize
|
|
|
|
);
|
|
|
|
uiCtx.rotate(-Math.PI / 2);
|
|
|
|
uiCtx.font = `bold ${20 * yshrink}px Open Sans`;
|
|
|
|
uiCtx.fillText(
|
|
|
|
`${state.cursorSize}px`,
|
|
|
|
0,
|
|
|
|
bbvp.h / 2 - 10 * yshrink,
|
|
|
|
state.cursorSize
|
|
|
|
);
|
|
|
|
|
|
|
|
uiCtx.restore();
|
|
|
|
}
|
2022-12-06 15:28:34 -06:00
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
return () => {
|
2022-12-06 15:28:34 -06:00
|
|
|
uiCtx.save();
|
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.clearRect(bbvp.x - 10, bbvp.y - 10, bbvp.w + 20, bbvp.h + 20);
|
2022-12-06 15:28:34 -06:00
|
|
|
|
|
|
|
uiCtx.restore();
|
2022-12-01 15:05:14 -06:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
/**
|
|
|
|
* Generic wheel handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _dream_onwheel = (evn, state) => {
|
|
|
|
if (!evn.evn.ctrlKey) {
|
|
|
|
const v =
|
|
|
|
state.cursorSize -
|
|
|
|
Math.floor(state.config.cursorSizeScrollSpeed * evn.delta);
|
|
|
|
state.cursorSize = state.setCursorSize(v + snap(v, 0, 128));
|
|
|
|
state.mousemovecb(evn);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
/**
|
|
|
|
* Registers Tools
|
|
|
|
*/
|
|
|
|
const dreamTool = () =>
|
|
|
|
toolbar.registerTool(
|
|
|
|
"res/icons/image-plus.svg",
|
|
|
|
"Dream",
|
|
|
|
(state, opt) => {
|
|
|
|
// Draw new cursor immediately
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = {
|
2022-11-29 14:55:25 -06:00
|
|
|
...mouse.coords.world.pos,
|
2022-12-06 17:00:50 -06:00
|
|
|
};
|
|
|
|
state.redraw();
|
2022-11-24 09:30:13 -06:00
|
|
|
|
|
|
|
// Start Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.mousemovecb);
|
2022-12-03 06:53:12 -06:00
|
|
|
mouse.listen.world.onwheel.on(state.wheelcb);
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.on(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.erasecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
|
|
|
// Display Mask
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
(state, opt) => {
|
|
|
|
// Clear Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.mousemovecb);
|
2022-12-03 06:53:12 -06:00
|
|
|
mouse.listen.world.onwheel.clear(state.wheelcb);
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.erasecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
|
|
|
// Hide Mask
|
|
|
|
setMask("none");
|
2022-12-04 13:22:35 -06:00
|
|
|
|
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
init: (state) => {
|
2022-12-03 06:53:12 -06:00
|
|
|
state.config = {
|
|
|
|
cursorSizeScrollSpeed: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
state.cursorSize = 512;
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
state.snapToGrid = true;
|
2022-11-26 13:34:12 -06:00
|
|
|
state.invertMask = false;
|
2022-11-24 09:30:13 -06:00
|
|
|
state.overMaskPx = 0;
|
2022-12-01 15:05:14 -06:00
|
|
|
|
|
|
|
state.erasePrevReticle = () =>
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-12-01 15:05:14 -06:00
|
|
|
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = {
|
2022-12-06 15:28:34 -06:00
|
|
|
...mouse.coords.world.pos,
|
|
|
|
};
|
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
state.mousemovecb = (evn) => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = evn;
|
2022-12-01 15:05:14 -06:00
|
|
|
state.erasePrevReticle();
|
2022-12-06 15:28:34 -06:00
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
2022-12-06 17:08:55 -06:00
|
|
|
state.erasePrevReticle = _reticle_draw(evn, state, "Dream", {
|
|
|
|
sizeTextStyle: style,
|
|
|
|
});
|
2022-12-06 15:28:34 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
state.redraw = () => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.mousemovecb(state.lastMouseMove);
|
2022-12-03 06:53:12 -06:00
|
|
|
};
|
2022-12-06 15:28:34 -06:00
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
state.wheelcb = (evn) => {
|
|
|
|
_dream_onwheel(evn, state);
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
2022-11-24 09:30:13 -06:00
|
|
|
state.dreamcb = (evn) => {
|
|
|
|
dream_generate_callback(evn, state);
|
|
|
|
};
|
|
|
|
state.erasecb = (evn) => dream_erase_callback(evn, state);
|
|
|
|
},
|
|
|
|
populateContextMenu: (menu, state) => {
|
|
|
|
if (!state.ctxmenu) {
|
|
|
|
state.ctxmenu = {};
|
2022-11-26 13:34:12 -06:00
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
// Cursor Size Slider
|
|
|
|
const cursorSizeSlider = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"cursorSize",
|
|
|
|
"Cursor Size",
|
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 2048,
|
|
|
|
step: 128,
|
|
|
|
textStep: 2,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
state.setCursorSize = cursorSizeSlider.setValue;
|
|
|
|
state.ctxmenu.cursorSizeSlider = cursorSizeSlider.slider;
|
|
|
|
|
2022-11-26 13:34:12 -06:00
|
|
|
// Snap to Grid Checkbox
|
2022-11-24 09:30:13 -06:00
|
|
|
state.ctxmenu.snapToGridLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"snapToGrid",
|
|
|
|
"Snap To Grid"
|
|
|
|
).label;
|
2022-11-26 13:34:12 -06:00
|
|
|
|
|
|
|
// Invert Mask Checkbox
|
|
|
|
state.ctxmenu.invertMaskLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"invertMask",
|
2022-11-26 19:35:16 -06:00
|
|
|
"Invert Mask",
|
|
|
|
() => {
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
|
|
|
}
|
2022-11-26 13:34:12 -06:00
|
|
|
).label;
|
|
|
|
|
|
|
|
// Overmasking Slider
|
2022-11-24 09:30:13 -06:00
|
|
|
state.ctxmenu.overMaskPxLabel = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"overMaskPx",
|
|
|
|
"Overmask px",
|
2022-12-03 04:50:23 -06:00
|
|
|
{
|
|
|
|
min: 0,
|
2022-12-03 06:53:12 -06:00
|
|
|
max: 64,
|
|
|
|
step: 5,
|
|
|
|
textStep: 1,
|
2022-12-03 04:50:23 -06:00
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
).slider;
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
menu.appendChild(state.ctxmenu.cursorSizeSlider);
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.snapToGridLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-26 13:34:12 -06:00
|
|
|
menu.appendChild(state.ctxmenu.invertMaskLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.overMaskPxLabel);
|
|
|
|
},
|
|
|
|
shortcut: "D",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const img2imgTool = () =>
|
|
|
|
toolbar.registerTool(
|
|
|
|
"res/icons/image.svg",
|
|
|
|
"Img2Img",
|
|
|
|
(state, opt) => {
|
|
|
|
// Draw new cursor immediately
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = {
|
2022-11-29 14:55:25 -06:00
|
|
|
...mouse.coords.world.pos,
|
2022-12-06 17:00:50 -06:00
|
|
|
};
|
|
|
|
state.redraw();
|
2022-11-24 09:30:13 -06:00
|
|
|
|
|
|
|
// Start Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.mousemovecb);
|
2022-12-03 06:53:12 -06:00
|
|
|
mouse.listen.world.onwheel.on(state.wheelcb);
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.on(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.erasecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
|
|
|
// Display Mask
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
(state, opt) => {
|
|
|
|
// Clear Listeners
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.mousemovecb);
|
2022-12-03 06:53:12 -06:00
|
|
|
mouse.listen.world.onwheel.clear(state.wheelcb);
|
2022-11-29 14:55:25 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.erasecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
|
|
|
// Hide mask
|
|
|
|
setMask("none");
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-11-24 09:30:13 -06:00
|
|
|
},
|
|
|
|
{
|
|
|
|
init: (state) => {
|
2022-12-03 06:53:12 -06:00
|
|
|
state.config = {
|
|
|
|
cursorSizeScrollSpeed: 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
state.cursorSize = 512;
|
2022-11-24 09:30:13 -06:00
|
|
|
state.snapToGrid = true;
|
2022-11-26 13:34:12 -06:00
|
|
|
state.invertMask = true;
|
2022-11-26 13:59:24 -06:00
|
|
|
state.fullResolution = false;
|
2022-11-26 13:34:12 -06:00
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
state.denoisingStrength = 0.7;
|
|
|
|
|
2022-11-26 13:34:12 -06:00
|
|
|
state.keepBorderSize = 64;
|
2022-12-04 15:57:26 -06:00
|
|
|
state.gradient = true;
|
2022-11-24 09:30:13 -06:00
|
|
|
|
2022-12-01 15:05:14 -06:00
|
|
|
state.erasePrevReticle = () =>
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
2022-12-01 15:05:14 -06:00
|
|
|
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = {
|
2022-12-06 15:28:34 -06:00
|
|
|
...mouse.coords.world.pos,
|
|
|
|
};
|
2022-12-01 15:05:14 -06:00
|
|
|
state.mousemovecb = (evn) => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = evn;
|
2022-12-01 15:05:14 -06:00
|
|
|
state.erasePrevReticle();
|
2022-12-06 17:08:55 -06:00
|
|
|
|
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
|
|
|
state.erasePrevReticle = _reticle_draw(evn, state, "Img2Img", {
|
|
|
|
sizeTextStyle: style,
|
|
|
|
});
|
2022-12-06 17:00:50 -06:00
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
2022-12-03 06:53:12 -06:00
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
2022-11-29 14:55:25 -06:00
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
// Resolution
|
|
|
|
const request = {
|
|
|
|
width: stableDiffusionData.width,
|
|
|
|
height: stableDiffusionData.height,
|
|
|
|
};
|
|
|
|
|
2022-12-04 13:22:35 -06:00
|
|
|
const bbvp = {
|
|
|
|
...viewport.canvasToView(bb.x, bb.y),
|
|
|
|
w: viewport.zoom * bb.w,
|
|
|
|
h: viewport.zoom * bb.h,
|
|
|
|
};
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// For displaying border mask
|
|
|
|
const auxCanvas = document.createElement("canvas");
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCanvas.width = request.width;
|
|
|
|
auxCanvas.height = request.height;
|
2022-11-29 14:55:25 -06:00
|
|
|
const auxCtx = auxCanvas.getContext("2d");
|
|
|
|
|
|
|
|
if (state.keepBorderSize > 0) {
|
2022-12-01 15:05:14 -06:00
|
|
|
auxCtx.fillStyle = "#6A6AFF30";
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const lg = auxCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
lg.addColorStop(0, "#6A6AFF30");
|
|
|
|
lg.addColorStop(1, "#0000");
|
|
|
|
auxCtx.fillStyle = lg;
|
|
|
|
}
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, state.keepBorderSize, request.height);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const tg = auxCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize
|
|
|
|
);
|
|
|
|
tg.addColorStop(0, "#6A6AFF30");
|
|
|
|
tg.addColorStop(1, "#6A6AFF00");
|
|
|
|
auxCtx.fillStyle = tg;
|
|
|
|
}
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCtx.fillRect(0, 0, request.width, state.keepBorderSize);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const rg = auxCtx.createLinearGradient(
|
|
|
|
request.width,
|
|
|
|
0,
|
|
|
|
request.width - state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
rg.addColorStop(0, "#6A6AFF30");
|
|
|
|
rg.addColorStop(1, "#6A6AFF00");
|
|
|
|
auxCtx.fillStyle = rg;
|
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
auxCtx.fillRect(
|
2022-12-03 06:53:12 -06:00
|
|
|
request.width - state.keepBorderSize,
|
2022-11-29 14:55:25 -06:00
|
|
|
0,
|
|
|
|
state.keepBorderSize,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.height
|
2022-11-24 09:30:13 -06:00
|
|
|
);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
|
|
|
const bg = auxCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
request.height,
|
|
|
|
0,
|
|
|
|
request.height - state.keepBorderSize
|
|
|
|
);
|
|
|
|
bg.addColorStop(0, "#6A6AFF30");
|
|
|
|
bg.addColorStop(1, "#6A6AFF00");
|
|
|
|
auxCtx.fillStyle = bg;
|
|
|
|
}
|
2022-11-29 14:55:25 -06:00
|
|
|
auxCtx.fillRect(
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.height - state.keepBorderSize,
|
|
|
|
request.width,
|
2022-11-29 14:55:25 -06:00
|
|
|
state.keepBorderSize
|
|
|
|
);
|
2022-12-04 13:22:35 -06:00
|
|
|
uiCtx.drawImage(
|
2022-12-03 06:53:12 -06:00
|
|
|
auxCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height,
|
2022-12-04 13:22:35 -06:00
|
|
|
bbvp.x,
|
|
|
|
bbvp.y,
|
|
|
|
bbvp.w,
|
|
|
|
bbvp.h
|
2022-12-03 06:53:12 -06:00
|
|
|
);
|
2022-11-25 16:39:38 -06:00
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
2022-12-06 15:28:34 -06:00
|
|
|
|
|
|
|
state.redraw = () => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.mousemovecb(state.lastMouseMove);
|
2022-12-06 15:28:34 -06:00
|
|
|
};
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
state.wheelcb = (evn) => {
|
|
|
|
_dream_onwheel(evn, state);
|
|
|
|
};
|
2022-11-24 09:30:13 -06:00
|
|
|
state.dreamcb = (evn) => {
|
|
|
|
dream_img2img_callback(evn, state);
|
|
|
|
};
|
|
|
|
state.erasecb = (evn) => dream_erase_callback(evn, state);
|
|
|
|
},
|
|
|
|
populateContextMenu: (menu, state) => {
|
|
|
|
if (!state.ctxmenu) {
|
|
|
|
state.ctxmenu = {};
|
2022-12-03 06:53:12 -06:00
|
|
|
|
|
|
|
// Cursor Size Slider
|
|
|
|
const cursorSizeSlider = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"cursorSize",
|
|
|
|
"Cursor Size",
|
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 2048,
|
|
|
|
step: 128,
|
|
|
|
textStep: 2,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
state.setCursorSize = cursorSizeSlider.setValue;
|
|
|
|
state.ctxmenu.cursorSizeSlider = cursorSizeSlider.slider;
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
// Snap To Grid Checkbox
|
|
|
|
state.ctxmenu.snapToGridLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"snapToGrid",
|
|
|
|
"Snap To Grid"
|
|
|
|
).label;
|
|
|
|
|
2022-11-26 13:34:12 -06:00
|
|
|
// Invert Mask Checkbox
|
|
|
|
state.ctxmenu.invertMaskLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"invertMask",
|
2022-11-26 19:35:16 -06:00
|
|
|
"Invert Mask",
|
|
|
|
() => {
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
|
|
|
}
|
2022-11-26 13:34:12 -06:00
|
|
|
).label;
|
|
|
|
|
2022-11-26 13:59:24 -06:00
|
|
|
// Inpaint Full Resolution Checkbox
|
|
|
|
state.ctxmenu.fullResolutionLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"fullResolution",
|
|
|
|
"Inpaint Full Resolution"
|
|
|
|
).label;
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
// Denoising Strength Slider
|
|
|
|
state.ctxmenu.denoisingStrengthSlider = _toolbar_input.slider(
|
|
|
|
state,
|
|
|
|
"denoisingStrength",
|
|
|
|
"Denoising Strength",
|
2022-12-03 04:50:23 -06:00
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 1,
|
|
|
|
step: 0.05,
|
|
|
|
textStep: 0.01,
|
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
).slider;
|
|
|
|
|
2022-12-04 15:57:26 -06:00
|
|
|
// Border Mask Gradient Checkbox
|
|
|
|
state.ctxmenu.borderMaskGradientCheckbox = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"gradient",
|
|
|
|
"Border Mask Gradient"
|
|
|
|
).label;
|
|
|
|
|
2022-11-24 09:30:13 -06:00
|
|
|
// Border Mask Size Slider
|
|
|
|
state.ctxmenu.borderMaskSlider = _toolbar_input.slider(
|
|
|
|
state,
|
2022-11-26 13:34:12 -06:00
|
|
|
"keepBorderSize",
|
|
|
|
"Keep Border Size",
|
2022-12-03 04:50:23 -06:00
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 128,
|
|
|
|
step: 8,
|
|
|
|
textStep: 1,
|
|
|
|
}
|
2022-11-24 09:30:13 -06:00
|
|
|
).slider;
|
|
|
|
}
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
menu.appendChild(state.ctxmenu.cursorSizeSlider);
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.snapToGridLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-26 13:34:12 -06:00
|
|
|
menu.appendChild(state.ctxmenu.invertMaskLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-26 13:59:24 -06:00
|
|
|
menu.appendChild(state.ctxmenu.fullResolutionLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.denoisingStrengthSlider);
|
2022-12-04 15:57:26 -06:00
|
|
|
menu.appendChild(state.ctxmenu.borderMaskGradientCheckbox);
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.borderMaskSlider);
|
|
|
|
},
|
|
|
|
shortcut: "I",
|
|
|
|
}
|
|
|
|
);
|
2022-12-06 15:28:34 -06:00
|
|
|
|
|
|
|
window.onbeforeunload = async () => {
|
|
|
|
// Stop current generation on page close
|
|
|
|
if (generating) await fetch(`${host}${url}interrupt`, {method: "POST"});
|
|
|
|
};
|