Enjin

Camera

function
 Camera() 

Option name Type Description
X Number

X coordinate of camera center

Y Number

Y coordinate of camera center

Scale Number

Scale of camera

Rotation Number

Rotation of camera (in radians)

return Object

A Camera Object with the given Params

Create a new Camera instance

var Camera = function(x, y, scale, rotation) {
	if(!enjin.ctx) {
		console.error("enjin needs a rendering context before it can have a Camera, did you enjin.atach(canvas)?");
		return;
	}
	//everything needed for default camera
	this.x = x || 0;
	this.y = y || 0;
	this.scale = scale || 1;
	this.rotation = rotation || 0;
	this.layers = {
		"main": new this.Layer(1)
	};
}

Layer

method
 Camera.prototype.Layer() 

Option name Type Description
TranslationScale Number

Scalar to mulitply translation by

Create a new Layer instance

Camera.prototype.Layer = function(translationScale) {
	this.translationScale = translationScale;
}

apply

method
 Camera.prototype.Layer.prototype.apply() 

Option name Type Description
Camera Object

Camera whose positioning we should apply

Apply Layer's positioning (Don't call this directly, use Camera.apply("LayerName"))

Camera.prototype.Layer.prototype.apply = function(camera) {
	var centerX = enjin.width/(2*camera.scale);
	var centerY = enjin.height/(2*camera.scale);

	enjin.ctx.save();
	enjin.ctx.scale(camera.scale, camera.scale);
	enjin.ctx.translate(centerX, centerY);
	enjin.ctx.rotate(camera.rot);
	enjin.ctx.translate(-camera.x * this.translationScale, -camera.y * this.translationScale);
};

newLayer

method
 Camera.prototype.newLayer() 

Option name Type Description
LayerName String

Name of layer 2param {Number} TranslationScale Scalar to mulitply translation by

Create a new Camera Layer the easy way

Camera.prototype.newLayer = function(layerName, translationScale) {
	this.layers[layerName] = new this.Layer(translationScale);
};

apply

method
 Camera.prototype.apply() 

Option name Type Description
LayerName String

Name of Layer to apply

Apply Camera's positioning

Camera.prototype.apply = function(layerName) {
	if(layerName) { // If we're given a specific layer to draw
		this.layers[layerName].apply(this);
	}
	else { // Otherwise just draw the main layer
		this.layers["main"].apply(this);
	}
}

remove

method
 Camera.prototype.remove() 

Remove Camera's positioning

Camera.prototype.remove = function() {
	// Remove is a pretty generic call and so it doesn't nee to be the Camera.Layer class
	enjin.ctx.restore();
}

moveTo

method
 Camera.prototype.moveTo() 

Option name Type Description
X Number

X coordinate of Camera

Y Number

Y coordinate of Camera

Move Camera to a specific coordinate

Camera.prototype.moveTo = function(x, y) {
 	this.x = x;
 	this.y = y;
}

move

method
 Camera.prototype.move() 

Option name Type Description
DX Number

Distance to move in the X direction

DY Number

Distance to move in the Y direction

Move Camera a certain distance

Camera.prototype.move = function(dx, dy) {
	this.x = this.x + dx;
	this.y = this.y + dy;
}

rotate

method
 Camera.prototype.rotate() 

Option name Type Description
Radians Number

Amount to rotate in radians

Rotate Camera a certain amount in radians

Camera.prototype.rotate = function(rad) {
	this.rotation = this.rotation + rad;
}

rotateTo

method
 Camera.prototype.rotateTo() 

Option name Type Description
Radians Number

Radians to rotate to

Rotate Camera to a specific value

Camera.prototype.rotateTo = function(rad) {
	this.rotation = rad;
}

scaleBy

method
 Camera.prototype.scaleBy() 

Option name Type Description
Scalar Number

Number to multiply the Camera's scale by

Scale Camera by a certain amount

Camera.prototype.scaleBy = function(scalar) {
	this.scale = this.scale * scalar;
}

scaleTo

method
 Camera.prototype.scaleTo() 

Option name Type Description
Scale Number

Number to scale Camera to

Scale Camera to a specific value

Camera.prototype.scaleTo = function(scale) {
	this.scale = scale;
}

scaleToPoint

method
 Camera.prototype.scaleToPoint() 

Option name Type Description
Scalar Number

Number to multiply the Camera's scale by

PointX Number

X coordinate to scale to

PointY Number

Y coordinate to scale to

Scale Camera by a certain amount to a specific point

Camera.prototype.scaleToPoint = function(scalar, pointx, pointy) {
	var relCenterX = enjin.canvas.width/2 - pointx
	var relCenterY = enjin.canvas.height/2 - pointy
	var moveX = (relCenterX/(this.scale * scalar) - relCenterX/this.scale);
	var moveY = (relCenterY/(this.scale * scalar) - relCenterY/this.scale);
	this.move(moveX, moveY);
	this.scaleBy(scalar);
}

toMapCoords

method
 Camera.prototype.toMapCoords() 

Option name Type Description
X Number

X coordinate of camera you want to convert

X Number

Y coordinate of camera you want to convert

return Object

X and Y value in Map coordinates

Convert Camera coordinates to Map coordinates

Camera.prototype.toMapCoords = function(x, y) {
	x = (x - this.canvas.getWidth/2) / this.scale;
	y = (y - this.canvas.getHeight/2) / this.scale;

	var cos = Math.cos(-this.rotation);
	var sin = Math.sin(-this.rotation);

	x = cos*x - sin*y;
	y = sin*x + cos*y;

	return {
		x: x+this.x,
		y: y+this.y
	}
}

toCameraCoords

method
 Camera.prototype.toCameraCoords() 

Option name Type Description
X Number

X coordinate of Map you want to convert

Y Number

Y coordinate of Map you want to convert

return Object

X and Y value in Camera coordinates

Convert Map coordinates to Camera coordinates

Camera.prototype.toCameraCoords = function(x, y) {
	x = x - this.x;
	y = y - this.y;
	var cos = Math.cos(this.rotation);
	var sin = Math.sin(this.rotation);

	x = cos*x - sin*y;
	y = sin*x + cos*y;

	return {
		x: x*this.scale + this.canvas.getWidth/2,
		y: y*this.scale + this.canvas.getHeight/2
	}
}

module.exports = Camera;