-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
121 lines (118 loc) · 2.94 KB
/
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
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<title>Canvas exercise</title>
<script type="text/javascript">
var e;
var w;
var delay;
var moder;
var threshold;
var vpanel = new Array(w);
var timer;
function init(){
clearInterval(timer);
e = parseInt(document.getElementById("e").value);
w = 750/e;
delay = parseInt(document.getElementById("delay").value);
moder = parseInt(document.getElementById("moder").value);
threshold = parseInt(document.getElementById("threshold").value);
var c = document.getElementById("myPanel");
var panel = c.getContext("2d");
for(var i=0;i<w;i++){
vpanel[i] = new Array(w);
for(var j=0;j<w;j++)
vpanel[i][j]=0;
}
//init
for(var i=0;i<w;i++){
for(var j=0;j<w;j++){
var det = Math.floor(Math.random()*(moder*10))%moder;
if(det <threshold){
vpanel[i][j]=1;
panel.fillStyle="#000000";
panel.fillRect(i*e,j*e,e,e);
}
else{
vpanel[i][j]=0;
panel.fillStyle="#FFFFFF";
panel.fillRect(i*e,j*e,e,e);
}
}
}
//game on
timer = setInterval("game();",delay);
}
function stop(){
clearInterval(timer);
}
function resume(){
clearInterval(timer);
timer = setInterval("game();",delay);
}
function game(){
var c = document.getElementById("myPanel");
var tmp_panel = JSON.parse(JSON.stringify(vpanel));
var panel = c.getContext("2d");
var movei = [-1,-1,-1,0,0,1,1,1];
var movej = [-1,0,1,-1,1,-1,0,1];
for(var i=0;i<w;i++){
for(var j=0;j<w;j++){
var count = 0;
for(var k=0;k<8;k++){
var ni = i+movei[k],nj = j+movej[k];
if(ni<0 || ni>=w || nj<0 || nj>=w)
continue;
if(vpanel[ni][nj] == 1){
count++;
}
}
if(vpanel[i][j] == 1){
if(count <2){
panel.fillStyle="#FFFFFF";
panel.fillRect(i*e,j*e,e,e);
tmp_panel[i][j] = 0;
}
else if(count > 3){
panel.fillStyle="#FFFFFF";
panel.fillRect(i*e,j*e,e,e);
tmp_panel[i][j] = 0;
}
}
else{
if(count == 3){
panel.fillStyle="#000000";
panel.fillRect(i*e,j*e,e,e);
tmp_panel[i][j] = 1;
}
}
}
}
vpanel = JSON.parse(JSON.stringify(tmp_panel));
}
</script>
</html>
<body>
<div id="wrapper">
<div id = "show_area" style="float:left;">
<canvas id="myPanel" width="750px" height="750px" style="border:2px solid #000000"></canvas>
</div>
<div id = "setting" style="float:left; border:2px solid #000000">
<p>Delay:</p>
<input type="text" id="delay" value="80">
<br>
<p>width:</p>
<input type="text" id="e" value="2">
<br>
<p>threshold:</p>
<input type="text" id="threshold" value="10">
<br>
<p>Moder:</p>
<input type="text" id="moder" value="100">
<p>初始設置機率為 threshold/moder</p>
<button onclick="init();">Init</button>
<button onclick="stop();">Stop</button>
<button onclick="resume();">Resume</button>
</div>
</div>
</body>