-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.js
119 lines (107 loc) · 2.91 KB
/
shapes.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
108
109
110
111
112
113
114
115
116
117
118
/////////////////// DRAWING A BASIC SHAPE /////////////////
// NOTE: if colors are not updating correctly when strokeStyle() is called,
// then make sure all the beginPath() and closePath() have defined
// the paths correctly.
function outlineCircle( _x, _y, _radius, _color) {
ctx.strokeStyle = _color;
ctx.beginPath();
ctx.arc( _x, _y, _radius, 0, 2*Math.PI);
ctx.closePath();
ctx.stroke();
}
function fillCircle( _x, _y, _radius, _color) {
ctx.fillStyle = _color;
ctx.beginPath();
ctx.arc( _x, _y, _radius, 0, 2*Math.PI);
ctx.stroke();
ctx.fill();
}
function outlineRect( _x, _y, _w, _h, _color) {
ctx.strokeStyle = _color;
ctx.beginPath();
ctx.strokeRect(_x, _y, _w, _h);
ctx.closePath();
}
function fillRect( _x, _y, _w, _h, _color) {
ctx.fillStyle = _color;
ctx.beginPath();
ctx.rect(_x, _y, _w, _h);
ctx.closePath();
ctx.fill();
}
// _x, _y is the right angle corner
// _w, _h are the length of the legs
function outlineRightTriangle( _x, _y, _w, _h, _color) {
ctx.strokeStyle = _color;
ctx.beginPath();
ctx.moveTo(_x,_y);
ctx.lineTo(_x+_w,_y);
ctx.lineTo(_x,_y-_h);
ctx.closePath();
ctx.stroke();
}
function fillRightTriangle( _x, _y, _w, _h, _color) {
ctx.fillStyle = _color;
ctx.beginPath();
ctx.moveTo(_x,_y);
ctx.lineTo(_x+_w,_y);
ctx.lineTo(_x,_y-_h);
ctx.closePath();
ctx.fill();
}
// _x, _y is the top corner of an equilateral triangle with the point on top
// _w is the width of the base
// _flipUpsideDown is a boolean. If true, then triangle is upside down, with
// one corner pointing down.
function outlineEquilateralTriangle( _x, _y, _w, _color, _flipUpsideDown=false) {
ctx.strokeStyle = _color;
ctx.beginPath();
ctx.moveTo(_x,_y);
if (_flipUpsideDown) {
y2 = _y - _w * Math.sqrt(3)/2;
} else {
y2 = _y + _w * Math.sqrt(3)/2;
}
ctx.lineTo(_x+_w/2, y2);
ctx.lineTo(_x-_w/2, y2);
ctx.closePath();
ctx.stroke();
}
// _x, _y is the top corner of an equilateral triangle with the point on top
// _w is the width of the base
// _flipUpsideDown is a boolean. If true, then triangle is upside down, with
// one corner pointing down.
function fillEquilateralTriangle( _x, _y, _w, _color, _flipUpsideDown=false) {
ctx.fillStyle = _color;
ctx.beginPath();
ctx.moveTo(_x,_y);
if (_flipUpsideDown) {
y2 = _y - _w * Math.sqrt(3)/2;
} else {
y2 = _y + _w * Math.sqrt(3)/2;
}
ctx.lineTo(_x+_w/2, y2);
ctx.lineTo(_x-_w/2, y2);
ctx.closePath();
ctx.fill();
}
function outlineTriangle( _x, _y, _x2, _y2, _x3, _y3, _color) {
ctx.strokeStyle = _color;
ctx.beginPath();
ctx.moveTo(_x,_y);
ctx.lineTo(_x2,_y2);
ctx.lineTo(_x3,_y3);
ctx.closePath();
ctx.stroke();
}
// fills and outlines the triangle
function fillTriangle( _x, _y, _x2, _y2, _x3, _y3, _color) {
ctx.beginPath();
ctx.moveTo(_x,_y);
ctx.lineTo(_x2,_y2);
ctx.lineTo(_x3,_y3);
ctx.closePath();
ctx.stroke();
ctx.fillStyle = _color;
ctx.fill();
}