'use strict'; /*! * Canvas - PNGStream * Copyright (c) 2010 LearnBoost * MIT Licensed */ /** * Module dependencies. */ var Stream = require('stream').Stream; /** * Initialize a `PNGStream` 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.png". * * var out = fs.createWriteStream(__dirname + '/my.png') * , stream = canvas.createPNGStream(); * * stream.pipe(out); * * @param {Canvas} canvas * @param {Boolean} sync * @api public */ var PNGStream = module.exports = function PNGStream(canvas, sync) { var self = this , method = sync ? 'streamPNGSync' : 'streamPNG'; this.sync = sync; this.canvas = canvas; this.readable = true; // TODO: implement async if ('streamPNG' == method) method = 'streamPNGSync'; process.nextTick(function(){ canvas[method](function(err, chunk, len){ if (err) { self.emit('error', err); self.readable = false; } else if (len) { self.emit('data', chunk, len); } else { self.emit('end'); self.readable = false; } }); }); }; /** * Inherit from `EventEmitter`. */ PNGStream.prototype.__proto__ = Stream.prototype;