Browse Source

Added PNGStream test

v1.x
Tj Holowaychuk 15 years ago
parent
commit
1e19b7ec10
  1. 14
      lib/canvas.js
  2. 1
      src/canvas.cc
  3. 46
      test/canvas.test.js

14
lib/canvas.js

@ -14,7 +14,8 @@ var canvas = require('../build/default/canvas')
, Canvas = canvas.Canvas , Canvas = canvas.Canvas
, Context2d = canvas.CanvasRenderingContext2d , Context2d = canvas.CanvasRenderingContext2d
, CanvasGradient = canvas.CanvasGradient , CanvasGradient = canvas.CanvasGradient
, cairoVersion = canvas.cairoVersion; , cairoVersion = canvas.cairoVersion
, PNGStream = require('./pngstream');
/** /**
* Export `Canvas` as the module. * Export `Canvas` as the module.
@ -126,6 +127,17 @@ Canvas.prototype.getContext = function(contextId){
} }
}; };
/**
* Create a `PNGStream` for `this` canvas.
*
* @return {PNGStream}
* @api public
*/
Canvas.prototype.createPNGStream = function(){
return new PNGStream(this);
};
/** /**
* Add `color` stop at the given `offset`. * Add `color` stop at the given `offset`.
* *

1
src/canvas.cc

@ -72,6 +72,7 @@ Handle<Value>
Canvas::StreamPNG(const Arguments &args) { Canvas::StreamPNG(const Arguments &args) {
HandleScope scope; HandleScope scope;
// TODO: error handling // TODO: error handling
// TODO: nonblocking
if (!args[0]->IsFunction()) if (!args[0]->IsFunction())
return ThrowException(Exception::TypeError(String::New("callback function required"))); return ThrowException(Exception::TypeError(String::New("callback function required")));
Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This()); Canvas *canvas = ObjectWrap::Unwrap<Canvas>(args.This());

46
test/canvas.test.js

@ -14,6 +14,10 @@ function hash(val) {
function assertChecksum(canvas, path, checksum, msg) { function assertChecksum(canvas, path, checksum, msg) {
canvas.savePNG(path); canvas.savePNG(path);
assertChecksumOf(canvas, path, checksum, msg);
}
function assertChecksumOf(canvas, path, checksum, msg) {
fs.readFile(path, function(err, buf){ fs.readFile(path, function(err, buf){
assert.equal(hash(buf), checksum, msg); assert.equal(hash(buf), checksum, msg);
}); });
@ -499,5 +503,47 @@ module.exports = {
assert.ok(ctx.isPointInPath(60,110)); assert.ok(ctx.isPointInPath(60,110));
assert.ok(!ctx.isPointInPath(70,110)); assert.ok(!ctx.isPointInPath(70,110));
assert.ok(!ctx.isPointInPath(50,120)); assert.ok(!ctx.isPointInPath(50,120));
},
'test PNGStream': function(assert, beforeExit){
var canvas = new Canvas(320, 320)
, ctx = canvas.getContext('2d')
, path = __dirname + '/images/pngstream.png'
, called = 0;
ctx.strokeStyle = 'rgba(0,0,0,0.5)';
ctx.strokeRect(0,0,320,320);
ctx.fillStyle = 'rgba(0,0,0,0.02)';
var steps = 200;
while (steps--) {
ctx.fillRect(
160 - (steps / 2)
, 160 - (steps / 2)
, steps
, steps
);
}
var out = fs.createWriteStream(path)
, stream = canvas.createPNGStream();
out.on('close', function(){
assertChecksumOf(
canvas
, path
, '04f2e1b4338de2d7451194cba7d29970'
, 'PNGStream failed');
});
stream.on('data', function(chunk){ out.write(chunk); });
stream.on('end', function(){
++called;
out.end();
});
beforeExit(function(){
assert.equal(1, called);
});
} }
} }
Loading…
Cancel
Save