Set of HTTP middlewares for gifsockets
This is part of the gifsockets project; it acts as a plug and play middleware that is used in the demo but can be re-used anywhere.
Install the module with: npm install gifsockets-middleware
var GifsocketMiddleware = require('gifsockets-middleware');
var middlewares = GifsocketMiddleware({width: 200, height: 200});
var express = require('express');
var app = express();
// middlewares returns an object containing 4 middlewares
// `openImage` writes the beginning of a .gif and leaves `res` open
app.get('/image.gif', middlewares.openImage);
// `writePixelsToImages` writes a new frame to all open `res` from openImage
var bodyParser = express.bodyParser();
app.post('/image/pixels', bodyParser, middlewares.writePixelsToImages);
// `writeTextToImages` accepts a string of text and writes a new frame
// This requires running `phantomjs-pixel-server`
app.post('/image/text', bodyParser, middlewares.writeTextToImages);
// `closeOpenImages` closes all active images opened by `openImage`
app.post('/image/close', middlewares.closeOpenImages);
// If you want to load a specific middleware, you can do so
var openImageMiddleware = require('gifsockets-middleware/lib/middlewares/open-image');
var openImage = openImageMiddleware(gifsocket);
// `openImage` has the same behavior as that returned from `GifsocketMiddleware`
gifsockets-middleware
returns GifsocketMiddleware
as its module.exports
GifsocketMiddleware(options)
Function that generates an object of middlewares for gifsockets
Object
Number
Width of the output GIF/gifsocket
Number
Height of the output GIF/gifsocket
openImage
middlewarewritePixelsToImages
middlewarewriteTextToImages
middlewarecloseOpenImages
middlewareopenImage
middlewareMiddleware that will maintain an open connection such that it can write additional GIF frames.
Function signature is function (req, res, next) {}
This does not expect any information on req
/res
and will not callback to next
.
writePixelsToImages
middlewareMiddleware that will write a new GIF frame with the provided pixels.
Function signature is function (req, res, next) {}
If req.rgbPixels
exists, we will draw a GIF frame with the pixel values.
req.rgbPixels
is expected to be an stringified array of rgb pixels;[0, 1, 2, 3, 4, 5]
is 2 pixels withr: 0, g: 1, b: 2
andr: 3, g: 4, b: 5
If req.rgbPixels
is not found, we look for req.rgbaPixels
or req.body
. If either of these is found, we will draw a GIF frame with the pixel values.
req.rgbaPixels
/req.body
is expected to be an stringified array of rgba pixels;[0, 1, 2, 3, 4, 5, 6, 7]
is 2 pixels withr: 0, g: 1, b: 2, a: 3
andr: 4, g: 5, b: 6, a: 7
This will reply with a 204
when it is complete.
writeTextToImages
middlewareThis depends on phantomjs-pixel-server running as another process.
Please read the documentation: https://github.com/twolfson/phantomjs-pixel-server
Middleware that will create and write a new GIF frame with the provided text parameters.
Function signature is function (req, res, next) {}
If req.body
is provided, we will parse it via querystring.parse
and use the following parameters:
String
- Text to write to the canvasString
- CSS color background for the canvas (default "#000000")
String
- CSS color for text on the canvas (default "#BADA55")
Number
- Size of the font to draw (default: 30)String
- Font family to use on the canvas (default: "Impact")This will reply with a 204
when it is complete.
closeOpenImages
middlewareMiddleware that iterates over open connections, writes GIF footer, and closes connection.
Function signature is function (req, res, next) {}
This will reply with a 204
when it is complete.
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 20 2013, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.