-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.js
61 lines (52 loc) · 1.44 KB
/
canvas.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
function Canvas() {
this.canvas = document.getElementById('canvas');
this.context = this.canvas.getContext('2d');
this.colors = ['#C03', 'DarkKhaki', 'yellow', 'grey', 'pink',
'green', 'orange', 'brown', 'Magenta', 'DeepSkyBlue'];
}
Canvas.prototype.drawGrid = function(color, stepx, stepy) {
var ctx = this.context;
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = 0.5;
for (var i = stepx + 0.5; i < ctx.canvas.width; i += stepx) {
ctx.beginPath();
ctx.moveTo(i, 0);
ctx.lineTo(i, ctx.canvas.height);
ctx.stroke();
ctx.closePath();
}
for (var i = stepy + 0.5; i < ctx.canvas.height; i += stepy) {
ctx.beginPath();
ctx.moveTo(0, i);
ctx.lineTo(ctx.canvas.width, i);
ctx.stroke();
ctx.closePath();
}
ctx.restore();
};
Canvas.prototype.drawBall = function(ball) {
var ctx = this.context;
var m = 50 * ball.x + 25,
n = 50 * ball.y + 25;
ctx.save();
ctx.fillStyle = '#C03';
ctx.fillStyle = this.colors[ball.color];
ctx.shadowColor = 'black';
ctx.shadowOffsetX = 4;
ctx.shadowOffsetY = 4;
ctx.shadowBlur = 12;
ctx.beginPath();
ctx.arc(m, n, 23, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
ctx.restore();
};
Canvas.prototype.drawCheckBox = function(m, n) {
var ctx = this.context;
ctx.save();
ctx.strokeStyle = 'rgba(0, 0, 0, 0.4)';
ctx.strokeRect(m-25, n-25, 50, 50);
ctx.restore();
};