-
Notifications
You must be signed in to change notification settings - Fork 0
/
sand.html
159 lines (126 loc) · 3.54 KB
/
sand.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>test</title>
<link rel="stylesheet" type="text/css" href="reset.css">
<style type="text/css">
*, *:before, *:after {-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; font-family:Arial;}
html {background-color:#e0e0e0; width:100%; height:100%;}
body {width:800px; min-height:100%; background-color:#f0f0f0; box-shadow: 0 0 10px #666; margin:0 auto;}
h1 {font-size:24px; padding:10px 0; text-align:center;}
h2 {font-size:18px; padding:5px 0;}
#fps {width:800px; height:500px; background-color:#000;}
#controls {float:left; width:250px; height:300px; padding:10px;}
#code {float:right; width:550px; height:300px; padding:10px;}
#txtCode {width:100%; height:250px; font-size:11px; font-family:monospace;}
</style>
</head>
<body>
<h1>
Shifter's Tank Thing</h1>
<canvas id="fps" width="800" height="500">
</canvas>
<div id="controls">
<p>FPS: <label id="lblFPS"></label></p>
<p>
<button id="btnPlay" onclick="Play();">
Play</button></p>
</div>
<div id="code">
<h2>
Code</h2>
<textarea id="txtCode"></textarea>
</div>
<div style="clear: both;">
</div>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function () {
});
function Play() {
running = !running;
$('#btnPlay').html(running ? 'Pause' : 'Play');
if (running)
requestAnimationFrame(update);
}
var canvas = document.getElementsByTagName("canvas")[0];
var moving = document.createElement("canvas");
var fixed = document.createElement("canvas");
var particles = [];
var collision = [];
var running = false;
var W = canvas.width;
var H = canvas.height;
var Y = 0;
var T = 0;
var FPS = 0;
var FPSTimer = 0;
var N = 0;
initialize();
function initialize() {
moving.width = fixed.width = W;
moving.height = fixed.height = H;
for (var y = 0; y < H; y++) {
for (var x = 0; x < W; x++) {
collision.push(false);
if (rand(0, 1000) < 500)
particles.push({ x: x, y: y });
}
}
var context = moving.getContext("2d");
var imageData = context.createImageData(W, H);
var data = imageData.data;
for (var i = 0; i < particles.length; i++) {
var x = particles[i].x;
var y = particles[i].y;
var index = 4 * y * W + 4 * x;
data[index + 0] = 255;
data[index + 1] = 255;
data[index + 2] = 255;
data[index + 3] = 255;
}
context.putImageData(imageData, 0, 0);
T = time();
N = particles.length;
requestAnimationFrame(update);
}
function update() {
context = canvas.getContext("2d");
context.fillStyle = "#000";
context.fillRect(0, 0, W, H);
context.drawImage(moving, 0, Y, W, H);
context.drawImage(fixed, 0, 0, W, H);
var context = fixed.getContext("2d");
context.fillStyle = "#FFF";
for (var i = 0; i < N; i++) {
var x = particles[i].x;
var y = particles[i].y + Y;
if (y + 1 >= H || collision[(y + 1) * W + x]) {
collision[y * W + x] = true;
context.fillRect(x, y, 1, 1);
particles[i--] = particles[N-- - 1];
}
}
Y++;
var t = time();
FPSTimer += t - T;
T = t;
if (FPSTimer >= 1000) {
document.getElementById("lblFPS").innerHTML = FPS;
FPSTimer = 0;
FPS = 0;
}
FPS++;
if (running)
requestAnimationFrame(update);
}
function rand(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function time() {
return new Date().getTime();
}
</script>
</body>
</html>