2018-07-22 22:34:35 +00:00
|
|
|
/**
|
|
|
|
* Render the frames into PNG images
|
|
|
|
* An electron app, takes one command line argument `step`
|
2022-09-05 18:54:57 +00:00
|
|
|
*
|
2018-07-22 22:34:35 +00:00
|
|
|
* @author Mohammad Fares <faressoft.com@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2022-09-05 18:54:57 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const { app } = require('electron');
|
|
|
|
const { BrowserWindow } = require('electron');
|
|
|
|
const ipcMain = require('electron').ipcMain;
|
|
|
|
const os = require('os');
|
2018-07-22 22:34:35 +00:00
|
|
|
|
2022-09-05 18:54:57 +00:00
|
|
|
let mainWindow = null;
|
2018-07-22 22:34:35 +00:00
|
|
|
|
2018-11-04 16:16:32 +00:00
|
|
|
/**
|
|
|
|
* The temporary rendering directory's path
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2022-09-05 18:54:57 +00:00
|
|
|
var renderDir = path.join(__dirname, 'frames');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The step option
|
|
|
|
* To reduce the number of rendered frames (step > 1)
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
|
|
|
var step = process.argv[2] || 1;
|
2018-11-04 16:16:32 +00:00
|
|
|
|
2018-07-24 08:00:21 +00:00
|
|
|
// Hide the Dock for macOS
|
|
|
|
if (os.platform() == 'darwin') {
|
|
|
|
app.dock.hide();
|
|
|
|
}
|
|
|
|
|
2018-07-22 22:34:35 +00:00
|
|
|
// When the app is ready
|
|
|
|
app.on('ready', createWindow);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a hidden browser window and load the rendering page
|
|
|
|
*/
|
|
|
|
function createWindow() {
|
|
|
|
// Create a browser window
|
2022-09-05 18:54:57 +00:00
|
|
|
mainWindow = new BrowserWindow({
|
2018-11-05 17:07:20 +00:00
|
|
|
show: false,
|
|
|
|
width: 8000,
|
2019-11-20 19:16:06 +00:00
|
|
|
height: 8000,
|
|
|
|
webPreferences: {
|
2022-09-05 18:54:57 +00:00
|
|
|
preload: path.join(__dirname, 'preload.js'),
|
|
|
|
},
|
2018-11-05 17:07:20 +00:00
|
|
|
});
|
2018-07-22 22:34:35 +00:00
|
|
|
|
2022-09-05 18:54:57 +00:00
|
|
|
// Load index.html
|
|
|
|
mainWindow.loadURL('file://' + __dirname + '/index.html');
|
2018-07-22 22:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback function for the event:
|
2022-09-05 18:54:57 +00:00
|
|
|
* getOptions to request the options that need
|
|
|
|
* to be passed to the renderer
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
*/
|
|
|
|
ipcMain.handle('getOptions', function () {
|
|
|
|
return { step };
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback function for the event:
|
|
|
|
* capturePage
|
|
|
|
*
|
2018-07-25 13:46:32 +00:00
|
|
|
* @param {Object} event
|
2018-07-22 22:34:35 +00:00
|
|
|
*/
|
2022-09-05 18:54:57 +00:00
|
|
|
ipcMain.handle('capturePage', async function (event, captureRect, frameIndex) {
|
2022-09-07 15:19:07 +00:00
|
|
|
// To show the cursor for headless browser
|
|
|
|
mainWindow.focusOnWebView();
|
2022-09-05 18:54:57 +00:00
|
|
|
const img = await mainWindow.webContents.capturePage(captureRect);
|
|
|
|
const outputPath = path.join(renderDir, frameIndex + '.png');
|
|
|
|
fs.writeFileSync(outputPath, img.toPNG());
|
|
|
|
console.log(frameIndex);
|
|
|
|
});
|
2018-07-22 22:34:35 +00:00
|
|
|
|
2022-09-05 18:54:57 +00:00
|
|
|
/**
|
|
|
|
* A callback function for the event:
|
|
|
|
* Close
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
* @param {String} error
|
|
|
|
*/
|
|
|
|
ipcMain.on('close', function (event, error) {
|
|
|
|
mainWindow.close();
|
2018-07-22 22:34:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A callback function for the event:
|
|
|
|
* When something unexpected happened
|
|
|
|
*
|
2018-11-04 16:16:32 +00:00
|
|
|
* @param {Object} event
|
|
|
|
* @param {String} error
|
2018-07-22 22:34:35 +00:00
|
|
|
*/
|
2022-09-05 18:54:57 +00:00
|
|
|
ipcMain.on('error', function (event, error) {
|
2018-11-04 16:16:32 +00:00
|
|
|
process.stderr.write(error);
|
2018-07-22 22:34:35 +00:00
|
|
|
});
|