mishig HF staff commited on
Commit
0dfc33a
1 Parent(s): 847ea5c

better img upload

Browse files
Files changed (1) hide show
  1. src/routes/+page.svelte +15 -6
src/routes/+page.svelte CHANGED
@@ -4,6 +4,7 @@
4
  let txt = '';
5
  let isLoading = false;
6
  let isOutputControlAdded = false;
 
7
  let canvas: HTMLCanvasElement;
8
  let ctx: CanvasRenderingContext2D | null;
9
  let noiseTs: DOMHighResTimeStamp;
@@ -14,7 +15,6 @@
14
  const animImageDuration = 500 as const;
15
  const animNoiseDuration = 3000 as const;
16
  let canvasSize = 400;
17
- let containerEl: HTMLDivElement;
18
  let canvasContainerEl: HTMLDivElement;
19
  let fileInput: HTMLInputElement;
20
  let sketchEl: HTMLCanvasElement;
@@ -188,11 +188,17 @@
188
  context!.drawImage(canvas, 0, 0);
189
  }
190
 
191
- function onChange() {
192
  const file = fileInput.files?.[0];
193
  if (file) {
194
- // todo: upload file
195
- // onSelectFile(file);
 
 
 
 
 
 
196
  }
197
  }
198
 
@@ -204,16 +210,19 @@
204
  sketchEl.style.width = `${canvasSize}px`;
205
  sketchEl.style.height = `${canvasSize}px`;
206
  await tick();
207
- const drawingBoard = new window.DrawingBoard.Board('board-container', {
208
  size: 10,
209
  controls: ['Color', { Size: { type: 'dropdown' } }, { DrawingMode: { filler: false } }],
210
  droppable: true,
 
211
  webStorage: false,
212
  enlargeYourContainer: true
213
  });
214
  canvas = drawingBoard.canvas;
215
  ctx = canvas.getContext('2d');
216
  copySketch();
 
 
217
  });
218
  </script>
219
 
@@ -229,7 +238,7 @@
229
  src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>
230
  </svelte:head>
231
 
232
- <div bind:this={containerEl} class="flex flex-wrap gap-x-4 gap-y-2 justify-center my-8">
233
  <canvas
234
  class="border-[1.2px] desktop:mt-[34px] {!isShowSketch && false ? 'hidden' : ''}"
235
  bind:this={sketchEl}
 
4
  let txt = '';
5
  let isLoading = false;
6
  let isOutputControlAdded = false;
7
+ let drawingBoard: any;
8
  let canvas: HTMLCanvasElement;
9
  let ctx: CanvasRenderingContext2D | null;
10
  let noiseTs: DOMHighResTimeStamp;
 
15
  const animImageDuration = 500 as const;
16
  const animNoiseDuration = 3000 as const;
17
  let canvasSize = 400;
 
18
  let canvasContainerEl: HTMLDivElement;
19
  let fileInput: HTMLInputElement;
20
  let sketchEl: HTMLCanvasElement;
 
188
  context!.drawImage(canvas, 0, 0);
189
  }
190
 
191
+ async function onChange() {
192
  const file = fileInput.files?.[0];
193
  if (file) {
194
+ const imgEl = new Image();
195
+ imgEl.src = URL.createObjectURL(file);
196
+ // await image.onload
197
+ await new Promise((resolve, _) => {
198
+ imgEl.onload = () => resolve(imgEl);
199
+ });
200
+ const { width, height } = imgEl;
201
+ ctx?.drawImage(imgEl, 0, 0, width, height, 0, 0, canvasSize, canvasSize);
202
  }
203
  }
204
 
 
210
  sketchEl.style.width = `${canvasSize}px`;
211
  sketchEl.style.height = `${canvasSize}px`;
212
  await tick();
213
+ drawingBoard = new window.DrawingBoard.Board('board-container', {
214
  size: 10,
215
  controls: ['Color', { Size: { type: 'dropdown' } }, { DrawingMode: { filler: false } }],
216
  droppable: true,
217
+ stretchImg: true,
218
  webStorage: false,
219
  enlargeYourContainer: true
220
  });
221
  canvas = drawingBoard.canvas;
222
  ctx = canvas.getContext('2d');
223
  copySketch();
224
+
225
+ console.log(drawingBoard._onCanvasDrop);
226
  });
227
  </script>
228
 
 
238
  src="https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/4.3.1/iframeResizer.contentWindow.min.js"></script>
239
  </svelte:head>
240
 
241
+ <div class="flex flex-wrap gap-x-4 gap-y-2 justify-center my-8">
242
  <canvas
243
  class="border-[1.2px] desktop:mt-[34px] {!isShowSketch && false ? 'hidden' : ''}"
244
  bind:this={sketchEl}