Spaces:
Runtime error
Runtime error
cleanup
Browse files- frontend/src/lib/App.svelte +13 -73
- frontend/src/lib/Canvas.svelte +3 -3
- frontend/src/lib/PromptModal.svelte +15 -0
frontend/src/lib/App.svelte
CHANGED
@@ -39,6 +39,9 @@
|
|
39 |
isPrompting: false,
|
40 |
currentPrompt: ''
|
41 |
});
|
|
|
|
|
|
|
42 |
function getKey({ position }: PromptImgObject): PromptImgKey {
|
43 |
return `${position.x}_${position.y}`;
|
44 |
}
|
@@ -85,80 +88,15 @@
|
|
85 |
|
86 |
return base64Crop;
|
87 |
}
|
88 |
-
|
89 |
-
function getImageMask(cursor: { x: number; y: number }) {
|
90 |
-
const tempCanvas = document.createElement('canvas');
|
91 |
-
const canvasCrop = document.createElement('canvas');
|
92 |
-
const mask = document.createElement('canvas');
|
93 |
-
|
94 |
-
tempCanvas.width = 512;
|
95 |
-
tempCanvas.height = 512;
|
96 |
-
canvasCrop.width = 512;
|
97 |
-
canvasCrop.height = 512;
|
98 |
-
mask.width = 512;
|
99 |
-
mask.height = 512;
|
100 |
-
|
101 |
-
const tempCanvasCtx = tempCanvas.getContext('2d') as CanvasRenderingContext2D;
|
102 |
-
const ctxCrop = canvasCrop.getContext('2d') as CanvasRenderingContext2D;
|
103 |
-
const ctxMask = mask.getContext('2d') as CanvasRenderingContext2D;
|
104 |
-
|
105 |
-
// crop image from point canvas
|
106 |
-
ctxCrop.save();
|
107 |
-
ctxCrop.clearRect(0, 0, 512, 512);
|
108 |
-
|
109 |
-
ctxCrop.globalCompositeOperation = 'source-over';
|
110 |
-
ctxCrop.drawImage(canvasEl, cursor.x, cursor.y, 512, 512, 0, 0, 512, 512);
|
111 |
-
ctxCrop.restore();
|
112 |
-
|
113 |
-
// create black image
|
114 |
-
tempCanvasCtx.fillStyle = 'white';
|
115 |
-
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
116 |
-
|
117 |
-
// create Mask
|
118 |
-
ctxMask.save();
|
119 |
-
// ctxMask.clearRect(0, 0, 512, 512);
|
120 |
-
ctxMask.drawImage(canvasCrop, 0, 0, 512, 512);
|
121 |
-
ctxMask.globalCompositeOperation = 'source-out';
|
122 |
-
ctxMask.drawImage(tempCanvas, 0, 0);
|
123 |
-
ctxMask.restore();
|
124 |
-
|
125 |
-
tempCanvasCtx.save();
|
126 |
-
tempCanvasCtx.fillStyle = 'white';
|
127 |
-
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
128 |
-
//random pixels
|
129 |
-
// tempCanvasCtx.filter = 'blur(4px)';
|
130 |
-
|
131 |
-
// const imageData = tempCanvasCtx.getImageData(0, 0, 512, 512);
|
132 |
-
// const pix = imageData.data;
|
133 |
-
// for (let i = 0, n = pix.length; i < n; i += 4) {
|
134 |
-
// pix[i] = Math.round(255 * Math.random());
|
135 |
-
// pix[i + 1] = Math.round(255 * Math.random());
|
136 |
-
// pix[i + 2] = Math.round(255 * Math.random());
|
137 |
-
// pix[i + 3] = 255;
|
138 |
-
// }
|
139 |
-
// tempCanvasCtx.putImageData(imageData, 0, 0);
|
140 |
-
tempCanvasCtx.drawImage(canvasCrop, 0, 0, 512, 512);
|
141 |
-
//convert canvas to base64
|
142 |
-
const base64Crop = tempCanvas.toDataURL('image/png');
|
143 |
-
tempCanvasCtx.restore();
|
144 |
-
|
145 |
-
tempCanvasCtx.save();
|
146 |
-
tempCanvasCtx.fillStyle = 'black';
|
147 |
-
tempCanvasCtx.fillRect(0, 0, 512, 512);
|
148 |
-
tempCanvasCtx.drawImage(mask, 0, 0, 512, 512);
|
149 |
-
//convert canvas to base64
|
150 |
-
const base64Mask = tempCanvas.toDataURL('image/png');
|
151 |
-
tempCanvasCtx.restore();
|
152 |
-
|
153 |
-
return { image: base64Crop, mask: base64Mask };
|
154 |
-
}
|
155 |
-
|
156 |
async function generateImage(_prompt: string) {
|
157 |
-
// getImageMask($clickedPosition);
|
158 |
-
// return;
|
159 |
if (!_prompt || $isLoading == true) return;
|
160 |
$loadingState = 'Pending';
|
161 |
$isLoading = true;
|
|
|
|
|
|
|
|
|
|
|
162 |
const sessionHash = crypto.randomUUID();
|
163 |
const payload = {
|
164 |
fn_index: 0,
|
@@ -237,6 +175,7 @@
|
|
237 |
};
|
238 |
}
|
239 |
let modal = false;
|
|
|
240 |
</script>
|
241 |
|
242 |
<!-- Show the current user's cursor location -->
|
@@ -265,7 +204,7 @@
|
|
265 |
<Frame color={COLORS[0]} position={$clickedPosition} transform={$currZoomTransform} />
|
266 |
{/if} -->
|
267 |
{#if $myPresence?.cursor}
|
268 |
-
<Frame color={COLORS[0]} position={$myPresence.cursor} transform={$currZoomTransform} />
|
269 |
<Cursor
|
270 |
emoji={EMOJIS[0]}
|
271 |
color={COLORS[0]}
|
@@ -277,14 +216,15 @@
|
|
277 |
<!-- When others connected, iterate through others and show their cursors -->
|
278 |
{#if $others}
|
279 |
{#each [...$others] as { connectionId, presence } (connectionId)}
|
280 |
-
{#if presence?.cursor}
|
281 |
<Frame
|
282 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
283 |
position={presence?.cursor}
|
284 |
prompt={presence?.currentPrompt}
|
285 |
transform={$currZoomTransform}
|
286 |
/>
|
287 |
-
|
|
|
288 |
<Cursor
|
289 |
emoji={EMOJIS[1 + (connectionId % (EMOJIS.length - 1))]}
|
290 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
|
|
39 |
isPrompting: false,
|
40 |
currentPrompt: ''
|
41 |
});
|
42 |
+
$: {
|
43 |
+
console.log($others)
|
44 |
+
}
|
45 |
function getKey({ position }: PromptImgObject): PromptImgKey {
|
46 |
return `${position.x}_${position.y}`;
|
47 |
}
|
|
|
88 |
|
89 |
return base64Crop;
|
90 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
async function generateImage(_prompt: string) {
|
|
|
|
|
92 |
if (!_prompt || $isLoading == true) return;
|
93 |
$loadingState = 'Pending';
|
94 |
$isLoading = true;
|
95 |
+
myPresence.update({
|
96 |
+
currentPrompt: _prompt,
|
97 |
+
isPrompting: true,
|
98 |
+
cursor: $clickedPosition
|
99 |
+
});
|
100 |
const sessionHash = crypto.randomUUID();
|
101 |
const payload = {
|
102 |
fn_index: 0,
|
|
|
175 |
};
|
176 |
}
|
177 |
let modal = false;
|
178 |
+
let currentPrompt = '';
|
179 |
</script>
|
180 |
|
181 |
<!-- Show the current user's cursor location -->
|
|
|
204 |
<Frame color={COLORS[0]} position={$clickedPosition} transform={$currZoomTransform} />
|
205 |
{/if} -->
|
206 |
{#if $myPresence?.cursor}
|
207 |
+
<!-- <Frame color={COLORS[0]} position={$myPresence.cursor} transform={$currZoomTransform} /> -->
|
208 |
<Cursor
|
209 |
emoji={EMOJIS[0]}
|
210 |
color={COLORS[0]}
|
|
|
216 |
<!-- When others connected, iterate through others and show their cursors -->
|
217 |
{#if $others}
|
218 |
{#each [...$others] as { connectionId, presence } (connectionId)}
|
219 |
+
{#if presence?.isPrompting && presence?.cursor}
|
220 |
<Frame
|
221 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
222 |
position={presence?.cursor}
|
223 |
prompt={presence?.currentPrompt}
|
224 |
transform={$currZoomTransform}
|
225 |
/>
|
226 |
+
{/if}
|
227 |
+
{#if presence?.cursor}
|
228 |
<Cursor
|
229 |
emoji={EMOJIS[1 + (connectionId % (EMOJIS.length - 1))]}
|
230 |
color={COLORS[1 + (connectionId % (COLORS.length - 1))]}
|
frontend/src/lib/Canvas.svelte
CHANGED
@@ -128,9 +128,9 @@
|
|
128 |
|
129 |
// When the pointer leaves the page, set cursor presence to null
|
130 |
function handlePointerLeave() {
|
131 |
-
myPresence.update({
|
132 |
-
|
133 |
-
});
|
134 |
}
|
135 |
</script>
|
136 |
|
|
|
128 |
|
129 |
// When the pointer leaves the page, set cursor presence to null
|
130 |
function handlePointerLeave() {
|
131 |
+
// myPresence.update({
|
132 |
+
// cursor: null
|
133 |
+
// });
|
134 |
}
|
135 |
</script>
|
136 |
|
frontend/src/lib/PromptModal.svelte
CHANGED
@@ -1,11 +1,26 @@
|
|
1 |
<script lang="ts">
|
2 |
import { createEventDispatcher, onMount } from 'svelte';
|
|
|
|
|
3 |
const dispatch = createEventDispatcher();
|
4 |
let prompt: string;
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
const onKeyup = (e: KeyboardEvent) => {
|
7 |
if (e.key === 'Escape') {
|
8 |
dispatch('close');
|
|
|
|
|
|
|
|
|
9 |
}
|
10 |
};
|
11 |
onMount(() => {
|
|
|
1 |
<script lang="ts">
|
2 |
import { createEventDispatcher, onMount } from 'svelte';
|
3 |
+
import { useMyPresence } from '$lib/liveblocks';
|
4 |
+
|
5 |
const dispatch = createEventDispatcher();
|
6 |
let prompt: string;
|
7 |
|
8 |
+
const myPresence = useMyPresence();
|
9 |
+
|
10 |
+
$: {
|
11 |
+
myPresence.update({
|
12 |
+
currentPrompt: prompt,
|
13 |
+
isPrompting: true
|
14 |
+
});
|
15 |
+
}
|
16 |
+
|
17 |
const onKeyup = (e: KeyboardEvent) => {
|
18 |
if (e.key === 'Escape') {
|
19 |
dispatch('close');
|
20 |
+
myPresence.update({
|
21 |
+
currentPrompt: '',
|
22 |
+
isPrompting: false
|
23 |
+
});
|
24 |
}
|
25 |
};
|
26 |
onMount(() => {
|