Spaces:
Runtime error
Runtime error
white background
Browse files- frontend/src/lib/App.svelte +30 -40
- frontend/src/lib/store.ts +1 -1
frontend/src/lib/App.svelte
CHANGED
@@ -27,8 +27,7 @@
|
|
27 |
*/
|
28 |
|
29 |
export let room: Room;
|
30 |
-
|
31 |
-
let CB: HTMLCanvasElement;
|
32 |
let canvasEl: HTMLCanvasElement;
|
33 |
|
34 |
onMount(() => {});
|
@@ -43,18 +42,18 @@
|
|
43 |
console.log('prompt', prompt, imgURLs);
|
44 |
}
|
45 |
function getImageMask(cursor: { x: number; y: number }) {
|
46 |
-
const
|
47 |
const canvasCrop = document.createElement('canvas');
|
48 |
const mask = document.createElement('canvas');
|
49 |
|
50 |
-
|
51 |
-
|
52 |
canvasCrop.width = 512;
|
53 |
canvasCrop.height = 512;
|
54 |
mask.width = 512;
|
55 |
mask.height = 512;
|
56 |
|
57 |
-
const
|
58 |
const ctxCrop = canvasCrop.getContext('2d') as CanvasRenderingContext2D;
|
59 |
const ctxMask = mask.getContext('2d') as CanvasRenderingContext2D;
|
60 |
|
@@ -62,49 +61,45 @@
|
|
62 |
ctxCrop.save();
|
63 |
ctxCrop.clearRect(0, 0, 512, 512);
|
64 |
|
65 |
-
|
66 |
-
const pix = imageData.data;
|
67 |
-
for (let i = 0, n = pix.length; i < n; i += 4) {
|
68 |
-
pix[i] = Math.round(255 * Math.random());
|
69 |
-
pix[i + 1] = Math.round(255 * Math.random());
|
70 |
-
pix[i + 2] = Math.round(255 * Math.random());
|
71 |
-
pix[i + 3] = 255;
|
72 |
-
}
|
73 |
-
// ctxCrop.putImageData(imageData, 0, 0);
|
74 |
ctxCrop.globalCompositeOperation = 'source-over';
|
75 |
ctxCrop.drawImage(canvasEl, cursor.x, cursor.y, 512, 512, 0, 0, 512, 512);
|
76 |
ctxCrop.restore();
|
77 |
|
78 |
// create black image
|
79 |
-
|
80 |
-
|
81 |
|
82 |
// create Mask
|
83 |
ctxMask.save();
|
84 |
// ctxMask.clearRect(0, 0, 512, 512);
|
85 |
ctxMask.drawImage(canvasCrop, 0, 0, 512, 512);
|
86 |
ctxMask.globalCompositeOperation = 'source-in';
|
87 |
-
ctxMask.drawImage(
|
88 |
ctxMask.restore();
|
89 |
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
//
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
//
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
contextB.restore();
|
104 |
-
|
105 |
//convert canvas to base64
|
106 |
-
const base64Crop =
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
return { image: base64Crop, mask: base64Mask };
|
109 |
}
|
110 |
|
@@ -201,11 +196,6 @@
|
|
201 |
{#if $isPrompting}
|
202 |
<PromptModal on:prompt={onPrompt} on:close={onClose} />
|
203 |
{/if}
|
204 |
-
|
205 |
-
<div class="flex">
|
206 |
-
<canvas bind:this={CA} width="512" height="512" />
|
207 |
-
<canvas bind:this={CB} width="512" height="512" />
|
208 |
-
</div>
|
209 |
<div class="fixed top-0 left-0 z-0 w-screen h-screen">
|
210 |
<Canvas bind:value={canvasEl} />
|
211 |
|
|
|
27 |
*/
|
28 |
|
29 |
export let room: Room;
|
30 |
+
|
|
|
31 |
let canvasEl: HTMLCanvasElement;
|
32 |
|
33 |
onMount(() => {});
|
|
|
42 |
console.log('prompt', prompt, imgURLs);
|
43 |
}
|
44 |
function getImageMask(cursor: { x: number; y: number }) {
|
45 |
+
const tempCanvas = document.createElement('canvas');
|
46 |
const canvasCrop = document.createElement('canvas');
|
47 |
const mask = document.createElement('canvas');
|
48 |
|
49 |
+
tempCanvas.width = 512;
|
50 |
+
tempCanvas.height = 512;
|
51 |
canvasCrop.width = 512;
|
52 |
canvasCrop.height = 512;
|
53 |
mask.width = 512;
|
54 |
mask.height = 512;
|
55 |
|
56 |
+
const tempCanvasCtx = tempCanvas.getContext('2d') as CanvasRenderingContext2D;
|
57 |
const ctxCrop = canvasCrop.getContext('2d') as CanvasRenderingContext2D;
|
58 |
const ctxMask = mask.getContext('2d') as CanvasRenderingContext2D;
|
59 |
|
|
|
61 |
ctxCrop.save();
|
62 |
ctxCrop.clearRect(0, 0, 512, 512);
|
63 |
|
64 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
ctxCrop.globalCompositeOperation = 'source-over';
|
66 |
ctxCrop.drawImage(canvasEl, cursor.x, cursor.y, 512, 512, 0, 0, 512, 512);
|
67 |
ctxCrop.restore();
|
68 |
|
69 |
// create black image
|
70 |
+
tempCanvasCtx.fillStyle = 'black';
|
71 |
+
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
72 |
|
73 |
// create Mask
|
74 |
ctxMask.save();
|
75 |
// ctxMask.clearRect(0, 0, 512, 512);
|
76 |
ctxMask.drawImage(canvasCrop, 0, 0, 512, 512);
|
77 |
ctxMask.globalCompositeOperation = 'source-in';
|
78 |
+
ctxMask.drawImage(tempCanvas, 0, 0);
|
79 |
ctxMask.restore();
|
80 |
|
81 |
+
tempCanvasCtx.fillStyle = 'white';
|
82 |
+
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
83 |
+
//random pixels
|
84 |
+
// const imageData = tempCanvasCtx.getImageData(0, 0, 512, 512);
|
85 |
+
// const pix = imageData.data;
|
86 |
+
// for (let i = 0, n = pix.length; i < n; i += 4) {
|
87 |
+
// pix[i] = Math.round(255 * Math.random());
|
88 |
+
// pix[i + 1] = Math.round(255 * Math.random());
|
89 |
+
// pix[i + 2] = Math.round(255 * Math.random());
|
90 |
+
// pix[i + 3] = 255;
|
91 |
+
// }
|
92 |
+
// tempCanvasCtx.putImageData(imageData, 0, 0);
|
93 |
+
tempCanvasCtx.drawImage(canvasCrop, 0, 0, 512, 512);
|
|
|
|
|
94 |
//convert canvas to base64
|
95 |
+
const base64Crop = tempCanvas.toDataURL('image/png');
|
96 |
+
|
97 |
+
tempCanvasCtx.fillStyle = 'white';
|
98 |
+
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
99 |
+
tempCanvasCtx.drawImage(mask, 0, 0, 512, 512);
|
100 |
+
//convert canvas to base64
|
101 |
+
const base64Mask = tempCanvas.toDataURL('image/png');
|
102 |
+
|
103 |
return { image: base64Crop, mask: base64Mask };
|
104 |
}
|
105 |
|
|
|
196 |
{#if $isPrompting}
|
197 |
<PromptModal on:prompt={onPrompt} on:close={onClose} />
|
198 |
{/if}
|
|
|
|
|
|
|
|
|
|
|
199 |
<div class="fixed top-0 left-0 z-0 w-screen h-screen">
|
200 |
<Canvas bind:value={canvasEl} />
|
201 |
|
frontend/src/lib/store.ts
CHANGED
@@ -8,7 +8,7 @@ export const isLoading = writable<boolean>(false);
|
|
8 |
export const isPrompting = writable<boolean>(false);
|
9 |
export const clickedPosition = writable<{ x: number; y: number }>();
|
10 |
export const showFrames = writable<boolean>(false);
|
11 |
-
export const text2img = writable<boolean>(
|
12 |
|
13 |
export const currZoomTransform = writable<ZoomTransform>(zoomIdentity);
|
14 |
|
|
|
8 |
export const isPrompting = writable<boolean>(false);
|
9 |
export const clickedPosition = writable<{ x: number; y: number }>();
|
10 |
export const showFrames = writable<boolean>(false);
|
11 |
+
export const text2img = writable<boolean>(false);
|
12 |
|
13 |
export const currZoomTransform = writable<ZoomTransform>(zoomIdentity);
|
14 |
|