bibibi12345 commited on
Commit
3426244
·
1 Parent(s): ed0fd8a

frontend gif debug

Browse files
Files changed (1) hide show
  1. index.html +119 -26
index.html CHANGED
@@ -433,7 +433,17 @@
433
  }
434
 
435
  function createAnimatedGif(imageDataArray, container) {
 
 
436
  try {
 
 
 
 
 
 
 
 
437
  const gif = new GIF({
438
  workers: 2,
439
  quality: 10,
@@ -445,42 +455,125 @@
445
  const totalImages = imageDataArray.length;
446
 
447
  // Show loading message for GIF creation
448
- container.innerHTML = '<p>Creating animated GIF...</p>';
 
 
 
 
 
 
 
 
449
 
450
  imageDataArray.forEach((imageData, index) => {
 
451
  const img = new Image();
452
- img.onload = function() {
453
- // Create a canvas to resize the image
454
- const canvas = document.createElement('canvas');
455
- const ctx = canvas.getContext('2d');
456
- canvas.width = 512;
457
- canvas.height = 512;
458
-
459
- // Draw and resize image
460
- ctx.drawImage(img, 0, 0, 512, 512);
461
-
462
- // Add frame to GIF with 500ms delay
463
- gif.addFrame(canvas, {delay: 500});
464
-
465
  loadedImages++;
 
 
466
  if (loadedImages === totalImages) {
467
- // All images loaded, render GIF
468
- gif.on('finished', function(blob) {
469
- const url = URL.createObjectURL(blob);
470
- const gifImg = document.createElement('img');
471
- gifImg.src = url;
472
- gifImg.className = 'generated-image';
473
- gifImg.alt = 'Generated animated GIF';
474
-
475
- container.innerHTML = '';
476
- container.appendChild(gifImg);
477
- });
 
 
 
 
478
 
479
- gif.render();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  }
481
  };
 
482
  img.src = `data:image/png;base64,${imageData}`;
483
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
  } catch (error) {
485
  console.error('Error creating GIF:', error);
486
  // Fallback to showing individual images
 
433
  }
434
 
435
  function createAnimatedGif(imageDataArray, container) {
436
+ console.log('Starting GIF creation with', imageDataArray.length, 'images');
437
+
438
  try {
439
+ // Check if GIF library is available
440
+ if (typeof GIF === 'undefined') {
441
+ console.error('GIF library not available');
442
+ showMultipleImages(imageDataArray, container);
443
+ return;
444
+ }
445
+
446
+ console.log('GIF library available, creating GIF instance');
447
  const gif = new GIF({
448
  workers: 2,
449
  quality: 10,
 
455
  const totalImages = imageDataArray.length;
456
 
457
  // Show loading message for GIF creation
458
+ container.innerHTML = '<p>Creating animated GIF... (0/' + totalImages + ' images loaded)</p>';
459
+
460
+ // Set a timeout to prevent infinite waiting
461
+ const timeout = setTimeout(() => {
462
+ console.error('GIF creation timeout after 30 seconds');
463
+ showMultipleImages(imageDataArray, container);
464
+ }, 30000);
465
+
466
+ console.log('Processing', totalImages, 'images for GIF');
467
 
468
  imageDataArray.forEach((imageData, index) => {
469
+ console.log('Loading image', index + 1, 'of', totalImages);
470
  const img = new Image();
471
+
472
+ img.onerror = function() {
473
+ console.error('Failed to load image', index + 1);
 
 
 
 
 
 
 
 
 
 
474
  loadedImages++;
475
+ container.innerHTML = '<p>Creating animated GIF... (' + loadedImages + '/' + totalImages + ' images loaded)</p>';
476
+
477
  if (loadedImages === totalImages) {
478
+ clearTimeout(timeout);
479
+ console.log('All images processed (some failed), starting GIF render');
480
+ startGifRender();
481
+ }
482
+ };
483
+
484
+ img.onload = function() {
485
+ console.log('Image', index + 1, 'loaded successfully, size:', img.width, 'x', img.height);
486
+
487
+ try {
488
+ // Create a canvas to resize the image
489
+ const canvas = document.createElement('canvas');
490
+ const ctx = canvas.getContext('2d');
491
+ canvas.width = 512;
492
+ canvas.height = 512;
493
 
494
+ // Draw and resize image
495
+ ctx.drawImage(img, 0, 0, 512, 512);
496
+ console.log('Image', index + 1, 'drawn to canvas');
497
+
498
+ // Add frame to GIF with 500ms delay
499
+ gif.addFrame(canvas, {delay: 500});
500
+ console.log('Frame', index + 1, 'added to GIF');
501
+
502
+ loadedImages++;
503
+ container.innerHTML = '<p>Creating animated GIF... (' + loadedImages + '/' + totalImages + ' images loaded)</p>';
504
+
505
+ if (loadedImages === totalImages) {
506
+ clearTimeout(timeout);
507
+ console.log('All images loaded, starting GIF render');
508
+ startGifRender();
509
+ }
510
+ } catch (canvasError) {
511
+ console.error('Error processing image', index + 1, ':', canvasError);
512
+ loadedImages++;
513
+ if (loadedImages === totalImages) {
514
+ clearTimeout(timeout);
515
+ startGifRender();
516
+ }
517
  }
518
  };
519
+
520
  img.src = `data:image/png;base64,${imageData}`;
521
  });
522
+
523
+ function startGifRender() {
524
+ console.log('Starting GIF render process');
525
+ container.innerHTML = '<p>Rendering animated GIF...</p>';
526
+
527
+ gif.on('start', function() {
528
+ console.log('GIF rendering started');
529
+ });
530
+
531
+ gif.on('progress', function(p) {
532
+ console.log('GIF rendering progress:', Math.round(p * 100) + '%');
533
+ container.innerHTML = '<p>Rendering animated GIF... ' + Math.round(p * 100) + '%</p>';
534
+ });
535
+
536
+ gif.on('finished', function(blob) {
537
+ console.log('GIF rendering finished, blob size:', blob.size, 'bytes');
538
+ try {
539
+ const url = URL.createObjectURL(blob);
540
+ const gifImg = document.createElement('img');
541
+ gifImg.src = url;
542
+ gifImg.className = 'generated-image';
543
+ gifImg.alt = 'Generated animated GIF';
544
+
545
+ container.innerHTML = '';
546
+ container.appendChild(gifImg);
547
+ console.log('GIF successfully displayed');
548
+ } catch (displayError) {
549
+ console.error('Error displaying GIF:', displayError);
550
+ showMultipleImages(imageDataArray, container);
551
+ }
552
+ });
553
+
554
+ gif.on('abort', function() {
555
+ console.error('GIF rendering was aborted');
556
+ showMultipleImages(imageDataArray, container);
557
+ });
558
+
559
+ // Set another timeout for the rendering process
560
+ const renderTimeout = setTimeout(() => {
561
+ console.error('GIF rendering timeout after 60 seconds');
562
+ gif.abort();
563
+ showMultipleImages(imageDataArray, container);
564
+ }, 60000);
565
+
566
+ gif.on('finished', function() {
567
+ clearTimeout(renderTimeout);
568
+ });
569
+
570
+ gif.on('abort', function() {
571
+ clearTimeout(renderTimeout);
572
+ });
573
+
574
+ gif.render();
575
+ }
576
+
577
  } catch (error) {
578
  console.error('Error creating GIF:', error);
579
  // Fallback to showing individual images