Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/cameras/2d/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,10 @@ var Camera = new Class({
var displayWidth = width / zoomX;
var displayHeight = height / zoomY;

var vwx = midX - (displayWidth / 2);
var vwy = midY - (displayHeight / 2);
// Zoom is applied around the camera origin, so the top-left of the view
// only lines up with the middle of the camera when the origin is 0.5
var vwx = sx + originX - (originX / zoomX);
var vwy = sy + originY - (originY / zoomY);

this.worldView.setTo(vwx, vwy, displayWidth, displayHeight);

Expand Down
49 changes: 49 additions & 0 deletions tests/cameras/2d/Camera.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,53 @@ describe('Camera', function ()
{
expect(Camera).toBeDefined();
});

describe('worldView', function ()
{
function makeCamera (originX, originY, zoom)
{
var camera = new Camera(0, 0, 800, 600);

camera.setOrigin(originX, originY);
camera.setZoom(zoom);
camera.setScroll(100, 50);
camera.preRender();

return camera;
}

function screenToWorld (camera, x, y)
{
var point = camera.matrix.applyInverse(x, y);

return { x: point.x, y: point.y };
}

it('should be centered on the camera with the default origin', function ()
{
var camera = makeCamera(0.5, 0.5, 2);

expect(camera.worldView.x).toBe(300);
expect(camera.worldView.y).toBe(200);
expect(camera.worldView.width).toBe(400);
expect(camera.worldView.height).toBe(300);
});

it('should match the area the camera renders when the origin is not centered', function ()
{
var origins = [ [ 0, 0 ], [ 1, 1 ], [ 0.25, 0.75 ] ];

for (var i = 0; i < origins.length; i++)
{
var camera = makeCamera(origins[i][0], origins[i][1], 3);
var topLeft = screenToWorld(camera, 0, 0);
var bottomRight = screenToWorld(camera, 800, 600);

expect(camera.worldView.x).toBeCloseTo(topLeft.x);
expect(camera.worldView.y).toBeCloseTo(topLeft.y);
expect(camera.worldView.right).toBeCloseTo(bottomRight.x);
expect(camera.worldView.bottom).toBeCloseTo(bottomRight.y);
}
});
});
});