-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomRanges.html
More file actions
210 lines (183 loc) · 5.59 KB
/
RandomRanges.html
File metadata and controls
210 lines (183 loc) · 5.59 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Random Ranges Visualizer</title>
<style type='text/css'>
canvas { border: 1px solid black; }
</style>
</head>
<body>
<p>Change the numbers and see what happens!</p>
<canvas id='canvas' width='800' height='500'></canvas>
<script type='text/javascript'>
var ctx;
var mouse = { x: 0, y: 0 };
var values = [ "ERR", "50", "300" ];
var samples = [ 0, 0, 0, 0, 0, 0, 0 ];
var need_samples = true;
var activeBox = 0;
var lo = 50, range = 300, hi = range+lo-1;
var codeFont = "24pt Consolas, Courier New, monospace";
var regularFont = "20pt Calibri, Arial, sans-serif";
var box1 = { x: 45, y: 50, w: 100, h: 38 };
var box2 = { x: 552, y: 50, w: 120, h: 38 };
function update() {
lo = s2i( values[1] );
range = s2i( values[2] );
hi = range + lo - 1;
if ( need_samples ) {
for ( var i = 0; i < samples.length; i++ )
samples[i] = lo + Math.floor( Math.random() * range );
need_samples = false;
}
}
function centerTextX( s, box ) {
// returns the x offset that horizontally centers the String within the given rectangle
var width = ctx.measureText(s).width;
return box.x + (box.w - width)/2;
}
function s2i( s ) {
var n = 0;
if ( s.length > 0 ) {
var test = parseInt(s, 10);
if ( ! isNaN(test) )
n = test;
}
return n;
}
function contains( click, box ) {
if ( click.x > box.x + box.w )
return false;
if ( click.y > box.y + box.h )
return false;
if ( box.x > click.x )
return false;
if ( box.y > click.y )
return false;
return true;
}
function draw(ctx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
// draw text boxes
ctx.fillStyle = "#AAAAAA";
ctx.fillRect(box1.x, box1.y, box1.w, box1.h);
ctx.fillRect(box2.x, box2.y, box2.w, box2.h);
// draw code snippet
ctx.fillStyle = "black";
ctx.font = codeFont;
ctx.textBaseline = "top";
ctx.fillText(" + (int)(Math.random()* )", 134, 50);
var b1 = values[1];
var b2 = values[2];
// outline active box and append 'cursor'
if ( activeBox === 1 ) {
ctx.strokeRect(box1.x, box1.y, box1.w, box1.h);
if ( b1.length === 0 )
b1 = "|";
}
else if ( activeBox === 2 ) {
ctx.strokeRect(box2.x, box2.y, box2.w, box2.h);
if ( b2.length === 0 )
b2 = "|";
}
// draw current values into text boxes
var x1 = centerTextX(b1, box1);
ctx.fillText( b1, x1, box1.y);
var x2 = centerTextX(b2, box2);
ctx.fillText( b2, x2, box2.y);
// draw details about random numbers
ctx.font = regularFont;
ctx.fillText("Gives one value randomly chosen from " + lo + " to " + hi + ".", 100, 200 );
ctx.fillText("There are " + Math.abs(range) + " possible values.", 100, 235 );
ctx.fillText("The smallest possible value is " + lo + ".", 100, 270 );
ctx.fillText("The largest possible value is " + hi + ".", 100, 305 );
ctx.fillText("Some samples: ", 100, 370 );
for ( var i = 0; i < samples.length; i++ )
ctx.fillText(samples[i], 70+(100*i), 415);
// draw debugging stuff
ctx.fillStyle = "black";
}
function preventDefault(e)
{
var evt = e ? e : window.event;
if ( evt.preventDefault )
evt.preventDefault();
else
evt.returnValue = false;
return false;
}
var KEY_UP = 38, KEY_DOWN = 40, KEY_LEFT = 37, KEY_RIGHT = 39;
var KEY_ENTER = 13, KEY_ESC = 27, KEY_BACKSPACE = 8, KEY_TAB = 9;
var KEY_MINUS = 189, KEYPAD_MINUS = 109;
function handleKeyDown(e) {
var evt = e ? e : window.event;
var ch = evt.keyCode;
if ( ch === KEY_BACKSPACE ) {
if ( values[activeBox].length > 0 )
values[activeBox] = values[activeBox].substring(0, values[activeBox].length-1);
need_samples = true;
return preventDefault(evt);
}
else if ( ch === KEY_MINUS || ch === KEYPAD_MINUS ) {
values[activeBox] += "-";
need_samples = true;
return preventDefault(evt);
}
else if ( ch === KEY_ENTER ) {
if ( values[activeBox].length === 0 )
values[activeBox] = "0";
activeBox = 0;
need_samples = true;
return preventDefault(evt);
}
else if ( ch === KEY_TAB ) {
if ( values[activeBox].length === 0 )
values[activeBox] = "0";
activeBox = (activeBox+1)%3;
need_samples = true;
return preventDefault(evt);
}
else if ( 48 <= ch && ch <= 57 ) {
values[activeBox] += String.fromCharCode(ch);
need_samples = true;
return preventDefault(evt);
}
else if ( 96 <= ch && ch <= 105 ) {
values[activeBox] += String.fromCharCode(ch - 48);
need_samples = true;
return preventDefault(evt);
}
}
function mouseClicked(e) {
var evt = e || window.event;
mouse.x = evt.pageX - ctx.canvas.offsetLeft;
mouse.y = evt.pageY - ctx.canvas.offsetTop;
if ( contains(mouse, box1) ) {
activeBox = 1;
values[1] = "";
need_samples = true;
}
else if ( contains(mouse, box2) ) {
activeBox = 2;
values[2] = "";
need_samples = true;
}
else {
if ( values[activeBox].length === 0 )
values[activeBox] = "0";
activeBox = 0;
need_samples = true;
}
}
function main() {
ctx = document.getElementById('canvas').getContext("2d");
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('click', mouseClicked);
setInterval(function() { update(); draw(ctx); }, 1000/60);
}
main();
</script>
<p><a href="./">TOC</a></p>
</body>
</html>