Browse Source

Add PDF button to test page to easily generate PDF version of the test image.

v1.x
Andy Wood 10 years ago
parent
commit
cbf3da4900
  1. 27
      test/public/app.js
  2. 36
      test/server.js

27
test/public/app.js

@ -26,6 +26,32 @@ function text(str) {
return document.createTextNode(str);
}
function pdfForm(fn, canvas) {
var form = create('form')
, input = create('input')
, submit = create('input');
form.setAttribute('action', '/pdf');
form.setAttribute('method', 'post');
form.setAttribute('target', '_blank');
input.setAttribute('type', 'hidden');
input.setAttribute('name', 'json');
input.setAttribute('value', JSON.stringify({
fn: fn.toString()
, width: canvas.width
, height: canvas.height
}));
submit.setAttribute('type', 'submit');
submit.setAttribute('value', 'PDF');
form.appendChild(input);
form.appendChild(submit);
return form;
}
function clearTests() {
var table = get('tests');
table.removeChild(table.children[1]);
@ -47,6 +73,7 @@ function runTests() {
tds[1].appendChild(canvas);
tds[2].appendChild(create('h3', name));
tds[2].appendChild(pdfForm(fn, canvas));
tr.appendChild(tds[0]);
tr.appendChild(tds[1]);

36
test/server.js

@ -28,7 +28,7 @@ app.get('/', function(req, res){
res.render('tests');
});
app.post('/render', function(req, res, next){
function testFn(req){
// Normalize state.png as ./public/state.png
// no good way around this at the moment
req.body.fn = req.body.fn
@ -36,10 +36,18 @@ app.post('/render', function(req, res, next){
.replace("'face.jpeg'", "'" + __dirname + "/public/face.jpeg'");
// Do not try this at home :)
var fn = eval('(' + req.body.fn + ')')
, width = req.body.width
, height = req.body.height
, canvas = new Canvas(width, height)
return eval('(' + req.body.fn + ')');
}
function createCanvas(req, type){
var width = req.body.width
, height = req.body.height;
return new Canvas(width, height, type);
}
app.post('/render', function(req, res, next){
var fn = testFn(req)
, canvas = createCanvas(req)
, ctx = canvas.getContext('2d')
, start = new Date;
@ -55,6 +63,24 @@ app.post('/render', function(req, res, next){
: fn(ctx), done();
});
app.post('/pdf', function(req, res, next){
req.body = JSON.parse(req.body.json);
var fn = testFn(req)
, canvas = createCanvas(req, 'pdf')
, ctx = canvas.getContext('2d');
function done(){
res.writeHead(200, {'Content-Type' : 'application/pdf'});
res.write(canvas.toBuffer());
res.end();
}
2 == fn.length
? fn(ctx, done)
: fn(ctx), done();
});
var port = parseInt(process.argv[2] || '4000', 10);
app.listen(port);
console.log('Test server listening on port %d', port);

Loading…
Cancel
Save