UNPKG

canvas

Version:
66 lines (54 loc) 1.52 kB
'use strict'; /*! * Canvas - JPEGStream * Copyright (c) 2010 LearnBoost <tj@learnboost.com> * MIT Licensed */ /** * Module dependencies. */ var Readable = require('stream').Readable; var util = require('util'); /** * Initialize a `JPEGStream` with the given `canvas`. * * "data" events are emitted with `Buffer` chunks, once complete the * "end" event is emitted. The following example will stream to a file * named "./my.jpeg". * * var out = fs.createWriteStream(__dirname + '/my.jpeg') * , stream = canvas.createJPEGStream(); * * stream.pipe(out); * * @param {Canvas} canvas * @private */ var JPEGStream = module.exports = function JPEGStream(canvas, options) { if (!(this instanceof JPEGStream)) { throw new TypeError("Class constructors cannot be invoked without 'new'"); } if (canvas.streamJPEGSync === undefined) { throw new Error("node-canvas was built without JPEG support."); } Readable.call(this); this.options = options; this.canvas = canvas; }; util.inherits(JPEGStream, Readable); function noop() {} JPEGStream.prototype._read = function _read() { // For now we're not controlling the c++ code's data emission, so we only // call canvas.streamJPEGSync once and let it emit data at will. this._read = noop; var self = this; self.canvas.streamJPEGSync(this.options, function(err, chunk){ if (err) { self.emit('error', err); } else if (chunk) { self.push(chunk); } else { self.push(null); } }); };