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
.
Constructor for a Gifsocket
- options
Object
- options.width
Number
- Width of the output GIF - options.height
Number
- Height of the output GIF
- options.width
Subscribe a writable stream to the stream of outgoing GIF frames
- stream
WritableStream
- Writable stream to write GIF information to - cb
Function
- Optional error-first callback to run once the stream has received the GIF header- Signature should look like
function (err) {}
- Signature should look like
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.
- rgbPixels
Number[]
- Array of rgb pixels of the new frame- This should be the the same length as
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
- This should be the the same length as
- cb
Function
- Optional error-first callback to run once the frame has been drawn to all listeners- Signature should look like
function (err) {}
- Signature should look like
Write a new RGBA based frame to all subscribers
- rgbaPixels
Number[]
- Array of rgba pixels for the next frame- Length should be
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
- Length should be
Write GIF footer to all open listeners and close them
- cb
Function
- Optional error-first callback to run once the footer has written to all listeners- Signature should look like
function (err) {}
- Signature should look like
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.