HTML5 Canvas Playing Card Suits

Open in new window
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function drawSpade(context, x, y, width, height){
    context.save();
    var bottomWidth = width * 0.7;
    var topHeight = height * 0.7;
    var bottomHeight = height * 0.3;
 
    context.beginPath();
    context.moveTo(x, y);
 
    // top left of spade          
    context.bezierCurveTo(
		x, y + topHeight / 2, // control point 1
    	x - width / 2, y + topHeight / 2, // control point 2
    	x - width / 2, y + topHeight // end point
    );
 
    // bottom left of spade
    context.bezierCurveTo(
		x - width / 2, y + topHeight * 1.3, // control point 1
    	x, y + topHeight * 1.3, // control point 2
    	x, y + topHeight // end point
    );
 
    // bottom right of spade
    context.bezierCurveTo(
		x, y + topHeight * 1.3, // control point 1
    	x + width / 2, y + topHeight * 1.3, // control point 2
    	x + width / 2, y + topHeight // end point
    );
 
    // top right of spade
    context.bezierCurveTo(
		x + width / 2, y + topHeight / 2, // control point 1
    	x, y + topHeight / 2, // control point 2
    	x, y // end point
    );
 
    context.closePath();
    context.fill();
 
    // bottom of spade
    context.beginPath();
    context.moveTo(x, y + topHeight);
    context.quadraticCurveTo(
		x, y + topHeight + bottomHeight, // control point
    	x - bottomWidth / 2, y + topHeight + bottomHeight // end point
    );
    context.lineTo(x + bottomWidth / 2, y + topHeight + bottomHeight);
    context.quadraticCurveTo(
		x, y + topHeight + bottomHeight, // control point
    	x, y + topHeight // end point
    );
    context.closePath();
    context.fillStyle = "black";
    context.fill();
    context.restore();
}
 
function drawHeart(context, x, y, width, height){
	context.save();
    context.beginPath();
	var topCurveHeight = height * 0.3;
    context.moveTo(x, y + topCurveHeight);
    // top left curve
    context.bezierCurveTo(
		x, y, 
		x - width / 2, y, 
		x - width / 2, y + topCurveHeight
	);
 
    // bottom left curve
    context.bezierCurveTo(
		x - width / 2, y + (height + topCurveHeight) / 2, 
		x, y + (height + topCurveHeight) / 2, 
		x, y + height
	);
 
    // bottom right curve
    context.bezierCurveTo(
		x, y + (height + topCurveHeight) / 2, 
		x + width / 2, y + (height + topCurveHeight) / 2, 
		x + width / 2, y + topCurveHeight
	);
 
    // top right curve
    context.bezierCurveTo(
		x + width / 2, y, 
		x, y, 
		x, y + topCurveHeight
	);
 
    context.closePath();
    context.fillStyle = "red";
    context.fill();
	context.restore();
}
 
function drawClub(context, x, y, width, height){
	context.save();
	var circleRadius = width * 0.3;
	var bottomWidth = width * 0.5;
	var bottomHeight = height * 0.35;
    context.fillStyle = "black";
 
    // top circle
    context.beginPath();
    context.arc(
		x, y + circleRadius + (height * 0.05), 
		circleRadius, 0, 2 * Math.PI, false
	);
    context.fill();
 
    // bottom right circle
    context.beginPath();
    context.arc(
		x + circleRadius, y + (height * 0.6), 
		circleRadius, 0, 2 * Math.PI, false
	);
    context.fill();
 
    // bottom left circle
    context.beginPath();
    context.arc(
		x - circleRadius, y + (height * 0.6), 
		circleRadius, 0, 2 * Math.PI, false
	);
    context.fill();
 
    // center filler circle
    context.beginPath();
    context.arc(
		x, y + (height * 0.5), 
		circleRadius / 2, 0, 2 * Math.PI, false
	);
    context.fill();
 
    // bottom of club
    context.moveTo(x, y + (height * 0.6));
    context.quadraticCurveTo(
		x, y + height, 
		x - bottomWidth / 2, y + height
	);
    context.lineTo(x + bottomWidth / 2, y + height);
    context.quadraticCurveTo(
		x, y + height, 
		x, y + (height * 0.6)
	);
    context.closePath();
    context.fill();
	context.restore();
}
 
function drawDiamond(context, x, y, width, height){
	context.save();
    context.beginPath();
    context.moveTo(x, y);
 
    // top left edge
    context.lineTo(x - width / 2, y + height / 2);
 
    // bottom left edge
    context.lineTo(x, y + height);
 
    // bottom right edge
    context.lineTo(x + width / 2, y + height / 2);
 
    // closing the path automatically creates
    // the top right edge
    context.closePath();
 
    context.fillStyle = "red";
    context.fill();
	context.restore();
}
 
window.onload = function(){
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");
 
	var suitY = canvas.height / 2 - 50;
    drawSpade(context, canvas.width * 0.2, suitY, 75, 100);
    drawHeart(context, canvas.width * 0.4, suitY, 75, 100);
    drawClub(context, canvas.width * 0.6, suitY, 75, 100);
    drawDiamond(context, canvas.width * 0.8, suitY, 75, 100);
};
Modified on November 6th, 2011 by Eric Rowell
comments powered by Disqus