5m4ck3r commited on
Commit
30daf55
·
verified ·
1 Parent(s): a5730df

Update static/js/script.js

Browse files
Files changed (1) hide show
  1. static/js/script.js +45 -2
static/js/script.js CHANGED
@@ -194,7 +194,21 @@ class Whiteboard {
194
  })
195
  .then(response => response.json())
196
  .then(data => {
197
- document.getElementById('result').innerText = data.message;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  })
199
  .catch(error => {
200
  console.error('Error:', error);
@@ -261,4 +275,33 @@ document.addEventListener('keydown', function(event) {
261
  link.click();
262
  console.log("Canvas saved as image with white background!");
263
  }
264
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  })
195
  .then(response => response.json())
196
  .then(data => {
197
+
198
+ if (data.status && Array.isArray(data.data)) {
199
+ data.data.forEach(item => {
200
+ const result = item.result;
201
+ const x = item.location.x;
202
+ const y = item.location.y;
203
+ const size = item.size;
204
+ const sizePx = `${size}px`;
205
+ writeOnCanvasDynamic(whiteboard.pages, result, x, y, null, "Arial", sizePx, "black", false);
206
+ });
207
+ } else {
208
+ console.error("Invalid data object or empty data array");
209
+ }
210
+
211
+ document.getElementById('result').innerText = data.text;
212
  })
213
  .catch(error => {
214
  console.error('Error:', error);
 
275
  link.click();
276
  console.log("Canvas saved as image with white background!");
277
  }
278
+ });
279
+
280
+ function writeOnCanvasDynamic(pagesInstance, text, x, y, fontUrl, fontFamily, fontSize = "16px", color = "black", clear = false) {
281
+ const ctx = pagesInstance.ctx;
282
+ if (!ctx) return;
283
+
284
+ const applyFontAndWrite = () => {
285
+ ctx.globalCompositeOperation = 'source-over';
286
+ if (clear) {
287
+ const textWidth = ctx.measureText(text).width;
288
+ const lineHeight = parseInt(fontSize, 10) || 16;
289
+ ctx.clearRect(x, y - lineHeight, textWidth, lineHeight);
290
+ return;
291
+ }
292
+ ctx.font = `${fontSize} ${fontFamily}`;
293
+ ctx.fillStyle = color;
294
+ ctx.fillText(text, x, y);
295
+ };
296
+ if (fontUrl) {
297
+ const fontFace = new FontFace(fontFamily, `url(${fontUrl})`);
298
+ fontFace.load().then((loadedFont) => {
299
+ document.fonts.add(loadedFont);
300
+ applyFontAndWrite();
301
+ }).catch((err) => {
302
+ console.error("Font loading failed:", err);
303
+ });
304
+ } else {
305
+ applyFontAndWrite();
306
+ }
307
+ }