Node.js: Add JPEG support to exportFrames()

Closes #1166
This commit is contained in:
therewasaguy 2016-09-18 15:38:28 -04:00 committed by Jürg Lehni
parent 7412939096
commit 6939c1674b
2 changed files with 7 additions and 4 deletions

View file

@ -55,7 +55,7 @@ module.exports = function(self, requireName) {
}); });
// Extend HTMLCanvasElement with useful methods from the underlying Canvas: // Extend HTMLCanvasElement with useful methods from the underlying Canvas:
['toBuffer', 'pngStream', 'createPNGStream', 'jpgStream', 'createJPGStream'] ['toBuffer', 'pngStream', 'createPNGStream', 'jpegStream', 'createJPEGStream']
.forEach(function(key) { .forEach(function(key) {
HTMLCanvasElement.prototype[key] = function() { HTMLCanvasElement.prototype[key] = function() {
var canvas = idlUtils.implForWrapper(this)._canvas; var canvas = idlUtils.implForWrapper(this)._canvas;

View file

@ -67,7 +67,7 @@ module.exports = function(paper) {
}, },
/** /**
* @deprecated use use {@link #createCanvas(width, height)} instead. * @deprecated, use use {@link #createCanvas(width, height)} instead.
*/ */
Canvas: '#createCanvas' Canvas: '#createCanvas'
}); });
@ -87,9 +87,12 @@ module.exports = function(paper) {
fps: 30, fps: 30,
prefix: 'frame-', prefix: 'frame-',
amount: 1, amount: 1,
extension: 'png' // options are 'png' or 'jpg'
}, options); }, options);
if (!options.directory) if (!options.directory)
throw new Error('Missing options.directory'); throw new Error('Missing options.directory');
if (options.extension && (options.extension !== 'jpg' && options.extension !== 'png'))
throw new Error('Unsupported extension. Options are "jpg" or "png"');
var view = this, var view = this,
count = 0, count = 0,
frameDuration = 1 / options.fps, frameDuration = 1 / options.fps,
@ -109,7 +112,7 @@ module.exports = function(paper) {
count: count count: count
})); }));
var file = path.join(options.directory, options.prefix + var file = path.join(options.directory, options.prefix +
(paddedStr + count).slice(-padding) + '.png'); (paddedStr + count).slice(-padding) + '.' + options.extension);
var out = view.exportImage(file, function() { var out = view.exportImage(file, function() {
// Once the file has been closed, export the next fame: // Once the file has been closed, export the next fame:
var then = Date.now(); var then = Date.now();
@ -140,7 +143,7 @@ module.exports = function(paper) {
exportImage: function(path, callback) { exportImage: function(path, callback) {
this.update(); this.update();
var out = fs.createWriteStream(path), var out = fs.createWriteStream(path),
stream = this._element.createPNGStream(); stream = path.substr(-3) === 'jpg' ? this._element.createJPEGStream() : this._element.createPNGStream();
// Pipe the png stream to the write stream: // Pipe the png stream to the write stream:
stream.pipe(out); stream.pipe(out);
if (callback) { if (callback) {