-
Notifications
You must be signed in to change notification settings - Fork 20
/
Drawing.js
75 lines (69 loc) · 1.98 KB
/
Drawing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* @fileoverview Class handling the drawing of objects in the game.
* @author kennethli.3470@gmail.com (Kenneth Li)
*/
/**
* Creates a Drawing object.
* @param {CanvasRenderingContext2D} context The context this object will
* draw to.
* @constructor
*/
function Drawing(context) {
this.context = context;
}
/**
* This is a factory method for creating a Drawing object.
* @param {CanvasRenderingContext2D} context The context this object will
* draw to.
* @return {Drawing}
*/
Drawing.create = function(context) {
return new Drawing(context);
};
/**
* This method creates and returns an Image object.
* @param {string} src The path to the image
* @param {number} width The width of the image in pixels
* @param {number} height The height of the image in pixels
* @return {Image}
*/
Drawing.createImage = function(src, width, height) {
var image = new Image(width, height);
image.src = src;
return image;
};
/**
* Clears the canvas context.
*/
Drawing.prototype.clear = function() {
var canvas = this.context.canvas;
this.context.clearRect(0, 0, canvas.width, canvas.height);
};
/**
* Draws the player's sprite as a red circle.
* @param {number} x The x coordinate of the player
* @param {number} y The y coordinate of the player
* @param {number} size The radial size of the player
*/
Drawing.prototype.drawSelf = function(x, y, size) {
this.context.save();
this.context.beginPath();
this.context.fillStyle = 'green';
this.context.arc(x, y, size, 0, Math.PI * 2);
this.context.fill();
this.context.restore();
};
/**
* Draws other players' sprite as a red circle.
* @param {number} x The x coordinate of the player
* @param {number} y The y coordinate of the player
* @param {number} size The radial size of the player
*/
Drawing.prototype.drawOther = function(x, y, size) {
this.context.save();
this.context.beginPath();
this.context.fillStyle = 'red';
this.context.arc(x, y, size, 0, Math.PI * 2);
this.context.fill();
this.context.restore();
};