Rendering scenes on server side

Sheetengine can also be used on the server-side to render scenes. In this example we use node ( and node-canvas ( For the latter you will also have to install Cairo ( Let's include the required modules:

var http = require('http');
var Canvas = require('canvas');
var sheetengine = require('../sheetengine');
The next step is to instruct sheetengine to use a custom function for creating canvases instead of the default implementation:

sheetengine.drawing.createCanvas = function(w, h) { return new Canvas(w, h); };
We also have to use the Canvas constructor of node-canvas when initializing sheetengine:

var canvasElement = new Canvas(900,500);
That's it, everything else works the same way you have seen it in previous examples. The only thing left is to serve the image over http: node-canvas provides means for this, too:

http.createServer(function (req, res) {

  // redraw the scene for every request
  // send the image as the response
  var buf = sheetengine.canvas.toBuffer();
  res.statusCode = 200;
  res.setHeader('Content-Type', 'image/png');
  res.setHeader('Content-Length', buf.length);

Last edited Dec 11, 2012 at 2:18 AM by dobsonl, version 1


No comments yet.