UNPKG

1.66 kBJavaScriptView Raw
1"use strict";
2
3var Entity = require("./entity");
4
5/**
6 * A basic camera. It's really an {@link Splat.Entity}, so you can control it in the same way.
7 * By changing {@link Splat.Entity#x} and {@link Splat.Entity#y} you control what portion of the canvas is viewable.
8 * For example, if the Camera is at 50,50, and you draw a rectangle at 200,200,
9 * it will appear on the screen at 150,150.
10 * @constructor
11 * @augments Splat.Entity
12 * @alias Splat.Camera
13 * @param {number} x The top-left x coordinate
14 * @param {number} y The top-left y coordinate
15 * @param {number} width The width on the x-axis. Currently doesn't do anything.
16 * @param {number} height The height on the y-axis. Currently doesn't do anything.
17 */
18function Camera(x, y, width, height) {
19 Entity.call(this, x, y, width, height);
20}
21Camera.prototype = Object.create(Entity.prototype);
22/**
23 * Offset all following draw operations on the canvas.
24 * This is automatically called for you by {@link Splat.Scene}.
25 * @param {external:CanvasRendingContext2D} context The context to offset
26 */
27Camera.prototype.draw = function(context) {
28 context.translate(-(this.x|0), -(this.y|0));
29};
30/**
31 * Draw on the canvas at not-offset coordinates.
32 * For example, if the camera is at 50,50 and you draw a rectangle at 200,200
33 * it will appear on the screen at 200,200.
34 * @param {external:CanvasRendingContext2D} context The context to offset
35 * @param {drawCallback} drawFunc The callback the performs the non-offset drawing.
36 */
37Camera.prototype.drawAbsolute = function(context, drawFunc) {
38 context.save();
39 context.translate(this.x|0, this.y|0);
40 drawFunc();
41 context.restore();
42};
43
44module.exports = Camera;