-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.js
120 lines (94 loc) · 1.94 KB
/
new.js
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
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var A=[];
A.push({
x:305,
y:55
});
var B=[];
B.push({
x:105,
y:505
});
var C=[];
C.push({
x:505,
y:455
});
var P=[];
P.push({
x:5,
y:5
});
drawRect();
drawText();
randPoint();
//fdg();
setInterval(fdg,1);
/*console.log(A[0].x);
console.log(B);
console.log(C);
console.log(P);*/
function drawRect(){
ctx.fillStyle = "black";
ctx.fillRect(300,50,5,5);
ctx.fillStyle = "black";
ctx.fillRect(100,500,5,5);
ctx.fillStyle = "black";
ctx.fillRect(500,450,5,5);
}
function drawText(){
ctx.fillStyle="red";
ctx.font = "30px Arial";
ctx.fillText("A",305,45);
ctx.fillStyle="red";
ctx.font = "30px Arial";
ctx.fillText("B",95,535);
ctx.fillStyle="red";
ctx.font = "30px Arial";
ctx.fillText("C",505,465);
}
function randPoint(){
ctx.fillStyle = "black";
ctx.fillRect(0,0,5,5);
}
function fdg(){
var midPoint=[];
var rand = getRandom();
if(rand==1||rand==2){
midPoint.push({
x:(A[0].x + P[0].x)/2,
y:(A[0].y + P[0].y)/2
});
ctx.fillStyle="green";
ctx.fillRect(midPoint[0].x-1,midPoint[0].y-1,1,1);
P[0].x=midPoint[0].x;
P[0].y=midPoint[0].y;
}
if(rand==3||rand==4){
midPoint.push({
x:(B[0].x + P[0].x)/2,
y:(B[0].y + P[0].y)/2
});
ctx.fillStyle="green";
ctx.fillRect(midPoint[0].x-1,midPoint[0].y-1,1,1);
P[0].x=midPoint[0].x;
P[0].y=midPoint[0].y;
}
if(rand==5||rand==6){
midPoint.push({
x:(C[0].x + P[0].x)/2,
y:(C[0].y + P[0].y)/2
});
ctx.fillStyle="green";
ctx.fillRect(midPoint[0].x-1,midPoint[0].y-1,1,1);
P[0].x=midPoint[0].x;
P[0].y=midPoint[0].y;
}
//console.log(P);
}
function getRandom(){
var max = 6;
var random = Math.floor((Math.random()*max)+1);
return(random);
}