-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.js
107 lines (93 loc) · 3.42 KB
/
graphics.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// a very simple graphics library using HTML5 canvas features
// constructor for 2D coordinate
var Coord = function (x, y) {
return {x:x, y:y};
};
// constructor for color
var Color = function (red, green, blue) {
return {red: red, green: green, blue: blue};
};
// an abstraction for drawing in a canvas
var Pad = function (canvas) {
var DEFAULT_CIRCLE_RADIUS = 5;
var DEFAULT_LINE_WIDTH = 2;
var context = canvas.getContext('2d');
// ONLY CHANGE TO GRAPHICS LIBRARY - to adjust for the borders by 2px in each direction.
var width = canvas.width - 2;
var height = canvas.height - 2;
// sets the line width for subsequent drawing
var apply_line_width = function (ctx, line_width) {
ctx.lineWidth = (line_width) ? line_width : DEFAULT_LINE_WIDTH;
}
// sets the color for subsequent drawing and a default stroke width
var apply_color = function (ctx, color) {
if (color) {
ctx.strokeStyle = 'rgba(' + color.red + ',' + color.green + ',' + color.blue + ', 1)';
}
}
// sets the fill color for subsequent drawing
var apply_fill_color = function (ctx, color) {
if (color) {
ctx.fillStyle = 'rgba(' + color.red + ',' + color.green + ',' + color.blue + ', 1)';
ctx.fill();
}
}
// return the abstract object from the constructor
return {
// Draws a circle at the given coordinate (as returned by the
// Coord function) of the given radius (defaulting to
// DEFAULT_CIRCLE_RADIUS if the radius is 0 or omitted). An
// optional line width can be supplied (defaults to
// DEFAULT_LINE_WIDTH otherwise), as well as an optional color
// and fill color (both objects returned by the Color
// function).
draw_circle: function(coord, radius, line_width, color, fill_color) {
context.beginPath();
context.arc(coord.x, coord.y, (radius) ? radius : DEFAULT_CIRCLE_RADIUS, 0, Math.PI * 2, true);
context.closePath();
apply_line_width(context, line_width);
apply_color(context, color);
apply_fill_color(context, fill_color);
context.stroke();
},
// Draws a line between the given coordinates (as returned by
// the Coord function). An optional line width can be supplied
// (defaults to DEFAULT_LINE_WIDTH otherwise), as well as an
// optional color (returned by the Color function).
draw_line: function(from, to, line_width, color) {
context.beginPath();
context.moveTo(from.x, from.y);
context.lineTo(to.x, to.y);
apply_line_width(context, line_width);
apply_color(context, color);
context.lineWidth = (line_width) ? line_width : DEFAULT_LINE_WIDTH;
context.closePath();
context.stroke();
},
// Draws a rectangle starting at the top left corner (as
// returned by the Coord function) of the given width and
// height. An optional line width can be supplied (defaults to
// DEFAULT_LINE_WIDTH otherwise), as well as an optional color
// and fill color (both returned by the Color function).
draw_rectangle: function(top_left, width, height, line_width, color, fill_color) {
context.beginPath();
context.rect(top_left.x, top_left.y, width, height);
apply_line_width(context, line_width);
apply_color(context, color);
apply_fill_color(context, fill_color);
context.closePath();
context.stroke();
},
// Clears the entire board
clear: function() {
context.clearRect(0, 0, width, height);
},
// return width and height of the drawing area
get_width: function() {
return width;
},
get_height: function() {
return height;
}
}
}