Fixed CanvasRenderer regression + added unit test

This commit is contained in:
jdescottes 2014-12-21 18:56:40 +01:00
parent 123ea31191
commit 5cb1d0cd03
2 changed files with 43 additions and 3 deletions

View File

@ -50,10 +50,11 @@
};
ns.CanvasRenderer.prototype.renderLine_ = function (color, x, y, width, context) {
if(color != Constants.TRANSPARENT_COLOR) {
if(color == Constants.TRANSPARENT_COLOR) {
color = this.transparentColor_;
}
context.fillStyle = color;
context.fillRect(x, y, 1, width);
}
};
ns.CanvasRenderer.prototype.createCanvas_ = function (zoom) {

View File

@ -0,0 +1,39 @@
describe("Canvas Renderer test", function() {
var BLACK = '#000000';
var WHITE = '#ffffff';
var TRANS = Constants.TRANSPARENT_COLOR;
var toFrameGrid = function (normalGrid) {
var frameGrid = [];
var w = normalGrid[0].length;
var h = normalGrid.length;
for (var x = 0 ; x < w ; x++) {
frameGrid[x] = [];
for (var y = 0 ; y < h ; y++) {
frameGrid[x][y] = normalGrid[y][x];
}
}
return frameGrid;
};
beforeEach(function() {});
afterEach(function() {});
it("draws transparent as white by default", function() {
// create frame
var frame = pskl.model.Frame.fromPixelGrid(toFrameGrid([
[BLACK, TRANS],
[TRANS, BLACK]
]));
var renderer = new pskl.rendering.CanvasRenderer(frame, 1);
var canvas = renderer.render();
var frameFromCanvas = pskl.utils.FrameUtils.createFromImage(canvas);
expect(frameFromCanvas.getPixel(0,0)).toBe(BLACK);
expect(frameFromCanvas.getPixel(0,1)).toBe(WHITE);
expect(frameFromCanvas.getPixel(1,0)).toBe(WHITE);
expect(frameFromCanvas.getPixel(1,1)).toBe(BLACK);
});
});