Appearance
html
<!DOCTYPE html>
<html lang="en">
<head>
<title>TweetShirt</title>
<meta charset="utf-8">
<style type="text/css">
canvas {
border: 1px solid black;
}
</style>
<script>
window.onload = function () {
var button = document.getElementById("previewButton");
button.onclick = previewHandler;
function previewHandler() {
var canvas = document.getElementById("tshirtCanvas");
var context = canvas.getContext("2d");
fillBackgroundColor(canvas, context);
var selectObj = document.getElementById("shape");
var index = selectObj.selectedIndex;
var shape = selectObj[index].value;
//判断是方形还是圆形
if (shape == "squares") {
for (var squares = 10; squares < 12; squares++) {
drawSquare(canvas, context);
}
} else if (shape == "circles") {
for (var circles = 0; circles < 20; circles++) {
drawCircle(canvas, context);
}
}
}
function fillBackgroundColor(canvas, context) {
var selectObj = document.getElementById("backgroundColor");
var index = selectObj.selectedIndex;
var bgColor = selectObj.options[index].value;
context.fillStyle = bgColor;
context.fillRect(0, 0, canvas.width, canvas.height);
}
//封装方形
function drawSquare(canvas, context) {
var w = Math.floor(Math.random() * 40);
var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height);
context.fillStyle = "blue";
context.fillRect(x, y, w, w);
}
//封装圆形
function drawCircle(canvas, context) {
var radius = Math.floor(Math.random() * 40);
var x = Math.floor(Math.random() * canvas.width);
var y = Math.floor(Math.random() * canvas.height);
context.beginPath();
context.arc(x, y, radius, 0, degreesToRadians(360), true);
context.fillStyle = "rgba(0,0,0, 0.6)";
context.fill();
}
function degreesToRadians(degrees) {
return (degrees * Math.PI) / 180;
}
}
</script>
</head>
<body>
<canvas id="tshirtCanvas" width="600" height="200">
<p>Please update your brower to use TweetShirt</p>
</canvas>
<form>
<p>
<label for="backgroundColor">Select background color:</label>
<select id="backgroundColor">
<option value="white" selected="selected">White</option>
<option value="black">Black</option>
</select>
</p>
<p>
<label for="shape">Circles or squares?</label>
<select id="shape">
<option value="none" selected="selected">Neither</option>
<option value="circles">Circles</option>
<option value="squares">squares</option>
</select>
</p>
<p>
<input type="button" name="previewButton" id="previewButton" value="Preview">
</p>
</form>
</body>
</html>
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
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