2022-11-29 14:55:25 -06:00
|
|
|
let blockNewImages = false;
|
2022-12-11 21:50:25 -06:00
|
|
|
let generationQueue = [];
|
|
|
|
let generationAreas = new Set();
|
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,
|
2022-12-21 09:07:29 -06:00
|
|
|
category: "display",
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
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-12-17 09:35:26 -06:00
|
|
|
layer.clear();
|
|
|
|
|
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.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}`;
|
|
|
|
|
2022-12-24 08:56:30 -06:00
|
|
|
// Debugging is enabled
|
|
|
|
if (global.debug) {
|
|
|
|
// Run in parallel
|
|
|
|
(async () => {
|
|
|
|
// Create canvas
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = request.width;
|
|
|
|
canvas.height = request.height * (request.init_images.length + 1);
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
|
|
|
// Load images and draw to canvas
|
|
|
|
for (let i = 0; i < request.init_images.length; i++) {
|
|
|
|
try {
|
|
|
|
const image = document.createElement("img");
|
|
|
|
image.src = request.init_images[i];
|
|
|
|
await image.decode();
|
|
|
|
|
|
|
|
ctx.drawImage(image, 0, i * request.height);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load mask and draw to canvas
|
|
|
|
if (request.mask) {
|
|
|
|
try {
|
|
|
|
const mask = document.createElement("img");
|
|
|
|
mask.src = request.mask;
|
|
|
|
await mask.decode();
|
|
|
|
|
|
|
|
ctx.drawImage(mask, 0, canvas.height - request.height);
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
downloadCanvas({
|
|
|
|
canvas,
|
|
|
|
cropToContent: false,
|
|
|
|
filename: `openOutpaint_debug_${new Date()}.png`,
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
/** @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
|
|
|
}
|
2022-12-13 19:30:40 -06:00
|
|
|
var responseSubdata = JSON.parse(data.info);
|
2022-12-15 09:25:20 -06:00
|
|
|
var returnData = {
|
|
|
|
images: data.images,
|
|
|
|
seeds: responseSubdata.all_seeds,
|
|
|
|
};
|
|
|
|
return returnData;
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-13 21:16:48 -06:00
|
|
|
* @param {object} options Options
|
|
|
|
* @param {number} [options.drawEvery=0.2 / request.n_iter] Percentage delta to draw progress at (by default 20% of each iteration)
|
2022-12-16 19:59:30 -06:00
|
|
|
* @param {HTMLCanvasElement} [options.keepUnmask=null] Whether to force keep image under fully opaque mask
|
|
|
|
* @param {number} [options.keepUnmaskBlur=0] Blur when applying full resolution back to the image
|
2022-11-29 14:55:25 -06:00
|
|
|
* @returns {Promise<HTMLImageElement | null>}
|
|
|
|
*/
|
2022-12-13 21:16:48 -06:00
|
|
|
const _generate = async (endpoint, request, bb, options = {}) => {
|
|
|
|
defaultOpt(options, {
|
|
|
|
drawEvery: 0.2 / request.n_iter,
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmask: null,
|
|
|
|
keepUnmaskBlur: 0,
|
2022-12-13 21:16:48 -06:00
|
|
|
});
|
|
|
|
|
2022-12-12 15:25:32 -06:00
|
|
|
events.tool.dream.emit({event: "generate", request});
|
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
const requestCopy = JSON.parse(JSON.stringify(request));
|
|
|
|
|
|
|
|
// Block requests to identical areas
|
|
|
|
const areaid = `${bb.x}-${bb.y}-${bb.w}-${bb.h}`;
|
|
|
|
if (generationAreas.has(areaid)) return;
|
|
|
|
generationAreas.add(areaid);
|
|
|
|
|
|
|
|
// Await for queue
|
2022-12-12 15:49:53 -06:00
|
|
|
let cancelled = false;
|
2022-12-11 21:50:25 -06:00
|
|
|
const waitQueue = async () => {
|
|
|
|
const stopQueueMarchingAnts = march(bb, {style: "#AAF"});
|
|
|
|
|
2022-12-12 15:49:53 -06:00
|
|
|
// Add cancel Button
|
|
|
|
const cancelButton = makeElement("button", bb.x + bb.w - 100, bb.y + bb.h);
|
|
|
|
cancelButton.classList.add("dream-stop-btn");
|
|
|
|
cancelButton.textContent = "Cancel";
|
|
|
|
cancelButton.addEventListener("click", () => {
|
|
|
|
cancelled = true;
|
|
|
|
imageCollection.inputElement.removeChild(cancelButton);
|
|
|
|
stopQueueMarchingAnts();
|
|
|
|
});
|
|
|
|
imageCollection.inputElement.appendChild(cancelButton);
|
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
let qPromise = null;
|
|
|
|
let qResolve = null;
|
|
|
|
await new Promise((finish) => {
|
|
|
|
// Will be this request's (kind of) semaphore
|
|
|
|
qPromise = new Promise((r) => (qResolve = r));
|
|
|
|
generationQueue.push(qPromise);
|
|
|
|
|
|
|
|
// Wait for last generation to end
|
|
|
|
if (generationQueue.length > 1) {
|
|
|
|
(async () => {
|
|
|
|
await generationQueue[generationQueue.length - 2];
|
|
|
|
finish();
|
|
|
|
})();
|
|
|
|
} else {
|
|
|
|
// If this is the first, just continue
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
});
|
2022-12-12 15:49:53 -06:00
|
|
|
if (!cancelled) {
|
|
|
|
imageCollection.inputElement.removeChild(cancelButton);
|
|
|
|
stopQueueMarchingAnts();
|
|
|
|
}
|
2022-12-11 21:50:25 -06:00
|
|
|
|
|
|
|
return {promise: qPromise, resolve: qResolve};
|
|
|
|
};
|
|
|
|
|
|
|
|
const nextQueue = (queueEntry) => {
|
|
|
|
const generationIndex = generationQueue.findIndex(
|
|
|
|
(v) => v === queueEntry.promise
|
|
|
|
);
|
|
|
|
generationQueue.splice(generationIndex, 1);
|
|
|
|
queueEntry.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
const initialQ = await waitQueue();
|
2022-11-29 14:55:25 -06:00
|
|
|
|
2022-12-12 15:49:53 -06:00
|
|
|
if (cancelled) {
|
|
|
|
nextQueue(initialQ);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
// Save masked content
|
2022-12-16 19:59:30 -06:00
|
|
|
let keepUnmaskCanvas = null;
|
|
|
|
let keepUnmaskCtx = null;
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
if (options.keepUnmask) {
|
2022-12-15 08:51:18 -06:00
|
|
|
const visibleCanvas = uil.getVisible({
|
2022-12-16 19:59:30 -06:00
|
|
|
x: bb.x - options.keepUnmaskBlur,
|
|
|
|
y: bb.y - options.keepUnmaskBlur,
|
|
|
|
w: bb.w + 2 * options.keepUnmaskBlur,
|
|
|
|
h: bb.h + 2 * options.keepUnmaskBlur,
|
2022-12-15 08:51:18 -06:00
|
|
|
});
|
|
|
|
const visibleCtx = visibleCanvas.getContext("2d");
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
const ctx = options.keepUnmask.getContext("2d", {willReadFrequently: true});
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
// Save current image
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmaskCanvas = document.createElement("canvas");
|
|
|
|
keepUnmaskCanvas.width = options.keepUnmask.width;
|
|
|
|
keepUnmaskCanvas.height = options.keepUnmask.height;
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmaskCtx = keepUnmaskCanvas.getContext("2d", {
|
|
|
|
willReadFrequently: true,
|
|
|
|
});
|
2022-12-13 21:16:48 -06:00
|
|
|
|
|
|
|
if (
|
2022-12-16 19:59:30 -06:00
|
|
|
visibleCanvas.width !==
|
|
|
|
keepUnmaskCanvas.width + 2 * options.keepUnmaskBlur ||
|
|
|
|
visibleCanvas.height !==
|
|
|
|
keepUnmaskCanvas.height + 2 * options.keepUnmaskBlur
|
2022-12-13 21:16:48 -06:00
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
"[dream] Provided mask is not the same size as the bounding box"
|
|
|
|
);
|
|
|
|
}
|
2022-12-15 08:51:18 -06:00
|
|
|
|
|
|
|
// Cut out changing elements
|
|
|
|
const blurMaskCanvas = document.createElement("canvas");
|
|
|
|
// A bit bigger to handle literal corner cases
|
2022-12-16 19:59:30 -06:00
|
|
|
blurMaskCanvas.width = bb.w + options.keepUnmaskBlur * 2;
|
|
|
|
blurMaskCanvas.height = bb.h + options.keepUnmaskBlur * 2;
|
2022-12-15 08:51:18 -06:00
|
|
|
const blurMaskCtx = blurMaskCanvas.getContext("2d");
|
|
|
|
|
|
|
|
const blurMaskData = blurMaskCtx.getImageData(
|
2022-12-16 19:59:30 -06:00
|
|
|
options.keepUnmaskBlur,
|
|
|
|
options.keepUnmaskBlur,
|
|
|
|
keepUnmaskCanvas.width,
|
|
|
|
keepUnmaskCanvas.height
|
2022-12-13 21:16:48 -06:00
|
|
|
);
|
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
const image = blurMaskData.data;
|
2022-12-13 21:16:48 -06:00
|
|
|
|
|
|
|
const maskData = ctx.getImageData(
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-16 19:59:30 -06:00
|
|
|
options.keepUnmask.width,
|
|
|
|
options.keepUnmask.height
|
2022-12-13 21:16:48 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
const mask = maskData.data;
|
|
|
|
|
|
|
|
for (let i = 0; i < mask.length; i += 4) {
|
|
|
|
if (mask[i] !== 0 || mask[i + 1] !== 0 || mask[i + 2] !== 0) {
|
2022-12-15 08:51:18 -06:00
|
|
|
// If pixel is fully black
|
|
|
|
// Set pixel as fully black here as well
|
2022-12-13 21:16:48 -06:00
|
|
|
image[i] = 0;
|
|
|
|
image[i + 1] = 0;
|
|
|
|
image[i + 2] = 0;
|
2022-12-15 08:51:18 -06:00
|
|
|
image[i + 3] = 255;
|
2022-12-13 21:16:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
blurMaskCtx.putImageData(
|
|
|
|
blurMaskData,
|
2022-12-16 19:59:30 -06:00
|
|
|
options.keepUnmaskBlur,
|
|
|
|
options.keepUnmaskBlur
|
2022-12-15 08:51:18 -06:00
|
|
|
);
|
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
visibleCtx.filter = `blur(${options.keepUnmaskBlur}px)`;
|
2022-12-15 08:51:18 -06:00
|
|
|
visibleCtx.globalCompositeOperation = "destination-out";
|
|
|
|
visibleCtx.drawImage(blurMaskCanvas, 0, 0);
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmaskCtx.drawImage(
|
2022-12-15 08:51:18 -06:00
|
|
|
visibleCanvas,
|
2022-12-16 19:59:30 -06:00
|
|
|
-options.keepUnmaskBlur,
|
|
|
|
-options.keepUnmaskBlur
|
2022-12-15 08:51:18 -06:00
|
|
|
);
|
2022-12-13 21:16:48 -06:00
|
|
|
}
|
|
|
|
|
2022-11-29 14:55:25 -06:00
|
|
|
// Images to select through
|
|
|
|
let at = 0;
|
2022-12-09 19:07:18 -06:00
|
|
|
/** @type {Array<string|null>} */
|
|
|
|
const images = [null];
|
2022-12-15 09:25:20 -06:00
|
|
|
const seeds = [-1];
|
2022-11-29 14:55:25 -06:00
|
|
|
/** @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-12-21 09:07:29 -06:00
|
|
|
category: "display",
|
2022-11-29 14:55:25 -06:00
|
|
|
});
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
const redraw = (url = images[at]) => {
|
2022-12-16 21:55:53 -06:00
|
|
|
if (url === null) layer.clear();
|
2022-12-06 15:28:34 -06:00
|
|
|
if (!url) return;
|
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
const img = new Image();
|
|
|
|
img.src = "data:image/png;base64," + url;
|
|
|
|
img.addEventListener("load", () => {
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = bb.w;
|
|
|
|
canvas.height = bb.h;
|
|
|
|
|
|
|
|
// Creates new canvas for blurred mask
|
|
|
|
const blurMaskCanvas = document.createElement("canvas");
|
2022-12-16 19:59:30 -06:00
|
|
|
blurMaskCanvas.width = bb.w + options.keepUnmaskBlur * 2;
|
|
|
|
blurMaskCanvas.height = bb.h + options.keepUnmaskBlur * 2;
|
2022-12-15 08:51:18 -06:00
|
|
|
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, bb.w, bb.h);
|
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
if (keepUnmaskCanvas) {
|
|
|
|
ctx.drawImage(keepUnmaskCanvas, 0, 0);
|
2022-12-15 08:51:18 -06:00
|
|
|
}
|
|
|
|
|
2022-12-16 19:57:28 -06:00
|
|
|
layer.clear();
|
2022-12-06 15:28:34 -06:00
|
|
|
layer.ctx.drawImage(
|
2022-12-15 08:51:18 -06:00
|
|
|
canvas,
|
2022-12-06 15:28:34 -06:00
|
|
|
0,
|
|
|
|
0,
|
2022-12-15 08:51:18 -06:00
|
|
|
canvas.width,
|
|
|
|
canvas.height,
|
2022-12-06 15:28:34 -06:00
|
|
|
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);
|
2022-12-12 15:49:53 -06:00
|
|
|
interruptButton.classList.add("dream-stop-btn");
|
2022-12-06 15:28:34 -06:00
|
|
|
interruptButton.textContent = "Interrupt";
|
|
|
|
interruptButton.addEventListener("click", () => {
|
|
|
|
fetch(`${host}${url}interrupt`, {method: "POST"});
|
|
|
|
interruptButton.disabled = true;
|
|
|
|
});
|
2022-12-14 12:12:44 -06:00
|
|
|
const marchingOptions = {};
|
|
|
|
const stopMarchingAnts = march(bb, marchingOptions);
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
// 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;
|
2022-12-13 21:16:48 -06:00
|
|
|
let nextCP = options.drawEvery;
|
2022-12-06 15:28:34 -06:00
|
|
|
stopProgress = _monitorProgress(bb, (data) => {
|
|
|
|
if (stopDrawingStatus) return;
|
|
|
|
|
|
|
|
if (lastProgress < nextCP && data.progress >= nextCP) {
|
2022-12-13 21:16:48 -06:00
|
|
|
nextCP += options.drawEvery;
|
2022-12-06 15:28:34 -06:00
|
|
|
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-15 09:25:20 -06:00
|
|
|
var dreamData = await _dream(endpoint, requestCopy);
|
|
|
|
images.push(...dreamData.images);
|
|
|
|
seeds.push(...dreamData.seeds);
|
2022-12-06 15:28:34 -06:00
|
|
|
stopDrawingStatus = true;
|
2022-12-09 19:07:18 -06:00
|
|
|
at = 1;
|
2022-12-03 10:20:35 -06:00
|
|
|
} catch (e) {
|
|
|
|
alert(
|
2022-12-10 20:13:14 -06:00
|
|
|
`Error generating images. Please try again or see console for more details`
|
2022-12-03 10:20:35 -06:00
|
|
|
);
|
|
|
|
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;
|
|
|
|
|
2022-12-09 20:18:24 -06:00
|
|
|
imageindextxt.textContent = `${at}/${images.length - 1}`;
|
2022-12-15 09:25:20 -06:00
|
|
|
var seed = seeds[at];
|
2022-12-13 19:30:40 -06:00
|
|
|
seedbtn.title = "Use seed " + seed;
|
2022-11-29 18:16:59 -06:00
|
|
|
redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
const nextImg = () => {
|
|
|
|
at++;
|
|
|
|
if (at >= images.length) at = 0;
|
|
|
|
|
2022-12-09 20:18:24 -06:00
|
|
|
imageindextxt.textContent = `${at}/${images.length - 1}`;
|
2022-12-15 09:25:20 -06:00
|
|
|
var seed = seeds[at];
|
2022-12-13 19:30:40 -06:00
|
|
|
seedbtn.title = "Use seed " + seed;
|
2022-11-29 18:16:59 -06:00
|
|
|
redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
const applyImg = async () => {
|
2022-12-11 18:01:55 -06:00
|
|
|
if (!images[at]) return;
|
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
const img = new Image();
|
|
|
|
// load the image data after defining the closure
|
|
|
|
img.src = "data:image/png;base64," + images[at];
|
|
|
|
img.addEventListener("load", () => {
|
2022-12-13 21:16:48 -06:00
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = bb.w;
|
|
|
|
canvas.height = bb.h;
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, bb.w, bb.h);
|
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
if (keepUnmaskCanvas) {
|
|
|
|
ctx.drawImage(keepUnmaskCanvas, 0, 0);
|
2022-12-15 08:51:18 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
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-12-13 21:16:48 -06:00
|
|
|
image: canvas,
|
2022-11-29 18:16:59 -06:00
|
|
|
});
|
2022-12-19 18:51:29 -06:00
|
|
|
clean(!toolbar._current_tool.state.preserveMasks);
|
2022-11-29 18:16:59 -06:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-30 15:57:40 -06:00
|
|
|
const makeMore = async () => {
|
2022-12-11 21:50:25 -06:00
|
|
|
const moreQ = await waitQueue();
|
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-13 20:03:58 -06:00
|
|
|
if (requestCopy.seed != -1) {
|
2022-12-13 20:08:46 -06:00
|
|
|
requestCopy.seed =
|
|
|
|
parseInt(requestCopy.seed) +
|
|
|
|
requestCopy.batch_size * requestCopy.n_iter;
|
2022-12-13 20:03:58 -06:00
|
|
|
}
|
2022-12-15 09:25:20 -06:00
|
|
|
dreamData = await _dream(endpoint, requestCopy);
|
|
|
|
images.push(...dreamData.images);
|
|
|
|
seeds.push(...dreamData.seeds);
|
2022-12-09 20:18:24 -06:00
|
|
|
imageindextxt.textContent = `${at}/${images.length - 1}`;
|
2022-12-03 10:20:35 -06:00
|
|
|
} catch (e) {
|
|
|
|
alert(
|
2022-12-10 20:13:14 -06:00
|
|
|
`Error generating images. Please try again or see console for more details`
|
2022-12-03 10:20:35 -06:00
|
|
|
);
|
|
|
|
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-12-11 21:50:25 -06:00
|
|
|
|
|
|
|
nextQueue(moreQ);
|
2022-11-30 15:57:40 -06:00
|
|
|
};
|
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
const discardImg = async () => {
|
|
|
|
clean();
|
|
|
|
};
|
|
|
|
|
2022-12-11 18:01:55 -06:00
|
|
|
const saveImg = async () => {
|
|
|
|
if (!images[at]) return;
|
|
|
|
|
|
|
|
const img = new Image();
|
|
|
|
// load the image data after defining the closure
|
|
|
|
img.src = "data:image/png;base64," + images[at];
|
|
|
|
img.addEventListener("load", () => {
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
canvas.width = img.width;
|
|
|
|
canvas.height = img.height;
|
|
|
|
canvas.getContext("2d").drawImage(img, 0, 0);
|
|
|
|
|
|
|
|
downloadCanvas({
|
|
|
|
canvas,
|
|
|
|
filename: `openOutpaint - dream - ${request.prompt} - ${at}.png`,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-11-29 18:16:59 -06:00
|
|
|
// 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-12-14 12:12:44 -06:00
|
|
|
// For handling mouse events for navigation
|
|
|
|
const onmovehandler = mouse.listen.world.onmousemove.on(
|
|
|
|
(evn, state) => {
|
|
|
|
const contains = bb.contains(evn.x, evn.y);
|
|
|
|
|
2022-12-17 15:07:10 -06:00
|
|
|
if (!contains && !state.dream_processed) {
|
2022-12-14 12:12:44 -06:00
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
2022-12-17 15:07:10 -06:00
|
|
|
toolbar._current_tool.state.block_res_change = false;
|
|
|
|
}
|
|
|
|
if (!contains || state.dream_processed) {
|
|
|
|
marchingOptions.style = "#FFF";
|
|
|
|
toolbar._current_tool.state.block_res_change = false;
|
|
|
|
}
|
2022-12-14 12:12:44 -06:00
|
|
|
if (!state.dream_processed && contains) {
|
|
|
|
marchingOptions.style = "#F55";
|
|
|
|
|
|
|
|
imageCollection.inputElement.style.cursor = "pointer";
|
|
|
|
|
|
|
|
state.dream_processed = true;
|
2022-12-17 15:07:10 -06:00
|
|
|
toolbar._current_tool.state.block_res_change = true;
|
2022-12-14 12:12:44 -06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
0,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
const onclickhandler = mouse.listen.world.btn.left.onclick.on(
|
|
|
|
(evn, state) => {
|
|
|
|
if (!state.dream_processed && bb.contains(evn.x, evn.y)) {
|
|
|
|
applyImg();
|
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
|
|
|
state.dream_processed = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
const oncancelhandler = mouse.listen.world.btn.right.onclick.on(
|
|
|
|
(evn, state) => {
|
|
|
|
if (!state.dream_processed && bb.contains(evn.x, evn.y)) {
|
|
|
|
discardImg();
|
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
|
|
|
state.dream_processed = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
2022-12-15 06:43:26 -06:00
|
|
|
const onmorehandler = mouse.listen.world.btn.middle.onclick.on(
|
|
|
|
(evn, state) => {
|
2022-12-15 08:59:01 -06:00
|
|
|
if (
|
|
|
|
!state.dream_processed &&
|
|
|
|
bb.contains(evn.x, evn.y) &&
|
|
|
|
!evn.evn.ctrlKey
|
|
|
|
) {
|
2022-12-15 06:43:26 -06:00
|
|
|
makeMore();
|
|
|
|
state.dream_processed = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
2022-12-14 12:12:44 -06:00
|
|
|
const onwheelhandler = mouse.listen.world.onwheel.on(
|
|
|
|
(evn, state) => {
|
2022-12-14 12:45:41 -06:00
|
|
|
if (evn.evn.ctrlKey) return;
|
2022-12-14 12:12:44 -06:00
|
|
|
if (!state.dream_processed && bb.contains(evn.x, evn.y)) {
|
|
|
|
if (evn.delta < 0) nextImg();
|
|
|
|
else prevImg();
|
|
|
|
state.dream_processed = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
1,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
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);
|
2022-11-29 18:16:59 -06:00
|
|
|
keyboard.listen.onkeyclick.clear(onarrow);
|
2022-12-11 21:50:25 -06:00
|
|
|
// Remove area from no-generate list
|
|
|
|
generationAreas.delete(areaid);
|
2022-12-14 12:12:44 -06:00
|
|
|
|
|
|
|
// Stop handling inputs
|
|
|
|
mouse.listen.world.onmousemove.clear(onmovehandler);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(onclickhandler);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(oncancelhandler);
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.btn.middle.onclick.clear(onmorehandler);
|
|
|
|
mouse.listen.world.onwheel.clear(onwheelhandler);
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
|
|
|
|
imageSelectMenu = makeElement("div", bb.x, bb.y + bb.h);
|
|
|
|
|
|
|
|
const imageindextxt = document.createElement("button");
|
2022-12-09 20:18:24 -06:00
|
|
|
imageindextxt.textContent = `${at}/${images.length - 1}`;
|
2022-11-29 14:55:25 -06:00
|
|
|
imageindextxt.addEventListener("click", () => {
|
|
|
|
at = 0;
|
|
|
|
|
2022-12-09 20:18:24 -06:00
|
|
|
imageindextxt.textContent = `${at}/${images.length - 1}`;
|
2022-11-29 14:55:25 -06:00
|
|
|
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", () => {
|
2022-12-13 19:30:40 -06:00
|
|
|
const response = prompt(
|
|
|
|
"Enter new resource name",
|
2022-12-15 09:25:20 -06:00
|
|
|
"Dream Resource " + seeds[at]
|
2022-12-13 19:30:40 -06:00
|
|
|
);
|
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);
|
2022-12-11 18:01:55 -06:00
|
|
|
|
|
|
|
const savebtn = document.createElement("button");
|
|
|
|
savebtn.textContent = "S";
|
|
|
|
savebtn.title = "Download image to computer";
|
|
|
|
savebtn.addEventListener("click", async () => {
|
|
|
|
saveImg();
|
|
|
|
});
|
|
|
|
imageSelectMenu.appendChild(savebtn);
|
2022-12-11 21:50:25 -06:00
|
|
|
|
2022-12-13 19:30:40 -06:00
|
|
|
const seedbtn = document.createElement("button");
|
|
|
|
seedbtn.textContent = "U";
|
2022-12-15 09:25:20 -06:00
|
|
|
seedbtn.title = "Use seed " + `${seeds[at]}`;
|
2022-12-13 19:30:40 -06:00
|
|
|
seedbtn.addEventListener("click", () => {
|
2022-12-15 09:51:08 -06:00
|
|
|
sendSeed(seeds[at]);
|
2022-12-13 19:30:40 -06:00
|
|
|
});
|
|
|
|
imageSelectMenu.appendChild(seedbtn);
|
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
nextQueue(initialQ);
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for generating a image (dream tool)
|
|
|
|
*
|
|
|
|
* @param {*} evn
|
|
|
|
* @param {*} state
|
|
|
|
*/
|
2022-12-13 17:22:16 -06:00
|
|
|
const dream_generate_callback = async (bb, resolution, state) => {
|
2022-12-11 21:50:25 -06:00
|
|
|
// Build request to the API
|
|
|
|
const request = {};
|
|
|
|
Object.assign(request, stableDiffusionData);
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-13 17:22:16 -06:00
|
|
|
request.width = resolution.w;
|
|
|
|
request.height = resolution.h;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// 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;
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Get visible pixels
|
|
|
|
const visibleCanvas = uil.getVisible(bb);
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Use txt2img if canvas is blank
|
|
|
|
if (isCanvasBlank(0, 0, bb.w, bb.h, visibleCanvas)) {
|
|
|
|
// Dream
|
|
|
|
_generate("txt2img", request, bb);
|
|
|
|
} else {
|
|
|
|
// Use img2img if not
|
2022-12-04 13:22:35 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Temporary canvas for init image and mask generation
|
2022-12-13 21:16:48 -06:00
|
|
|
const bbCanvas = document.createElement("canvas");
|
|
|
|
bbCanvas.width = bb.w;
|
|
|
|
bbCanvas.height = bb.h;
|
|
|
|
const bbCtx = bbCanvas.getContext("2d");
|
|
|
|
|
|
|
|
const reqCanvas = document.createElement("canvas");
|
|
|
|
reqCanvas.width = request.width;
|
|
|
|
reqCanvas.height = request.height;
|
|
|
|
const reqCtx = reqCanvas.getContext("2d");
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = "#000F";
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Get init image
|
2022-12-13 21:16:48 -06:00
|
|
|
reqCtx.fillRect(0, 0, request.width, request.height);
|
|
|
|
reqCtx.drawImage(
|
2022-12-11 21:50:25 -06:00
|
|
|
visibleCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
2022-12-13 21:16:48 -06:00
|
|
|
request.init_images = [reqCanvas.toDataURL()];
|
2022-11-22 16:24:55 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Get mask image
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = "#000F";
|
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
2022-12-11 21:50:25 -06:00
|
|
|
if (state.invertMask) {
|
|
|
|
// 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-12-13 21:16:48 -06:00
|
|
|
bbCtx.globalCompositeOperation = "destination-in";
|
|
|
|
bbCtx.drawImage(
|
2022-12-11 21:50:25 -06:00
|
|
|
maskPaintCanvas,
|
2022-12-23 18:48:25 -06:00
|
|
|
bb.x,
|
|
|
|
bb.y,
|
2022-12-11 21:50:25 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-03 06:53:12 -06:00
|
|
|
bb.w,
|
2022-12-13 21:16:48 -06:00
|
|
|
bb.h
|
2022-12-03 06:53:12 -06:00
|
|
|
);
|
2022-12-13 21:16:48 -06:00
|
|
|
|
|
|
|
bbCtx.globalCompositeOperation = "destination-in";
|
|
|
|
bbCtx.drawImage(visibleCanvas, 0, 0);
|
2022-12-11 21:50:25 -06:00
|
|
|
} else {
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.globalCompositeOperation = "destination-in";
|
|
|
|
bbCtx.drawImage(visibleCanvas, 0, 0);
|
2022-12-11 21:50:25 -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
|
|
|
|
if (state.overMaskPx > 0) {
|
|
|
|
// transparent to white first
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
bbCtx.fillStyle = "#FFFF";
|
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
|
|
|
applyOvermask(bbCanvas, bbCtx, state.overMaskPx);
|
2022-11-26 13:34:12 -06:00
|
|
|
}
|
2022-12-11 21:50:25 -06:00
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.globalCompositeOperation = "destination-out"; // ???
|
|
|
|
bbCtx.drawImage(
|
2022-12-11 21:50:25 -06:00
|
|
|
maskPaintCanvas,
|
2022-12-23 18:48:25 -06:00
|
|
|
bb.x,
|
|
|
|
bb.y,
|
2022-12-11 21:50:25 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
2022-12-13 21:16:48 -06:00
|
|
|
bb.w,
|
|
|
|
bb.h
|
2022-12-11 21:50:25 -06:00
|
|
|
);
|
2022-11-22 16:24:55 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
|
|
|
|
bbCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
bbCtx.fillStyle = "#FFFF";
|
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
|
|
|
|
|
|
|
reqCtx.clearRect(0, 0, reqCanvas.width, reqCanvas.height);
|
|
|
|
reqCtx.drawImage(
|
|
|
|
bbCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
|
|
|
request.mask = reqCanvas.toDataURL();
|
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Dream
|
2022-12-13 21:16:48 -06:00
|
|
|
_generate("img2img", request, bb, {
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmask: state.keepUnmasked ? bbCanvas : null,
|
|
|
|
keepUnmaskBlur: state.keepUnmaskedBlur,
|
2022-12-13 21:16:48 -06:00
|
|
|
});
|
2022-11-22 16:24:55 -06:00
|
|
|
}
|
|
|
|
};
|
2022-12-13 12:13:01 -06:00
|
|
|
const dream_erase_callback = (bb) => {
|
2022-11-22 16:24:55 -06:00
|
|
|
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
|
|
|
|
*/
|
2022-12-13 17:22:16 -06:00
|
|
|
const dream_img2img_callback = (bb, resolution, state) => {
|
2022-12-11 21:50:25 -06:00
|
|
|
// Get visible pixels
|
|
|
|
const visibleCanvas = uil.getVisible(bb);
|
|
|
|
|
|
|
|
// Do nothing if no image exists
|
|
|
|
if (isCanvasBlank(0, 0, bb.w, bb.h, visibleCanvas)) return;
|
|
|
|
|
|
|
|
// Build request to the API
|
|
|
|
const request = {};
|
|
|
|
Object.assign(request, stableDiffusionData);
|
|
|
|
|
2022-12-13 17:22:16 -06:00
|
|
|
request.width = resolution.w;
|
|
|
|
request.height = resolution.h;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
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;
|
|
|
|
|
|
|
|
// Use img2img
|
|
|
|
|
|
|
|
// Temporary canvas for init image and mask generation
|
2022-12-13 21:16:48 -06:00
|
|
|
const bbCanvas = document.createElement("canvas");
|
|
|
|
bbCanvas.width = bb.w;
|
|
|
|
bbCanvas.height = bb.h;
|
|
|
|
const bbCtx = bbCanvas.getContext("2d");
|
2022-12-11 21:50:25 -06:00
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = "#000F";
|
2022-12-11 21:50:25 -06:00
|
|
|
|
|
|
|
// Get init image
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
|
|
|
bbCtx.drawImage(visibleCanvas, 0, 0);
|
|
|
|
request.init_images = [bbCanvas.toDataURL()];
|
2022-12-11 21:50:25 -06:00
|
|
|
|
|
|
|
// Get mask image
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = state.invertMask ? "#FFFF" : "#000F";
|
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
|
|
|
bbCtx.globalCompositeOperation = "destination-out";
|
2022-12-23 18:48:55 -06:00
|
|
|
bbCtx.drawImage(maskPaintCanvas, bb.x, bb.y, bb.w, bb.h, 0, 0, bb.w, bb.h);
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.globalCompositeOperation = "destination-atop";
|
|
|
|
bbCtx.fillStyle = state.invertMask ? "#000F" : "#FFFF";
|
2022-12-15 22:26:46 -06:00
|
|
|
bbCtx.fillRect(0, 0, bb.w, bb.h);
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Border Mask
|
|
|
|
if (state.keepBorderSize > 0) {
|
2022-12-15 22:26:46 -06:00
|
|
|
const keepBorderCanvas = document.createElement("canvas");
|
|
|
|
keepBorderCanvas.width = request.width;
|
|
|
|
keepBorderCanvas.height = request.height;
|
|
|
|
const keepBorderCtx = keepBorderCanvas.getContext("2d");
|
|
|
|
keepBorderCtx.fillStyle = "#000F";
|
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
if (state.gradient) {
|
2022-12-15 22:26:46 -06:00
|
|
|
const lg = keepBorderCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
2022-12-11 21:50:25 -06:00
|
|
|
lg.addColorStop(0, "#000F");
|
|
|
|
lg.addColorStop(1, "#0000");
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillStyle = lg;
|
2022-12-11 21:50:25 -06:00
|
|
|
}
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillRect(0, 0, state.keepBorderSize, request.height);
|
2022-12-11 21:50:25 -06:00
|
|
|
if (state.gradient) {
|
2022-12-15 22:26:46 -06:00
|
|
|
const tg = keepBorderCtx.createLinearGradient(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize
|
|
|
|
);
|
2022-12-11 21:50:25 -06:00
|
|
|
tg.addColorStop(0, "#000F");
|
|
|
|
tg.addColorStop(1, "#0000");
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillStyle = tg;
|
2022-12-11 21:50:25 -06:00
|
|
|
}
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillRect(0, 0, request.width, state.keepBorderSize);
|
2022-12-11 21:50:25 -06:00
|
|
|
if (state.gradient) {
|
2022-12-15 22:26:46 -06:00
|
|
|
const rg = keepBorderCtx.createLinearGradient(
|
2022-12-11 21:50:25 -06:00
|
|
|
request.width,
|
|
|
|
0,
|
|
|
|
request.width - state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
rg.addColorStop(0, "#000F");
|
|
|
|
rg.addColorStop(1, "#0000");
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillStyle = rg;
|
2022-12-11 21:50:25 -06:00
|
|
|
}
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillRect(
|
2022-12-11 21:50:25 -06:00
|
|
|
request.width - state.keepBorderSize,
|
2022-12-03 06:53:12 -06:00
|
|
|
0,
|
2022-12-11 21:50:25 -06:00
|
|
|
state.keepBorderSize,
|
2022-12-03 06:53:12 -06:00
|
|
|
request.height
|
|
|
|
);
|
2022-12-11 21:50:25 -06:00
|
|
|
if (state.gradient) {
|
2022-12-15 22:26:46 -06:00
|
|
|
const bg = keepBorderCtx.createLinearGradient(
|
2022-11-22 19:24:04 -06:00
|
|
|
0,
|
2022-12-11 21:50:25 -06:00
|
|
|
request.height,
|
2022-11-22 19:24:04 -06:00
|
|
|
0,
|
2022-12-11 21:50:25 -06:00
|
|
|
request.height - state.keepBorderSize
|
2022-11-22 19:24:04 -06:00
|
|
|
);
|
2022-12-11 21:50:25 -06:00
|
|
|
bg.addColorStop(0, "#000F");
|
|
|
|
bg.addColorStop(1, "#0000");
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillStyle = bg;
|
2022-11-22 19:24:04 -06:00
|
|
|
}
|
2022-12-15 22:26:46 -06:00
|
|
|
keepBorderCtx.fillRect(
|
2022-12-11 21:50:25 -06:00
|
|
|
0,
|
|
|
|
request.height - state.keepBorderSize,
|
|
|
|
request.width,
|
|
|
|
state.keepBorderSize
|
|
|
|
);
|
2022-12-15 22:26:46 -06:00
|
|
|
|
|
|
|
bbCtx.globalCompositeOperation = "source-over";
|
|
|
|
bbCtx.drawImage(
|
|
|
|
keepBorderCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
bb.w,
|
|
|
|
bb.h
|
|
|
|
);
|
2022-12-11 21:50:25 -06:00
|
|
|
}
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-12-15 22:26:46 -06:00
|
|
|
const reqCanvas = document.createElement("canvas");
|
|
|
|
reqCanvas.width = request.width;
|
|
|
|
reqCanvas.height = request.height;
|
|
|
|
const reqCtx = reqCanvas.getContext("2d");
|
|
|
|
|
|
|
|
reqCtx.drawImage(
|
|
|
|
bbCanvas,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
bb.w,
|
|
|
|
bb.h,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
request.width,
|
|
|
|
request.height
|
|
|
|
);
|
|
|
|
|
|
|
|
request.mask = reqCanvas.toDataURL();
|
2022-12-11 21:50:25 -06:00
|
|
|
request.inpaint_full_res = state.fullResolution;
|
2022-11-22 19:24:04 -06:00
|
|
|
|
2022-12-11 21:50:25 -06:00
|
|
|
// Dream
|
2022-12-13 21:16:48 -06:00
|
|
|
_generate("img2img", request, bb, {
|
2022-12-16 19:59:30 -06:00
|
|
|
keepUnmask: state.keepUnmasked ? bbCanvas : null,
|
|
|
|
keepUnmaskBlur: state.keepUnmaskedBlur,
|
2022-12-13 21:16:48 -06:00
|
|
|
});
|
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-03 06:53:12 -06:00
|
|
|
/**
|
|
|
|
* Generic wheel handler
|
|
|
|
*/
|
|
|
|
|
|
|
|
const _dream_onwheel = (evn, state) => {
|
|
|
|
if (!evn.evn.ctrlKey) {
|
2022-12-07 15:51:33 -06:00
|
|
|
// Seems mouse wheel scroll is very different between different browsers.
|
|
|
|
// Will use scroll as just an event to go to the next cursor snap position instead.
|
|
|
|
//
|
|
|
|
// TODO: Someone that has a smooth scrolling mouse should verify if this works with them.
|
|
|
|
|
|
|
|
const v = state.cursorSize - 128 * (evn.delta / Math.abs(evn.delta));
|
|
|
|
|
2022-12-03 06:53:12 -06:00
|
|
|
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(
|
2022-12-18 17:50:40 -06:00
|
|
|
"./res/icons/image-plus.svg",
|
2022-11-24 09:30:13 -06:00
|
|
|
"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-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.on(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.erasecb);
|
|
|
|
|
|
|
|
// Select Region listeners
|
2022-12-13 12:13:01 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.on(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.on(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.on(state.dragendcb);
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.smousemovecb, 2, true);
|
|
|
|
mouse.listen.world.onwheel.on(state.swheelcb, 2, true);
|
|
|
|
mouse.listen.world.btn.left.onclick.on(state.sdreamcb, 2, true);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.serasecb, 2, true);
|
|
|
|
mouse.listen.world.btn.middle.onclick.on(state.smiddlecb, 2, true);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
// Clear Selection
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-11-26 19:35:16 -06:00
|
|
|
// Display Mask
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
2022-12-12 16:25:49 -06:00
|
|
|
|
|
|
|
// update cursor size if matching is enabled
|
2022-12-12 18:25:02 -06:00
|
|
|
if (stableDiffusionData.sync_cursor_size) {
|
2022-12-12 16:25:49 -06:00
|
|
|
state.setCursorSize(stableDiffusionData.width);
|
|
|
|
}
|
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-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.erasecb);
|
|
|
|
|
|
|
|
// Clear Select Region listeners
|
2022-12-13 12:13:01 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.clear(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.clear(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.clear(state.dragendcb);
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.smousemovecb);
|
|
|
|
mouse.listen.world.onwheel.clear(state.swheelcb);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.sdreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.serasecb);
|
|
|
|
mouse.listen.world.btn.middle.onclick.clear(state.smiddlecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
// Clear Selection
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
|
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-12-16 19:59:30 -06:00
|
|
|
state.keepUnmasked = true;
|
|
|
|
state.keepUnmaskedBlur = 8;
|
2022-11-24 09:30:13 -06:00
|
|
|
state.overMaskPx = 0;
|
2022-12-19 18:51:29 -06:00
|
|
|
state.preserveMasks = false;
|
2022-12-01 15:05:14 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
state.erasePrevCursor = () =>
|
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
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-15 06:43:26 -06:00
|
|
|
/**
|
|
|
|
* Selection handlers
|
|
|
|
*/
|
|
|
|
const selection = _tool._draggable_selection(state);
|
|
|
|
state.dragstartcb = (evn) => selection.dragstartcb(evn);
|
|
|
|
state.dragcb = (evn) => selection.dragcb(evn);
|
|
|
|
state.dragendcb = (evn) => selection.dragendcb(evn);
|
|
|
|
state.smousemovecb = (evn, estate) => {
|
|
|
|
selection.smousemovecb(evn);
|
|
|
|
if (selection.inside) {
|
|
|
|
imageCollection.inputElement.style.cursor = "pointer";
|
|
|
|
|
|
|
|
estate.dream_processed = true;
|
|
|
|
} else {
|
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
|
|
|
}
|
2022-12-13 12:13:01 -06:00
|
|
|
};
|
2022-12-15 06:43:26 -06:00
|
|
|
state.swheelcb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
state.wheelcb(evn, {});
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
2022-12-13 12:13:01 -06:00
|
|
|
};
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.sdreamcb = (evn, estate) => {
|
|
|
|
if (selection.exists && !selection.inside) {
|
|
|
|
selection.deselect();
|
|
|
|
state.redraw();
|
|
|
|
estate.selection_processed = true;
|
|
|
|
}
|
|
|
|
if (selection.inside) {
|
|
|
|
state.dreamcb(evn, {});
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
|
|
|
};
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.serasecb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
state.redraw();
|
2022-12-15 06:43:26 -06:00
|
|
|
estate.dream_processed = true;
|
2022-12-13 12:13:01 -06:00
|
|
|
}
|
|
|
|
};
|
2022-12-15 06:43:26 -06:00
|
|
|
state.smiddlecb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
state.selection = selection;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
/**
|
|
|
|
* Dream Handlers
|
|
|
|
*/
|
2022-12-01 15:05:14 -06:00
|
|
|
state.mousemovecb = (evn) => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = evn;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
|
|
|
state.erasePrevCursor();
|
2022-12-01 15:05:14 -06:00
|
|
|
state.erasePrevReticle();
|
2022-12-13 12:13:01 -06:00
|
|
|
|
|
|
|
let x = evn.x;
|
|
|
|
let y = evn.y;
|
|
|
|
if (state.snapToGrid) {
|
|
|
|
x += snap(evn.x, 0, 64);
|
|
|
|
y += snap(evn.y, 0, 64);
|
|
|
|
}
|
|
|
|
|
2022-12-21 15:29:11 -06:00
|
|
|
state.erasePrevCursor = _tool._cursor_draw(x, y);
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
if (state.selection.exists) {
|
|
|
|
const bb = state.selection.bb;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-13 14:04:16 -06:00
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.erasePrevReticle = _tool._reticle_draw(
|
2022-12-13 14:04:16 -06:00
|
|
|
bb,
|
|
|
|
"Dream",
|
|
|
|
{
|
|
|
|
w: Math.round(
|
|
|
|
bb.w * (stableDiffusionData.width / state.cursorSize)
|
|
|
|
),
|
|
|
|
h: Math.round(
|
|
|
|
bb.h * (stableDiffusionData.height / state.cursorSize)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-12-21 09:07:29 -06:00
|
|
|
toolTextStyle:
|
|
|
|
global.connection === "online" ? "#FFF5" : "#F555",
|
2022-12-15 06:43:26 -06:00
|
|
|
reticleStyle: state.selection.inside ? "#F55" : "#FFF",
|
2022-12-13 14:04:16 -06:00
|
|
|
sizeTextStyle: style,
|
|
|
|
}
|
|
|
|
);
|
2022-12-13 12:13:01 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:28:34 -06:00
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
2022-12-15 06:43:26 -06:00
|
|
|
state.erasePrevReticle = _tool._reticle_draw(
|
2022-12-13 12:13:01 -06:00
|
|
|
getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
),
|
|
|
|
"Dream",
|
|
|
|
{
|
|
|
|
w: stableDiffusionData.width,
|
|
|
|
h: stableDiffusionData.height,
|
|
|
|
},
|
|
|
|
{
|
2022-12-21 09:07:29 -06:00
|
|
|
toolTextStyle: global.connection === "online" ? "#FFF5" : "#F555",
|
2022-12-13 12:13:01 -06:00
|
|
|
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-14 12:12:44 -06:00
|
|
|
state.wheelcb = (evn, estate) => {
|
|
|
|
if (estate.dream_processed) return;
|
2022-12-03 06:53:12 -06:00
|
|
|
_dream_onwheel(evn, state);
|
2022-11-29 14:55:25 -06:00
|
|
|
};
|
2022-12-14 12:12:44 -06:00
|
|
|
state.dreamcb = (evn, estate) => {
|
2022-12-15 06:43:26 -06:00
|
|
|
if (estate.dream_processed || estate.selection_processed) return;
|
2022-12-13 12:13:01 -06:00
|
|
|
const bb =
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.bb ||
|
2022-12-13 12:13:01 -06:00
|
|
|
getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
2022-12-15 06:43:26 -06:00
|
|
|
const resolution = state.selection.bb || {
|
2022-12-13 17:22:16 -06:00
|
|
|
w: stableDiffusionData.width,
|
|
|
|
h: stableDiffusionData.height,
|
|
|
|
};
|
2022-12-21 09:07:29 -06:00
|
|
|
|
|
|
|
if (global.connection === "online") {
|
|
|
|
dream_generate_callback(bb, resolution, state);
|
|
|
|
} else {
|
|
|
|
const stop = march(bb, {
|
|
|
|
title: "offline",
|
|
|
|
titleStyle: "#F555",
|
|
|
|
style: "#F55",
|
|
|
|
});
|
|
|
|
setTimeout(stop, 2000);
|
|
|
|
}
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-21 09:07:29 -06:00
|
|
|
state.redraw();
|
2022-12-13 12:13:01 -06:00
|
|
|
};
|
2022-12-14 12:12:44 -06:00
|
|
|
state.erasecb = (evn, estate) => {
|
2022-12-15 06:43:26 -06:00
|
|
|
if (state.selection.exists) {
|
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
state.redraw();
|
|
|
|
return;
|
|
|
|
}
|
2022-12-14 12:12:44 -06:00
|
|
|
if (estate.dream_processed) return;
|
2022-12-13 12:13:01 -06:00
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
dream_erase_callback(bb, state);
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
},
|
|
|
|
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",
|
|
|
|
{
|
2022-12-12 17:09:27 -06:00
|
|
|
min: 128,
|
2022-12-03 06:53:12 -06:00
|
|
|
max: 2048,
|
|
|
|
step: 128,
|
|
|
|
textStep: 2,
|
2022-12-12 21:05:04 -06:00
|
|
|
cb: () => {
|
|
|
|
if (stableDiffusionData.sync_cursor_size) {
|
|
|
|
state.ignorePrevious = true;
|
|
|
|
resSlider.value = state.cursorSize;
|
|
|
|
}
|
|
|
|
},
|
2022-12-03 06:53:12 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2022-12-13 21:16:48 -06:00
|
|
|
// Keep Masked Content Checkbox
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedLabel = _toolbar_input.checkbox(
|
2022-12-13 21:16:48 -06:00
|
|
|
state,
|
2022-12-16 19:59:30 -06:00
|
|
|
"keepUnmasked",
|
|
|
|
"Keep Unmasked",
|
2022-12-15 08:51:18 -06:00
|
|
|
() => {
|
2022-12-16 19:59:30 -06:00
|
|
|
if (state.keepUnmasked) {
|
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider.classList.remove(
|
2022-12-15 08:51:18 -06:00
|
|
|
"invisible"
|
|
|
|
);
|
|
|
|
} else {
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider.classList.add("invisible");
|
2022-12-15 08:51:18 -06:00
|
|
|
}
|
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
).label;
|
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
// Keep Masked Content Blur Slider
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider = _toolbar_input.slider(
|
2022-12-15 08:51:18 -06:00
|
|
|
state,
|
2022-12-16 19:59:30 -06:00
|
|
|
"keepUnmaskedBlur",
|
|
|
|
"Keep Unmasked Blur",
|
2022-12-15 08:51:18 -06:00
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 64,
|
|
|
|
step: 4,
|
|
|
|
textStep: 1,
|
|
|
|
}
|
|
|
|
).slider;
|
|
|
|
|
2022-12-19 18:51:29 -06:00
|
|
|
// Preserve Brushed Masks Checkbox
|
|
|
|
state.ctxmenu.preserveMasksLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"preserveMasks",
|
|
|
|
"Preserve Brushed Masks"
|
|
|
|
).label;
|
|
|
|
|
2022-11-26 13:34:12 -06:00
|
|
|
// 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,
|
2022-12-07 10:31:15 -06:00
|
|
|
step: 4,
|
2022-12-03 06:53:12 -06:00
|
|
|
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-12-16 19:59:30 -06:00
|
|
|
menu.appendChild(state.ctxmenu.keepUnmaskedLabel);
|
|
|
|
menu.appendChild(state.ctxmenu.keepUnmaskedBlurSlider);
|
2022-12-19 18:51:29 -06:00
|
|
|
menu.appendChild(state.ctxmenu.preserveMasksLabel);
|
|
|
|
menu.appendChild(document.createElement("br"));
|
2022-11-24 09:30:13 -06:00
|
|
|
menu.appendChild(state.ctxmenu.overMaskPxLabel);
|
|
|
|
},
|
|
|
|
shortcut: "D",
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const img2imgTool = () =>
|
|
|
|
toolbar.registerTool(
|
2022-12-18 17:50:40 -06:00
|
|
|
"./res/icons/image.svg",
|
2022-11-24 09:30:13 -06:00
|
|
|
"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-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.on(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.erasecb);
|
|
|
|
|
|
|
|
// Select Region listeners
|
2022-12-13 12:13:01 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.on(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.on(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.on(state.dragendcb);
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.onmousemove.on(state.smousemovecb, 2, true);
|
|
|
|
mouse.listen.world.onwheel.on(state.swheelcb, 2, true);
|
|
|
|
mouse.listen.world.btn.left.onclick.on(state.sdreamcb, 2, true);
|
|
|
|
mouse.listen.world.btn.right.onclick.on(state.serasecb, 2, true);
|
|
|
|
mouse.listen.world.btn.middle.onclick.on(state.smiddlecb, 2, true);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
// Clear Selection
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-11-26 19:35:16 -06:00
|
|
|
// Display Mask
|
|
|
|
setMask(state.invertMask ? "hold" : "clear");
|
2022-12-12 16:25:49 -06:00
|
|
|
|
|
|
|
// update cursor size if matching is enabled
|
2022-12-12 18:25:02 -06:00
|
|
|
if (stableDiffusionData.sync_cursor_size) {
|
2022-12-12 16:25:49 -06:00
|
|
|
state.setCursorSize(stableDiffusionData.width);
|
|
|
|
}
|
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-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.dreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.erasecb);
|
|
|
|
|
|
|
|
// Clear Select Region listeners
|
2022-12-13 12:13:01 -06:00
|
|
|
mouse.listen.world.btn.left.ondragstart.clear(state.dragstartcb);
|
|
|
|
mouse.listen.world.btn.left.ondrag.clear(state.dragcb);
|
|
|
|
mouse.listen.world.btn.left.ondragend.clear(state.dragendcb);
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
mouse.listen.world.onmousemove.clear(state.smousemovecb);
|
|
|
|
mouse.listen.world.onwheel.clear(state.swheelcb);
|
|
|
|
mouse.listen.world.btn.left.onclick.clear(state.sdreamcb);
|
|
|
|
mouse.listen.world.btn.right.onclick.clear(state.serasecb);
|
|
|
|
mouse.listen.world.btn.middle.onclick.clear(state.smiddlecb);
|
2022-11-26 19:35:16 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
// Clear Selection
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
|
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-12-16 19:59:30 -06:00
|
|
|
state.keepUnmasked = true;
|
|
|
|
state.keepUnmaskedBlur = 8;
|
2022-11-26 13:59:24 -06:00
|
|
|
state.fullResolution = false;
|
2022-12-19 18:51:29 -06:00
|
|
|
state.preserveMasks = 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-13 12:13:01 -06:00
|
|
|
state.erasePrevCursor = () =>
|
|
|
|
uiCtx.clearRect(0, 0, uiCanvas.width, uiCanvas.height);
|
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-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
/**
|
|
|
|
* Selection handlers
|
|
|
|
*/
|
|
|
|
const selection = _tool._draggable_selection(state);
|
|
|
|
state.dragstartcb = (evn) => selection.dragstartcb(evn);
|
|
|
|
state.dragcb = (evn) => selection.dragcb(evn);
|
|
|
|
state.dragendcb = (evn) => selection.dragendcb(evn);
|
|
|
|
state.smousemovecb = (evn, estate) => {
|
|
|
|
selection.smousemovecb(evn);
|
|
|
|
if (selection.inside) {
|
|
|
|
imageCollection.inputElement.style.cursor = "pointer";
|
|
|
|
|
|
|
|
estate.dream_processed = true;
|
|
|
|
} else {
|
|
|
|
imageCollection.inputElement.style.cursor = "auto";
|
|
|
|
}
|
2022-12-13 12:13:01 -06:00
|
|
|
};
|
2022-12-15 06:43:26 -06:00
|
|
|
state.swheelcb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
state.wheelcb(evn, {});
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
2022-12-13 12:13:01 -06:00
|
|
|
};
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.sdreamcb = (evn, estate) => {
|
|
|
|
if (selection.exists && !selection.inside) {
|
|
|
|
selection.deselect();
|
2022-12-15 19:36:41 -06:00
|
|
|
state.redraw();
|
2022-12-15 06:43:26 -06:00
|
|
|
estate.selection_processed = true;
|
|
|
|
}
|
|
|
|
if (selection.inside) {
|
|
|
|
state.dreamcb(evn, {});
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
|
|
|
};
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.serasecb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
state.erasecb(evn, {});
|
|
|
|
estate.dream_processed = true;
|
|
|
|
}
|
|
|
|
};
|
2022-12-13 12:13:01 -06:00
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.smiddlecb = (evn, estate) => {
|
|
|
|
if (selection.inside) {
|
|
|
|
estate.dream_processed = true;
|
2022-12-13 12:13:01 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection = selection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dream handlers
|
|
|
|
*/
|
2022-12-01 15:05:14 -06:00
|
|
|
state.mousemovecb = (evn) => {
|
2022-12-06 17:00:50 -06:00
|
|
|
state.lastMouseMove = evn;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
|
|
|
state.erasePrevCursor();
|
2022-12-01 15:05:14 -06:00
|
|
|
state.erasePrevReticle();
|
2022-12-06 17:08:55 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
let x = evn.x;
|
|
|
|
let y = evn.y;
|
|
|
|
if (state.snapToGrid) {
|
|
|
|
x += snap(evn.x, 0, 64);
|
|
|
|
y += snap(evn.y, 0, 64);
|
|
|
|
}
|
2022-12-06 17:00:50 -06:00
|
|
|
|
2022-12-21 15:29:11 -06:00
|
|
|
state.erasePrevCursor = _tool._cursor_draw(x, y);
|
2022-12-03 06:53:12 -06:00
|
|
|
|
2022-12-13 12:13:01 -06:00
|
|
|
// Resolution
|
|
|
|
let bb = null;
|
|
|
|
let request = null;
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
if (state.selection.exists) {
|
|
|
|
bb = state.selection.bb;
|
2022-12-13 12:13:01 -06:00
|
|
|
|
|
|
|
request = {width: bb.w, height: bb.h};
|
|
|
|
|
2022-12-15 06:43:26 -06:00
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
|
|
|
state.erasePrevReticle = _tool._reticle_draw(
|
2022-12-13 14:04:16 -06:00
|
|
|
bb,
|
|
|
|
"Img2Img",
|
|
|
|
{
|
|
|
|
w: Math.round(
|
|
|
|
bb.w * (stableDiffusionData.width / state.cursorSize)
|
|
|
|
),
|
|
|
|
h: Math.round(
|
|
|
|
bb.h * (stableDiffusionData.height / state.cursorSize)
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2022-12-21 09:07:29 -06:00
|
|
|
toolTextStyle:
|
|
|
|
global.connection === "online" ? "#FFF5" : "#F555",
|
2022-12-15 06:43:26 -06:00
|
|
|
reticleStyle: state.selection.inside ? "#F55" : "#FFF",
|
2022-12-13 14:04:16 -06:00
|
|
|
sizeTextStyle: style,
|
|
|
|
}
|
|
|
|
);
|
2022-12-13 12:13:01 -06:00
|
|
|
} else {
|
|
|
|
bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
|
|
|
|
request = {
|
|
|
|
width: stableDiffusionData.width,
|
|
|
|
height: stableDiffusionData.height,
|
|
|
|
};
|
|
|
|
|
|
|
|
const style =
|
|
|
|
state.cursorSize > stableDiffusionData.width
|
|
|
|
? "#FBB5"
|
|
|
|
: state.cursorSize < stableDiffusionData.width
|
|
|
|
? "#BFB5"
|
|
|
|
: "#FFF5";
|
2022-12-15 06:43:26 -06:00
|
|
|
state.erasePrevReticle = _tool._reticle_draw(
|
2022-12-13 12:13:01 -06:00
|
|
|
bb,
|
|
|
|
"Img2Img",
|
|
|
|
{w: request.width, h: request.height},
|
|
|
|
{
|
2022-12-21 09:07:29 -06:00
|
|
|
toolTextStyle:
|
|
|
|
global.connection === "online" ? "#FFF5" : "#F555",
|
2022-12-13 12:13:01 -06:00
|
|
|
sizeTextStyle: style,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.exists &&
|
|
|
|
(state.selection.selected.now.x ===
|
|
|
|
state.selection.selected.start.x ||
|
|
|
|
state.selection.selected.now.y ===
|
|
|
|
state.selection.selected.start.y)
|
2022-12-13 12:13:01 -06:00
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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
|
2022-12-13 21:16:48 -06:00
|
|
|
const bbCanvas = document.createElement("canvas");
|
|
|
|
bbCanvas.width = request.width;
|
|
|
|
bbCanvas.height = request.height;
|
|
|
|
const bbCtx = bbCanvas.getContext("2d");
|
2022-11-29 14:55:25 -06:00
|
|
|
|
|
|
|
if (state.keepBorderSize > 0) {
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = "#6A6AFF30";
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
2022-12-13 21:16:48 -06:00
|
|
|
const lg = bbCtx.createLinearGradient(
|
2022-12-04 15:57:26 -06:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
lg.addColorStop(0, "#6A6AFF30");
|
|
|
|
lg.addColorStop(1, "#0000");
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = lg;
|
2022-12-04 15:57:26 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillRect(0, 0, state.keepBorderSize, request.height);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
2022-12-13 21:16:48 -06:00
|
|
|
const tg = bbCtx.createLinearGradient(
|
2022-12-04 15:57:26 -06:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
state.keepBorderSize
|
|
|
|
);
|
|
|
|
tg.addColorStop(0, "#6A6AFF30");
|
|
|
|
tg.addColorStop(1, "#6A6AFF00");
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = tg;
|
2022-12-04 15:57:26 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillRect(0, 0, request.width, state.keepBorderSize);
|
2022-12-04 15:57:26 -06:00
|
|
|
if (state.gradient) {
|
2022-12-13 21:16:48 -06:00
|
|
|
const rg = bbCtx.createLinearGradient(
|
2022-12-04 15:57:26 -06:00
|
|
|
request.width,
|
|
|
|
0,
|
|
|
|
request.width - state.keepBorderSize,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
rg.addColorStop(0, "#6A6AFF30");
|
|
|
|
rg.addColorStop(1, "#6A6AFF00");
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = rg;
|
2022-12-04 15:57:26 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.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) {
|
2022-12-13 21:16:48 -06:00
|
|
|
const bg = bbCtx.createLinearGradient(
|
2022-12-04 15:57:26 -06:00
|
|
|
0,
|
|
|
|
request.height,
|
|
|
|
0,
|
|
|
|
request.height - state.keepBorderSize
|
|
|
|
);
|
|
|
|
bg.addColorStop(0, "#6A6AFF30");
|
|
|
|
bg.addColorStop(1, "#6A6AFF00");
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillStyle = bg;
|
2022-12-04 15:57:26 -06:00
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
bbCtx.fillRect(
|
2022-11-29 14:55:25 -06:00
|
|
|
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-13 21:16:48 -06:00
|
|
|
bbCanvas,
|
2022-12-03 06:53:12 -06:00
|
|
|
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-14 12:12:44 -06:00
|
|
|
state.wheelcb = (evn, estate) => {
|
|
|
|
if (estate.dream_processed) return;
|
2022-12-03 06:53:12 -06:00
|
|
|
_dream_onwheel(evn, state);
|
|
|
|
};
|
2022-12-14 12:12:44 -06:00
|
|
|
state.dreamcb = (evn, estate) => {
|
2022-12-15 06:43:26 -06:00
|
|
|
if (estate.dream_processed || estate.selection_processed) return;
|
2022-12-13 12:13:01 -06:00
|
|
|
const bb =
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.bb ||
|
2022-12-13 12:13:01 -06:00
|
|
|
getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
2022-12-15 06:43:26 -06:00
|
|
|
const resolution = state.selection.bb || {
|
2022-12-13 17:22:16 -06:00
|
|
|
w: stableDiffusionData.width,
|
|
|
|
h: stableDiffusionData.height,
|
|
|
|
};
|
2022-12-21 09:07:29 -06:00
|
|
|
if (global.connection === "online") {
|
|
|
|
dream_img2img_callback(bb, resolution, state);
|
|
|
|
} else {
|
|
|
|
const stop = march(bb, {
|
|
|
|
title: "offline",
|
|
|
|
titleStyle: "#F555",
|
|
|
|
style: "#F55",
|
|
|
|
});
|
|
|
|
setTimeout(stop, 2000);
|
|
|
|
}
|
2022-12-15 06:43:26 -06:00
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
state.redraw();
|
|
|
|
};
|
2022-12-14 12:12:44 -06:00
|
|
|
state.erasecb = (evn, estate) => {
|
|
|
|
if (estate.dream_processed) return;
|
2022-12-15 06:43:26 -06:00
|
|
|
if (state.selection.exists) {
|
|
|
|
state.selection.deselect();
|
2022-12-13 12:13:01 -06:00
|
|
|
state.redraw();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const bb = getBoundingBox(
|
|
|
|
evn.x,
|
|
|
|
evn.y,
|
|
|
|
state.cursorSize,
|
|
|
|
state.cursorSize,
|
|
|
|
state.snapToGrid && basePixelCount
|
|
|
|
);
|
|
|
|
dream_erase_callback(bb, state);
|
2022-11-24 09:30:13 -06:00
|
|
|
};
|
|
|
|
},
|
|
|
|
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",
|
|
|
|
{
|
2022-12-12 17:09:27 -06:00
|
|
|
min: 128,
|
2022-12-03 06:53:12 -06:00
|
|
|
max: 2048,
|
|
|
|
step: 128,
|
|
|
|
textStep: 2,
|
2022-12-12 19:52:14 -06:00
|
|
|
cb: () => {
|
|
|
|
if (stableDiffusionData.sync_cursor_size) {
|
|
|
|
state.ignorePrevious = true;
|
|
|
|
resSlider.value = state.cursorSize;
|
|
|
|
}
|
|
|
|
},
|
2022-12-03 06:53:12 -06:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
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-12-13 21:16:48 -06:00
|
|
|
// Keep Masked Content Checkbox
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedLabel = _toolbar_input.checkbox(
|
2022-12-13 21:16:48 -06:00
|
|
|
state,
|
2022-12-16 19:59:30 -06:00
|
|
|
"keepUnmasked",
|
|
|
|
"Keep Unmasked",
|
2022-12-15 08:51:18 -06:00
|
|
|
() => {
|
2022-12-16 19:59:30 -06:00
|
|
|
if (state.keepUnmasked) {
|
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider.classList.remove(
|
2022-12-15 08:51:18 -06:00
|
|
|
"invisible"
|
|
|
|
);
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSliderLinebreak.classList.add(
|
2022-12-15 08:51:18 -06:00
|
|
|
"invisible"
|
|
|
|
);
|
|
|
|
} else {
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider.classList.add("invisible");
|
|
|
|
state.ctxmenu.keepUnmaskedBlurSliderLinebreak.classList.remove(
|
2022-12-15 08:51:18 -06:00
|
|
|
"invisible"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-12-13 21:16:48 -06:00
|
|
|
).label;
|
|
|
|
|
2022-12-15 08:51:18 -06:00
|
|
|
// Keep Masked Content Blur Slider
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSlider = _toolbar_input.slider(
|
2022-12-15 08:51:18 -06:00
|
|
|
state,
|
2022-12-16 19:59:30 -06:00
|
|
|
"keepUnmaskedBlur",
|
|
|
|
"Keep Unmasked Blur",
|
2022-12-15 08:51:18 -06:00
|
|
|
{
|
|
|
|
min: 0,
|
|
|
|
max: 64,
|
|
|
|
step: 4,
|
|
|
|
textStep: 1,
|
|
|
|
}
|
|
|
|
).slider;
|
|
|
|
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSliderLinebreak =
|
2022-12-15 08:51:18 -06:00
|
|
|
document.createElement("br");
|
2022-12-16 19:59:30 -06:00
|
|
|
state.ctxmenu.keepUnmaskedBlurSliderLinebreak.classList.add(
|
2022-12-15 08:51:18 -06:00
|
|
|
"invisible"
|
|
|
|
);
|
|
|
|
|
2022-12-19 18:51:29 -06:00
|
|
|
// Preserve Brushed Masks Checkbox
|
|
|
|
state.ctxmenu.preserveMasksLabel = _toolbar_input.checkbox(
|
|
|
|
state,
|
|
|
|
"preserveMasks",
|
|
|
|
"Preserve Brushed Masks"
|
|
|
|
).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-12-16 19:59:30 -06:00
|
|
|
menu.appendChild(state.ctxmenu.keepUnmaskedLabel);
|
|
|
|
menu.appendChild(state.ctxmenu.keepUnmaskedBlurSlider);
|
|
|
|
menu.appendChild(state.ctxmenu.keepUnmaskedBlurSliderLinebreak);
|
2022-12-19 18:51:29 -06:00
|
|
|
menu.appendChild(state.ctxmenu.preserveMasksLabel);
|
|
|
|
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"});
|
|
|
|
};
|
2022-12-13 19:30:40 -06:00
|
|
|
|
2022-12-15 09:51:08 -06:00
|
|
|
const sendSeed = (seed) => {
|
|
|
|
stableDiffusionData.seed = document.getElementById("seed").value = seed;
|
|
|
|
};
|