terminalizer/render/index.js

101 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-07-22 22:34:35 +00:00
/**
* Render the frames into PNG images
* An electron app, takes one command line argument `step`
*
2018-07-22 22:34:35 +00:00
* @author Mohammad Fares <faressoft.com@gmail.com>
*/
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
let mainWindow = null;
2018-07-22 22:34:35 +00:00
/**
* The temporary rendering directory's path
* @type {String}
*/
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-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
mainWindow = new BrowserWindow({
show: false,
width: 8000,
2019-11-20 19:16:06 +00:00
height: 8000,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
},
});
2018-07-22 22:34:35 +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:
* 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
*/
ipcMain.handle('capturePage', async function (event, captureRect, frameIndex) {
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
/**
* 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
*
* @param {Object} event
* @param {String} error
2018-07-22 22:34:35 +00:00
*/
ipcMain.on('error', function (event, error) {
process.stderr.write(error);
2018-07-22 22:34:35 +00:00
});