Skip to content

Commit

Permalink
Fixing a bug in centerOn for non-zero origins
Browse files Browse the repository at this point in the history
When the viewport is not already at zero and .centerOn
is called, it was not correctly centering. The further
away from the origin the viewport was, the further away
from the center target it would end up.
  • Loading branch information
ericathegreat committed Aug 7, 2013
1 parent 073d89e commit fa4ad57
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,8 +578,8 @@ Crafty.extend({
* Centers the viewport on the given entity
*/
centerOn: function (targ, time) {
var x = targ.x,
y = targ.y,
var x = targ.x + Crafty.viewport.x,
y = targ.y + Crafty.viewport.y,
mid_x = targ.w / 2,
mid_y = targ.h / 2,
cent_x = Crafty.viewport.width / 2,
Expand Down
9 changes: 9 additions & 0 deletions tests/stage.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@
Crafty.timer.simulateFrames(10);
equal(Crafty.viewport._x, -e.w / 2 + Crafty.viewport.width / 2, "Entity still centered 10 frames later");


var e2 = Crafty.e("2D, DOM").attr({ x: 450, y: 450, w: 20, h: 20 });
Crafty.viewport.scroll('x', 1500);
Crafty.viewport.scroll('y', 300);
Crafty.viewport.centerOn(e2, 1);
Crafty.timer.simulateFrames(1);
equal(Crafty.viewport._x, -(e2.x + e2.w/2 - Crafty.viewport.width / 2), "Entity centered from non-zero origin");
equal(Crafty.viewport._y, -(e2.y + e2.h/2 - Crafty.viewport.height / 2), "Entity centered from non-zero origin");

Crafty.viewport.scroll('x', 0);
Crafty.viewport.scroll('y', 0);
});
Expand Down

0 comments on commit fa4ad57

Please sign in to comment.