Skip to content

Commit

Permalink
Node.js: Add JPEG support to exportFrames()
Browse files Browse the repository at this point in the history
  • Loading branch information
therewasaguy authored and lehni committed Oct 4, 2017
1 parent 7412939 commit 6939c16
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/node/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = function(self, requireName) {
});

// Extend HTMLCanvasElement with useful methods from the underlying Canvas:
['toBuffer', 'pngStream', 'createPNGStream', 'jpgStream', 'createJPGStream']
['toBuffer', 'pngStream', 'createPNGStream', 'jpegStream', 'createJPEGStream']
.forEach(function(key) {
HTMLCanvasElement.prototype[key] = function() {
var canvas = idlUtils.implForWrapper(this)._canvas;
Expand Down
9 changes: 6 additions & 3 deletions src/node/extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
});
Expand All @@ -87,9 +87,12 @@ module.exports = function(paper) {
fps: 30,
prefix: 'frame-',
amount: 1,
extension: 'png' // options are 'png' or 'jpg'
}, options);
if (!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,
count = 0,
frameDuration = 1 / options.fps,
Expand All @@ -109,7 +112,7 @@ module.exports = function(paper) {
count: count
}));
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() {
// Once the file has been closed, export the next fame:
var then = Date.now();
Expand Down Expand Up @@ -140,7 +143,7 @@ module.exports = function(paper) {
exportImage: function(path, callback) {
this.update();
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:
stream.pipe(out);
if (callback) {
Expand Down

0 comments on commit 6939c16

Please sign in to comment.