-
Notifications
You must be signed in to change notification settings - Fork 0
/
can_draw.js
93 lines (76 loc) · 1.99 KB
/
can_draw.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
function runMaker(cd)
{
return function() { cd.renderFrame(); }
}
function CanDraw(id)
{
this.dom_item = document.getElementById(id);
this.ctx = this.dom_item.getContext('2d');
this.animateObjects = function() {}
this.drawObjects = function() {}
this.xSize = this.dom_item.width;
this.ySize = this.dom_item.height;
this.debug = false;
this.runner = runMaker(this)
}
CanDraw.prototype.beginFrame = function()
{
this.ctx.fillStyle = 'white';
this.ctx.fillRect(0,0,this.xSize, this.ySize);
}
CanDraw.prototype.endFrame = function()
{
}
CanDraw.prototype.renderFrame = function()
{
this.animateObjects();
this.beginFrame();
this.drawObjects();
this.endFrame();
}
CanDraw.prototype.run = function()
{
window.setInterval(this.runner, 0);
}
CanDraw.prototype.circle = function(x, y, rad, stroke, fill)
{
this.ctx.beginPath();
this.ctx.arc(x, y, rad, 0, Math.PI * 2, false);
if (fill) this.ctx.fill();
if (stroke) this.ctx.stroke();
}
CanDraw.prototype.line = function (x1, y1, x2, y2)
{
this.ctx.beginPath();
this.ctx.moveTo(x1,y1);
this.ctx.lineTo(x2,y2);
this.ctx.stroke();
}
CanDraw.prototype.setLineWidth = function(w)
{
this.ctx.lineWidth = w;
}
CanDraw.prototype.setLineCap = function(c)
{
this.ctx.lineCap = c;
}
CanDraw.prototype.setLineJoin = function(j)
{
this.ctx.lineJoin = j;
}
CanDraw.prototype.rectangle = function(x, y, w, h, stroke, fill)
{
if (stroke) this.ctx.strokeRect(x, y, w, h);
if (fill) this.ctx.fillRect(x,y,w,h)
}
CanDraw.prototype.setFillRGBA = function(r,g,b,a)
{
canDraw.ctx.fillStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}
CanDraw.prototype.setStrokeRGBA = function(r,g,b,a)
{
canDraw.ctx.strokeStyle = 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
}
CanDraw.prototype.getDomItem = function() { return this.dom_item; }
CanDraw.prototype.getXSize = function() { return this.xSize;}
CanDraw.prototype.getYSize = function() { return this.ySize;}