4 changed files with 160 additions and 116 deletions
@ -1,124 +1,15 @@ |
|||
<!DOCTYPE html> |
|||
<html> |
|||
<head> |
|||
<title>Terminalizer</title> |
|||
<title>Renderer</title> |
|||
<meta charset="UTF-8"> |
|||
<link rel="stylesheet" href="../node_modules/xterm/dist/xterm.css"> |
|||
<link rel="stylesheet" href="../lib/terminalizer.css"> |
|||
|
|||
<style type="text/css"> |
|||
|
|||
body { |
|||
background-color: white; |
|||
margin: 0; |
|||
} |
|||
|
|||
#terminal { |
|||
display: inline-block; |
|||
font-size: 0px; |
|||
} |
|||
|
|||
</style> |
|||
<link rel="stylesheet" type="text/css" href="dist/css/app.css"> |
|||
<script src="dist/js/app.js" type="text/javascript"></script> |
|||
</head> |
|||
<body> |
|||
|
|||
<div id="terminal"></div> |
|||
|
|||
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script> |
|||
|
|||
<script src="../node_modules/xterm/dist/xterm.js"></script> |
|||
<script src="../node_modules/jquery/dist/jquery.min.js"></script> |
|||
<script src="../node_modules/async/dist/async.min.js"></script> |
|||
<script src="../lib/terminalizer.js"></script> |
|||
|
|||
<script type="text/javascript"> |
|||
|
|||
var fs = require('fs'), |
|||
path = require('path'), |
|||
remote = require('electron').remote, |
|||
ipcRenderer = require('electron').ipcRenderer; |
|||
var currentWindow = remote.getCurrentWindow(), |
|||
capturePage = currentWindow.webContents.capturePage, |
|||
step = remote.getGlobal('step'); |
|||
|
|||
/** |
|||
* Used for the step option |
|||
* @type {Number} |
|||
*/ |
|||
var stepsCounter = 0; |
|||
|
|||
/** |
|||
* Options for the terminalizer plugin |
|||
* @type {Object} |
|||
*/ |
|||
var options = { |
|||
recordingFile: 'data.json', |
|||
frameDelay: 0 |
|||
}; |
|||
|
|||
/** |
|||
* A middleware that called after rendering frames |
|||
* |
|||
* @param {Object} record {delay, content, index} |
|||
* @param {Function} next |
|||
*/ |
|||
options.afterMiddleware = function(record, next) { |
|||
|
|||
var width = this.width(); |
|||
var height = this.height(); |
|||
var captureRect = {x: 0, y: 0, width: width, height: height}; |
|||
|
|||
if (stepsCounter != 0) { |
|||
stepsCounter = (stepsCounter + 1) % step; |
|||
return next(); |
|||
} |
|||
|
|||
stepsCounter = (stepsCounter + 1) % step; |
|||
|
|||
// A workaround by a delay to make sure the record is rendered |
|||
setTimeout(function() { |
|||
|
|||
capturePage(captureRect, function(img) { |
|||
|
|||
var outputPath = path.join(__dirname, '/frames/' + record.index + '.png'); |
|||
|
|||
fs.writeFileSync(outputPath, img.toPNG()); |
|||
ipcRenderer.send('captured', record.index); |
|||
next(); |
|||
|
|||
}); |
|||
|
|||
}, 10); |
|||
|
|||
}; |
|||
|
|||
// Initialize the terminalizer plugin |
|||
$('#terminal').terminalizer(options); |
|||
|
|||
/** |
|||
* A callback function for the event: |
|||
* playingDone |
|||
*/ |
|||
$('#terminal').on('playingDone', function() { |
|||
|
|||
currentWindow.close(); |
|||
|
|||
}); |
|||
|
|||
/** |
|||
* Catch all unhandled errors |
|||
* |
|||
* @param {String} errorMsg |
|||
*/ |
|||
window.onerror = function(errorMsg) { |
|||
|
|||
ipcRenderer.send('error', errorMsg); |
|||
|
|||
}; |
|||
|
|||
</script> |
|||
|
|||
<script>if (window.module) module = window.module;</script> |
|||
|
|||
</body> |
|||
</html> |
@ -0,0 +1,15 @@ |
|||
/** |
|||
* Terminalizer |
|||
* |
|||
* @author Mohammad Fares <faressoft.com@gmail.com> |
|||
*/ |
|||
|
|||
body { |
|||
background-color: white; |
|||
margin: 0; |
|||
} |
|||
|
|||
#terminal { |
|||
display: inline-block; |
|||
font-size: 0px; |
|||
} |
@ -0,0 +1,127 @@ |
|||
/** |
|||
* Terminalizer |
|||
* |
|||
* @author Mohammad Fares <faressoft.com@gmail.com> |
|||
*/ |
|||
|
|||
var fs = require('fs'), |
|||
path = require('path'), |
|||
async = require('async'), |
|||
remote = require('electron').remote, |
|||
ipcRenderer = require('electron').ipcRenderer, |
|||
terminalizerPlayer = require('terminalizer-player'); |
|||
var currentWindow = remote.getCurrentWindow(), |
|||
capturePage = currentWindow.webContents.capturePage, |
|||
step = remote.getGlobal('step'), |
|||
renderDir = remote.getGlobal('renderDir'); |
|||
|
|||
// Styles
|
|||
import '../css/app.css'; |
|||
import 'terminalizer-player/dist/css/terminalizer.min.css'; |
|||
import 'xterm/dist/xterm.css'; |
|||
|
|||
/** |
|||
* Used for the step option |
|||
* @type {Number} |
|||
*/ |
|||
var stepsCounter = 0; |
|||
|
|||
/** |
|||
* A callback function for the event: |
|||
* When the document is loaded |
|||
*/ |
|||
$(document).ready(function() { |
|||
|
|||
// Initialize the terminalizer plugin
|
|||
$('#terminal').terminalizer({ |
|||
recordingFile: 'data.json', |
|||
autoplay: true, |
|||
controls: false, |
|||
}); |
|||
|
|||
/** |
|||
* A callback function for the event: |
|||
* When the terminal playing is started |
|||
*/ |
|||
$('#terminal').on('playingStarted', function() { |
|||
|
|||
var terminalizer = $('#terminal').data('terminalizer'); |
|||
var framesCount = terminalizer.getFramesCount(); |
|||
var tasks = []; |
|||
|
|||
terminalizer.pause(); |
|||
|
|||
for (var i = 0; i < framesCount; i++) { |
|||
|
|||
(function(frameIndex) { |
|||
|
|||
tasks.push(function(callback) { |
|||
|
|||
terminalizer._applySnapshot(terminalizer._snapshots[frameIndex]); |
|||
|
|||
// A workaround delay to allow rendering
|
|||
setTimeout(function() { |
|||
capture(frameIndex, callback); |
|||
}, 20); |
|||
|
|||
}); |
|||
|
|||
}(i)); |
|||
|
|||
} |
|||
|
|||
async.series(tasks, function(error, result) { |
|||
|
|||
if (error) { |
|||
throw new Error(error); |
|||
} |
|||
|
|||
currentWindow.close(); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
|||
/** |
|||
* Capture the current frame |
|||
* |
|||
* @param {Number} frameIndex |
|||
* @param {Function} callback |
|||
*/ |
|||
function capture(frameIndex, callback) { |
|||
|
|||
var width = $('#terminal').width(); |
|||
var height = $('#terminal').height(); |
|||
var captureRect = {x: 0, y: 0, width: width, height: height}; |
|||
|
|||
if (stepsCounter != 0) { |
|||
stepsCounter = (stepsCounter + 1) % step; |
|||
return callback(); |
|||
} |
|||
|
|||
stepsCounter = (stepsCounter + 1) % step; |
|||
|
|||
capturePage(captureRect, function(img) { |
|||
|
|||
var outputPath = path.join(renderDir, frameIndex + '.png'); |
|||
|
|||
fs.writeFileSync(outputPath, img.toPNG()); |
|||
ipcRenderer.send('captured', frameIndex); |
|||
callback(); |
|||
|
|||
}); |
|||
|
|||
} |
|||
|
|||
/** |
|||
* Catch all unhandled errors |
|||
* |
|||
* @param {String} error |
|||
*/ |
|||
window.onerror = function(error) { |
|||
|
|||
ipcRenderer.send('error', error); |
|||
|
|||
}; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue