function Camera() { this.x = 0; this.y = 0; this.scale = 1; this.viewBox = {x: 100, y: 100, width: 600, height: 400}; this.padding = 10; this.init = function(canvas) { this.viewBox.x = canvas.width / 3; this.viewBox.y = canvas.height / 3; this.viewBox.width = canvas.width - 2 * this.viewBox.x; this.viewBox.height = canvas.height - 2 * this.viewBox.y; } this.update = function(canvas, player, delta) { this.followPlayer(player, delta); } this.handleResize = function(canvas) { this.viewBox.x = canvas.width / 3; this.viewBox.y = canvas.height / 3; this.viewBox.width = canvas.width - 2 * this.viewBox.x; this.viewBox.height = canvas.height - 2 * this.viewBox.y; } this.followPlayer = function(player) { var truePos = {x: 0, y: 0}; truePos.x = this.x + player.position.x; truePos.y = this.y + player.position.y; if(truePos.x <= this.viewBox.x) { this.x -= truePos.x - this.viewBox.x; } if(player.position.x + this.x >= this.viewBox.x + this.viewBox.width) { this.x += (this.viewBox.x + this.viewBox.width) - truePos.x; } if(truePos.y <= this.viewBox.y) { this.y -= truePos.y - this.viewBox.y; } if(player.position.y + this.y >= this.viewBox.y + this.viewBox.height) { this.y += (this.viewBox.y + this.viewBox.height) - truePos.y; } } };