Skip to content

Commit a6e1634

Browse files
author
Em01
committed
amended for a moving bee
1 parent 99cb5ab commit a6e1634

File tree

1 file changed

+60
-6
lines changed

1 file changed

+60
-6
lines changed

canvasanimated/canvasanimation.html

+60-6
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,74 @@
88
<canvas id="canvas" width="200" height="200"></canvas>
99

1010
<script>
11+
1112
var canvas = document.getElementById("canvas");
1213
var ctx = canvas.getContext("2d");
1314

14-
var position = 0;
15+
var x = 100;
16+
var y = 100;
1517

1618
setInterval(function () {
1719
ctx.clearRect(0, 0, 200, 200);
18-
ctx.fillRect(position, 0, 20, 20);
1920

20-
position++;
21-
if (position > 200) {
22-
position = 0;
23-
}
21+
drawBee(x, y);
22+
x = update(x);
23+
y = update(y);
24+
25+
ctx.strokeRect(0, 0, 200, 200);
2426
}, 30);
27+
28+
var circle = function (x, y, radius, fillCircle) {
29+
ctx.beginPath();
30+
ctx.arc(x, y, radius, 0, Math.PI * 2, false);
31+
if (fillCircle) {
32+
ctx.fill();
33+
} else {
34+
ctx.stroke();
35+
}
36+
};
37+
38+
var drawBee = function (x, y) {
39+
ctx.lineWidth = 2;
40+
ctx.strokeStyle = "Black";
41+
ctx.fillStyle = "Gold";
42+
43+
circle(x, y, 8, true);
44+
circle(x, y, 8, false);
45+
circle(x - 5, y - 11, 5, false);
46+
circle(x + 5, y - 11, 5, false);
47+
circle(x - 2, y - 1, 2, false);
48+
circle(x + 2, y - 1, 2, false);
49+
};
50+
var update = function (coordinate) {
51+
var offset = Math.random() * 4 - 2;
52+
coordinate += offset;
53+
54+
if (coordinate > 200) {
55+
coordinate = 200;
56+
}
57+
if (coordinate < 0) {
58+
coordinate = 0;
59+
}
60+
61+
return coordinate;
62+
};
63+
64+
65+
// var canvas = document.getElementById("canvas");
66+
// var ctx = canvas.getContext("2d");
67+
68+
// var position = 0;
69+
70+
// setInterval(function () {
71+
// ctx.clearRect(0, 0, 200, 200);
72+
// ctx.fillRect(position, 0, 20, 20);
73+
74+
// position++;
75+
// if (position > 200) {
76+
// position = 0;
77+
// }
78+
// }, 30);
2579
</script>
2680
</body>
2781
</html>

0 commit comments

Comments
 (0)