-
Notifications
You must be signed in to change notification settings - Fork 1
/
drawing_on_canvas.html
137 lines (128 loc) · 3.74 KB
/
drawing_on_canvas.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
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
<html>
<body style="font-family: Calibri">
<ol>
<li><h3>Shapes</h3></li>
<canvas width="350" height="200" id="1"></canvas>
<script>
var cx = document.getElementById("1").getContext("2d");
function trapeze(x, y) {
cx.beginPath();
cx.moveTo(x, y);
cx.lineTo(x+50, y);
cx.lineTo(x+70, y+40);
cx.lineTo(x-20, y+40);
cx.closePath();
cx.stroke();
}
trapeze(20,10);
cx.save();
function diamond(x, y, a) {
cx.translate(x, y);
cx.rotate(0.25*Math.PI);
cx.fillStyle = "red";
cx.fillRect(0, 0, a, a);
}
diamond(150, 0, 50);
cx.restore();
function zigzag(x, y, n) {
cx.beginPath();
cx.moveTo(x, y);
for (var i = 0; i < n; i++) {
cx.lineTo(x+50, y+5+i*10);
cx.lineTo(x, y+10+i*10);
}
cx.stroke();
}
zigzag(10, 90, 8);
function spiral(x, y) {
var radius = 50, xCenter = x + radius, yCenter = y + radius;
cx.beginPath();
cx.moveTo(xCenter, yCenter);
for (var i = 0; i < 100; i++) {
var angle = i * Math.PI / 15;
var dist = radius * i / 100;
cx.lineTo(xCenter + Math.cos(angle) * dist, yCenter + Math.sin(angle) * dist);
}
cx.stroke();
}
spiral(100, 80);
function star(x, y) {
var radius = 50, xCenter = x + radius, yCenter = y + radius;
cx.beginPath();
cx.moveTo(xCenter + radius, yCenter);
for (var i = 1; i < 9; i++) {
var angle = i * Math.PI / 4;
cx.quadraticCurveTo(xCenter, yCenter, xCenter + Math.cos(angle) * radius, yCenter + Math.sin(angle) * radius);
}
cx.fillStyle = "#ffc700";
cx.fill();
}
star(220, 30);
</script>
<li><h3>The pie chart</h3></li>
<canvas width="600" height="300" id="2"></canvas>
<script>
var results = [
{name: "Satisfied", count: 1043, color: "lightblue"},
{name: "Neutral", count: 563, color: "lightgreen"},
{name: "Unsatisfied", count: 510, color: "pink"},
{name: "No comment", count: 175, color: "silver"}
];
var cx = document.getElementById("2").getContext("2d");
var total = results.reduce(function(sum, choice) {
return sum + choice.count;
}, 0);
var currentAngle = -0.5 * Math.PI;
var centerX = 300, centerY = 150, radius = 100;
// Add code to draw the slice labels in this loop.
results.forEach(function(result) {
var sliceAngle = (result.count / total) * 2 * Math.PI;
cx.beginPath();
cx.arc(centerX, centerY, radius,
currentAngle, currentAngle + sliceAngle);
var middleAngle = currentAngle + 0.5 * sliceAngle;
var textLocX = Math.cos(middleAngle) * (radius+10) + centerX;
var textLocY = Math.sin(middleAngle) * (radius+10) + centerY;
if (Math.cos(middleAngle) > 0)
cx.textAlign = "left";
else
cx.textAlign = "right";
cx.fillStyle = "black";
cx.fillText(result.name, textLocX, textLocY);
currentAngle += sliceAngle;
cx.lineTo(centerX, centerY);
cx.fillStyle = result.color;
cx.fill();
});
</script>
<li><h3>A bouncing ball</h3></li>
<canvas width="300" height="300" id ="3"></canvas>
<script>
var cx = document.getElementById("3").getContext("2d");
var lastTime = null;
function frame(time) {
if (lastTime != null)
updateAnimation(Math.min(100, time - lastTime) / 750);
lastTime = time;
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
var x = 100, y = 200, radius = 20, speedX = 92, speedY = 73;
function updateAnimation(step) {
cx.clearRect(0, 0, 300, 300);
cx.strokeRect(0, 0, 300, 300);
x += step * speedX;
y += step * speedY;
if (x < radius || x > 300 - radius)
speedX = -speedX;
if (y < radius || y > 300 - radius)
speedY = -speedY;
cx.fillStyle = "blue";
cx.beginPath();
cx.arc(x, y, radius, 0, 2*Math.PI);
cx.fill(); }
</script>
</ol>
</body>
<input type="button" value="Refresh Page" onClick="window.location.reload()">
</html>