Stream never-ending animated GIFs
This is part of the gifsockets project. It creates a mediator for streams to subscribe to as new GIF frames are written.
Want to see gifsockets
in action? See the demo:
http://gifsockets.twolfson.com/
Install the module with: npm install gifsockets
// Pass any writable stream as a listener to gifsocket
var gifsocket = new require('gifsockets');
gifsocket.addListener(stream, function (err) {
// Write a new GIF frame to the stream
// WARNING: YOUR DATA WILL BE MUTATED.
// PLEASE CLONE IT BEFORE HAND IF YOU ARE WORRIED ABOUT IT.
var rgbPixels = [0, 0, 0, /*, ...*/];
gifsocket.writeRgbFrame(rgbPixels, function (err) {
// Close the stream
gifsocket.closeAll();
});
});
gifsockets
exposes a constructor Gifsocket
function as its module.exports
.
new Gifsocket(options)
Constructor for a Gifsocket
Object
Number
- Width of the output GIFNumber
- Height of the output GIFgifsocket.addListener(stream, [cb])
Subscribe a writable stream to the stream of outgoing GIF frames
WritableStream
- Writable stream to write GIF information toFunction
- Optional error-first callback to run once the stream has received the GIF header
function (err) {}
gifsocket.writeRgbFrame(rgbPixels, [cb])
Write a new RGB based frame to all of the current subscribers
IF YOU USE THIS METHOD, IT WILL MUTATE rgbPixels
.
PLEASE CLONE YOUR DATA IF YOU ARE WORRIED ABOUT CORRUPTION.
Number[]
- Array of rgb pixels of the new frame
width * height * 3
[0, 10, 20, 30, 40, 50]
represents 2 pixels; the first, r: 0, g: 10, b: 20
; the second, r: 30, g: 40, b: 50
Function
- Optional error-first callback to run once the frame has been drawn to all listeners
function (err) {}
gifsocket.writeRgbaFrame(rgbaPixels, [cb])
Write a new RGBA based frame to all subscribers
Number[]
- Array of rgba pixels for the next frame
width * height * 4
[0, 10, 20, 30, 40, 50, 60, 70]
represents 2 pixels; the first, r: 0, g: 10, b: 20, a: 30
; the second, r: 40, g: 50, b: 60, a: 70
gifsocket.closeAll([cb])
Write GIF footer to all open listeners and close them
Function
- Optional error-first callback to run once the footer has written to all listeners
function (err) {}
A streaming interface does not quite line up with what happens here. gifsockets
that are receiving their first frame will not receive the same data as their second frame counterparts. As a result, a data
event or read
does not make sense as all listeners/readers are indistinguishable.
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via grunt and test via npm test
.
Support this project and others by twolfson via gittip.
As of Nov 18 2013, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.