You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
|
|
/*!
|
|
* Canvas - PNGStream
|
|
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
|
|
* MIT Licensed
|
|
*/
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
/**
|
|
* 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.on('data', function(chunk){
|
|
* out.write(chunk);
|
|
* });
|
|
*
|
|
* stream.on('end', function(){
|
|
* out.end();
|
|
* });
|
|
*
|
|
* @param {Canvas} canvas
|
|
* @api public
|
|
*/
|
|
|
|
var PNGStream = module.exports = function PNGStream(canvas) {
|
|
var self = this;
|
|
this.canvas = canvas;
|
|
process.nextTick(function(){
|
|
canvas.streamPNG(function(err, chunk, len){
|
|
if (err) {
|
|
self.emit('error', err);
|
|
} else if (len) {
|
|
self.emit('data', chunk, len);
|
|
} else {
|
|
self.emit('end');
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Inherit from `EventEmitter`.
|
|
*/
|
|
|
|
PNGStream.prototype.__proto__ = EventEmitter.prototype;
|