|
|
@ -5,7 +5,8 @@ |
|
|
|
var Canvas = require('../') |
|
|
|
, assert = require('assert') |
|
|
|
, parseFont = Canvas.Context2d.parseFont |
|
|
|
, fs = require('fs'); |
|
|
|
, fs = require('fs') |
|
|
|
, os = require('os'); |
|
|
|
|
|
|
|
console.log(); |
|
|
|
console.log(' canvas: %s', Canvas.version); |
|
|
@ -246,6 +247,12 @@ describe('Canvas', function () { |
|
|
|
assert.equal(50, canvas.height); |
|
|
|
}); |
|
|
|
|
|
|
|
it('Canvas#stride', function() { |
|
|
|
var canvas = new Canvas(24, 10); |
|
|
|
assert.ok(canvas.stride >= 24, 'canvas.stride is too short'); |
|
|
|
assert.ok(canvas.stride < 1024, 'canvas.stride seems too long'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('Canvas#getContext("invalid")', function () { |
|
|
|
assert.equal(null, new Canvas(200, 300).getContext('invalid')); |
|
|
|
}); |
|
|
@ -377,6 +384,87 @@ describe('Canvas', function () { |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
describe('#toBuffer("raw")', function() { |
|
|
|
var canvas = new Canvas(10, 10) |
|
|
|
, ctx = canvas.getContext('2d'); |
|
|
|
|
|
|
|
ctx.clearRect(0, 0, 10, 10); |
|
|
|
|
|
|
|
ctx.fillStyle = 'rgba(200, 200, 200, 0.505)'; |
|
|
|
ctx.fillRect(0, 0, 5, 5); |
|
|
|
|
|
|
|
ctx.fillStyle = 'red'; |
|
|
|
ctx.fillRect(5, 0, 5, 5); |
|
|
|
|
|
|
|
ctx.fillStyle = '#00ff00'; |
|
|
|
ctx.fillRect(0, 5, 5, 5); |
|
|
|
|
|
|
|
ctx.fillStyle = 'black'; |
|
|
|
ctx.fillRect(5, 5, 4, 5); |
|
|
|
|
|
|
|
/** Output: |
|
|
|
* *****RRRRR |
|
|
|
* *****RRRRR |
|
|
|
* *****RRRRR |
|
|
|
* *****RRRRR |
|
|
|
* *****RRRRR |
|
|
|
* GGGGGBBBB- |
|
|
|
* GGGGGBBBB- |
|
|
|
* GGGGGBBBB- |
|
|
|
* GGGGGBBBB- |
|
|
|
* GGGGGBBBB- |
|
|
|
*/ |
|
|
|
|
|
|
|
var buf = canvas.toBuffer('raw'); |
|
|
|
var stride = canvas.stride; |
|
|
|
|
|
|
|
// Buffer doesn't have readUInt32(): it only has readUInt32LE() and
|
|
|
|
// readUInt32BE().
|
|
|
|
if (os.endianness() === 'LE') buf.swap32(); |
|
|
|
|
|
|
|
function assertPixel(u32, x, y, message) { |
|
|
|
var expected = '0x' + u32.toString(16); |
|
|
|
var actual = '0x' + buf.readUInt32BE(y * stride + x * 4).toString(16); |
|
|
|
assert.equal(actual, expected, message); |
|
|
|
} |
|
|
|
|
|
|
|
it('should have the correct size', function() { |
|
|
|
assert.equal(buf.length, stride * 10); |
|
|
|
}); |
|
|
|
|
|
|
|
it('does not premultiply alpha', function() { |
|
|
|
assertPixel(0x80646464, 0, 0, 'first semitransparent pixel'); |
|
|
|
assertPixel(0x80646464, 4, 4, 'last semitransparent pixel'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('draws red', function() { |
|
|
|
assertPixel(0xffff0000, 5, 0, 'first red pixel'); |
|
|
|
assertPixel(0xffff0000, 9, 4, 'last red pixel'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('draws green', function() { |
|
|
|
assertPixel(0xff00ff00, 0, 5, 'first green pixel'); |
|
|
|
assertPixel(0xff00ff00, 4, 9, 'last green pixel'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('draws black', function() { |
|
|
|
assertPixel(0xff000000, 5, 5, 'first black pixel'); |
|
|
|
assertPixel(0xff000000, 8, 9, 'last black pixel'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('leaves undrawn pixels black, transparent', function() { |
|
|
|
assertPixel(0x0, 9, 5, 'first undrawn pixel'); |
|
|
|
assertPixel(0x0, 9, 9, 'last undrawn pixel'); |
|
|
|
}); |
|
|
|
|
|
|
|
it('is immutable', function() { |
|
|
|
ctx.fillStyle = 'white'; |
|
|
|
ctx.fillRect(0, 0, 10, 10); |
|
|
|
canvas.toBuffer('raw'); // (side-effect: flushes canvas)
|
|
|
|
assertPixel(0xffff0000, 5, 0, 'first red pixel'); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
describe('#toDataURL()', function () { |
|
|
|
var canvas = new Canvas(200, 200) |
|
|
|
, ctx = canvas.getContext('2d'); |
|
|
|