Skip to content

Instantly share code, notes, and snippets.

@10io

10io/main.js Secret

Created January 26, 2016 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 10io/bbec2fe0d71fc308cf60 to your computer and use it in GitHub Desktop.
Save 10io/bbec2fe0d71fc308cf60 to your computer and use it in GitHub Desktop.
Electron screenshot
'use strict';
const electron = require('electron');
const fs = require('fs');
const app = electron.app; // Module to control application life.
const BrowserWindow = electron.BrowserWindow; // Module to create native browser window.
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
var mainWindow = null;
// Quit when all windows are closed.
app.on('window-all-closed', function () {
app.quit();
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', function () {
var geometry = process.argv[2].split('x');
var width = parseInt(geometry[0], 10);
var height = parseInt(geometry[1], 10);
var htmlPath = process.argv[3];
var imagePath = process.argv[4];
var wait = process.argv[5] || 3;
// Create the browser window.
mainWindow = new BrowserWindow({
width: width,
height: height,
fullscreen: true,
webPreferences: {
nodeIntegration: false,
webSecurity: false,
webaudio: false
}
});
mainWindow.webContents.on('did-finish-load', function () {
console.log('Page loaded');
setTimeout(function () {
console.log('Capturing screenshot...');
mainWindow.capturePage({x: 0, y: 0, width: width, height: height}, function (imageBuffer) {
console.log('Got screenshot. Empty? ' + imageBuffer.isEmpty());
fs.writeFile(imagePath, imageBuffer.toPng(), function (err) {
console.log('Done.');
mainWindow.close();
});
});
}, wait * 1000);
});
// and load the index.html of the app.
mainWindow.loadURL(htmlPath);
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
});
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<style>
* {
margin: 0;
padding: 0;
}
.floating {
position: absolute;
}
</style>
</head>
<body>
<div style="width: 800px; height:600px; background-image: url('https://placeholdit.imgix.net/~text?txtsize=33&txt=BANANAS&w=800&h=600'); background-size: cover; background-repeat: no-repeat;"></div>
<div class="floating" style="top: 10px; left: 10px; width: 380px; height: 190px; background-image: url('https://placehold.it/380x190/ffffff/000000'); background-size: contain; background-repeat: no-repeat; background-position:center;"></div>
<div style='color: #000; font-family: Times New Roman; font-size: 42pt; font-weight: bold; font-style: italic; text-decoration: underline;'>
<span style='white-space: nowrap;'>Apples</span>
</div>
</body>
</html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
</head>
<body>
<div style='color: #000; font-family: Times New Roman; font-size: 42pt; font-weight: bold; font-style: italic; text-decoration: overline;'>
<span style='white-space: nowrap;'>BANANAS</span>
</div>
<div style='color: #000; font-family: Times New Roman; font-size: 42pt; font-weight: bold; font-style: italic; text-decoration: line-through;'>
<span style='white-space: nowrap;'>BANANAS</span>
</div>
<div style='color: #000; font-family: Times New Roman; font-size: 42pt; font-weight: bold; font-style: italic; text-decoration: underline;'>
<span style='white-space: nowrap;'>BANANAS</span>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment