Skip to content

Commit

Permalink
Ticket #119 - Translate the canvas context to world coordinates for a…
Browse files Browse the repository at this point in the history
…ll "non-floating" objects.

- This patch allows `floating` objects to always be drawn in screencoordinates, and `non-floating` objects to always be drawn in world coordinates.
  • Loading branch information
Jason Oster committed Sep 26, 2012
1 parent f9dc7a4 commit 290d12c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,8 +998,22 @@ var me = me || {};
&& !obj.overlaps(rect)) {
continue;
}

if (!obj.floating) {
// translate object position to world coordinates
var pos = me.game.viewport.pos;

context.save();
context.translate(-pos.x, -pos.y);
}

// draw the object using the dirty area to be updated
obj.draw(context, rect);

if (!obj.floating) {
context.restore();
}

drawCount++;
}
// some debug stuff
Expand Down
4 changes: 1 addition & 3 deletions src/entity/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,9 +955,7 @@
if (me.debug.renderHitBox) {
// draw the sprite rectangle
context.strokeStyle = "blue";
context.strokeRect(this.pos.x - me.game.viewport.pos.x,
this.pos.y - me.game.viewport.pos.y,
this.width, this.height);
context.strokeRect(this.pos.x, this.pos.y, this.width, this.height);

this.collisionBox.draw(context);
}
Expand Down
4 changes: 2 additions & 2 deletions src/entity/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@
// sprite alpha value
context.globalAlpha = this.alpha;

// translate to screen coordinates
var xpos = ~~(this.pos.x - this.vp.pos.x), ypos = ~~(this.pos.y - this.vp.pos.y);
// clamp position vector to pixel grid
var xpos = ~~this.pos.x, ypos = ~~this.pos.y;

if ((this.scaleFlag) || (this.angle!==0)) {
// calculate pixel pos of the anchor point
Expand Down
4 changes: 1 addition & 3 deletions src/math/geometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,9 +566,7 @@
draw : function(context, color) {
// draw the rectangle
context.strokeStyle = color || "red";
context.strokeRect(this.left - me.game.viewport.pos.x,
this.top - me.game.viewport.pos.y, this.width,
this.height);
context.strokeRect(this.left, this.top, this.width, this.height);

}
});
Expand Down

0 comments on commit 290d12c

Please sign in to comment.