8
8
< canvas id ="canvas " width ="200 " height ="200 "> </ canvas >
9
9
10
10
< script >
11
+
11
12
var canvas = document . getElementById ( "canvas" ) ;
12
13
var ctx = canvas . getContext ( "2d" ) ;
13
14
14
- var position = 0 ;
15
+ var x = 100 ;
16
+ var y = 100 ;
15
17
16
18
setInterval ( function ( ) {
17
19
ctx . clearRect ( 0 , 0 , 200 , 200 ) ;
18
- ctx . fillRect ( position , 0 , 20 , 20 ) ;
19
20
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 ) ;
24
26
} , 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);
25
79
</ script >
26
80
</ body >
27
81
</ html >
0 commit comments