radames commited on
Commit
8c259b0
β€’
1 Parent(s): 99eaa71

efficient way to handle images already on canvas

Browse files
frontend/src/lib/App.svelte CHANGED
@@ -81,7 +81,7 @@
81
  };
82
 
83
  const datapayload = {
84
- data: [base64Crop, prompt, 0.75, 7.5, 35, 'patchmatch']
85
  };
86
 
87
  const websocket = new WebSocket(PUBLIC_WS_INPAINTING);
 
81
  };
82
 
83
  const datapayload = {
84
+ data: [base64Crop, prompt, 0.75, 7.5, 40, 'patchmatch']
85
  };
86
 
87
  const websocket = new WebSocket(PUBLIC_WS_INPAINTING);
frontend/src/lib/PaintCanvas.svelte CHANGED
@@ -7,7 +7,7 @@
7
 
8
  import { useMyPresence, useObject } from '$lib/liveblocks';
9
  import type { PromptImgObject } from '$lib/types';
10
- import { CANVAS_SIZE } from '$lib/constants';
11
 
12
  const myPresence = useMyPresence();
13
  const promptImgStorage = useObject('promptImgStorage');
@@ -18,13 +18,34 @@
18
  let containerEl: HTMLDivElement;
19
  let canvasCtx: CanvasRenderingContext2D;
20
 
21
- // keep track of images already rendered
22
  const imagesOnCanvas = new Set();
23
 
24
  function getpromptImgList(promptImgList: PromptImgObject[]): PromptImgObject[] {
25
  if (promptImgList) {
26
- const list: PromptImgObject[] = Object.values(promptImgList).sort((a, b) => a.date - b.date);
27
- return list.filter(({ id }) => !imagesOnCanvas.has(id));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  }
29
  return [];
30
  }
 
7
 
8
  import { useMyPresence, useObject } from '$lib/liveblocks';
9
  import type { PromptImgObject } from '$lib/types';
10
+ import { CANVAS_SIZE, FRAME_SIZE, GRID_SIZE } from '$lib/constants';
11
 
12
  const myPresence = useMyPresence();
13
  const promptImgStorage = useObject('promptImgStorage');
 
18
  let containerEl: HTMLDivElement;
19
  let canvasCtx: CanvasRenderingContext2D;
20
 
 
21
  const imagesOnCanvas = new Set();
22
 
23
  function getpromptImgList(promptImgList: PromptImgObject[]): PromptImgObject[] {
24
  if (promptImgList) {
25
+ //sorted by last updated
26
+ const canvasPixels = new Map();
27
+ for (const x of Array.from(Array(width / GRID_SIZE).keys())) {
28
+ for (const y of Array.from(Array(height / GRID_SIZE).keys())) {
29
+ canvasPixels.set(`${x * GRID_SIZE}_${y * GRID_SIZE}`, null);
30
+ }
31
+ }
32
+ const list: PromptImgObject[] = Object.values(promptImgList).sort((a, b) => b.date - a.date);
33
+ // init
34
+ for (const promptImg of list) {
35
+ const x = promptImg.position.x;
36
+ const y = promptImg.position.y;
37
+ for (const i of [...Array(FRAME_SIZE / GRID_SIZE).keys()]) {
38
+ for (const j of [...Array(FRAME_SIZE / GRID_SIZE).keys()]) {
39
+ const key = `${x + i * GRID_SIZE}_${y + j * GRID_SIZE}`;
40
+ if (!canvasPixels.get(key)) {
41
+ canvasPixels.set(key, promptImg.id);
42
+ }
43
+ }
44
+ }
45
+ }
46
+ const ids = new Set([...canvasPixels.values()]);
47
+ const filteredImages = list.filter((promptImg) => ids.has(promptImg.id));
48
+ return filteredImages.reverse().filter((promptImg) => !imagesOnCanvas.has(promptImg.id));
49
  }
50
  return [];
51
  }
frontend/src/lib/constants.ts CHANGED
@@ -17,8 +17,8 @@ export const EMOJIS = ['🐝', '🐌', '🐞', '🐜', 'πŸ¦‹', 'πŸ›', '🐝', '
17
  export const MAX_CAPACITY = 20;
18
 
19
  export const CANVAS_SIZE = {
20
- width: 512 * 10,
21
- height: 512 * 10,
22
  }
23
  export const GRID_SIZE = 32
24
 
 
17
  export const MAX_CAPACITY = 20;
18
 
19
  export const CANVAS_SIZE = {
20
+ width: 512 * 6,
21
+ height: 512 * 6,
22
  }
23
  export const GRID_SIZE = 32
24