-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgenerate-gif.js
More file actions
94 lines (76 loc) · 3.28 KB
/
generate-gif.js
File metadata and controls
94 lines (76 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const puppeteer = require('puppeteer');
const fs = require('fs');
const GIFEncoder = require('gifencoder');
const { createCanvas, loadImage } = require('canvas');
async function generateDashboardGIF() {
console.log('🎬 Starting animated dashboard GIF generation...');
const browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const page = await browser.newPage();
await page.setViewport({ width: 800, height: 400 });
// Load the dashboard HTML
await page.goto('file://' + process.cwd() + '/dashboard.html', {
waitUntil: 'networkidle2'
});
// Wait for animations to initialize
console.log('⏳ Waiting for animations to start...');
await new Promise(resolve => setTimeout(resolve, 3000));
// Setup GIF encoder
console.log('🎞️ Setting up GIF encoder...');
const encoder = new GIFEncoder(800, 400);
const stream = fs.createWriteStream('dashboard.gif');
encoder.createWriteStream(stream);
encoder.start();
encoder.setRepeat(0); // 0 for infinite loop
encoder.setDelay(100); // 100ms = 10 FPS
encoder.setQuality(10); // Image quality (1-20, lower is better)
// Capture frames for animation
console.log('📸 Capturing animation frames...');
const totalFrames = 60; // 6 seconds at 10 FPS
for (let i = 0; i < totalFrames; i++) {
// Take screenshot
const screenshot = await page.screenshot({
type: 'png',
clip: { x: 0, y: 0, width: 800, height: 400 }
});
// Convert to canvas and add to GIF
const img = await loadImage(screenshot);
const canvas = createCanvas(800, 400);
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
encoder.addFrame(ctx);
// Progress indicator
const progress = Math.round((i + 1) / totalFrames * 100);
console.log(`📸 Frame ${i + 1}/${totalFrames} (${progress}%)`);
// Wait between frames to capture animation
await new Promise(resolve => setTimeout(resolve, 100));
}
// Finish GIF creation
encoder.finish();
console.log('GIF creation completed!');
// Also create a high-quality static PNG
console.log('📱 Creating PNG fallback...');
await page.screenshot({
path: 'dashboard.png',
type: 'png',
clip: { x: 0, y: 0, width: 800, height: 400 }
});
console.log('🎉 Dashboard images generated successfully!');
console.log('📁 Files created:');
console.log(' - dashboard.gif (animated)');
console.log(' - dashboard.png (static fallback)');
} catch (error) {
console.error('Error generating dashboard GIF:', error);
throw error;
} finally {
await browser.close();
}
}
// Run if called directly
if (require.main === module) {
generateDashboardGIF().catch(console.error);
}
module.exports = generateDashboardGIF;