-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
569 lines (513 loc) · 17.4 KB
/
index.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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<!-- http://stackoverflow.com/questions/4182494/how-to-draw-number-lines-using-html -->
<title>Numberline</title>
<style type='text/css'>
body {
background-color: lightblue;
font-family: arial;
color: white;
}
.form-container-container {
margin: auto;
width:600px;
}
.form-container {
border: 5px solid #000000;
background: #04baab;
background: -webkit-gradient(linear, left top, left bottom, from(#0009ff), to(#04baab));
background: -webkit-linear-gradient(top, #0009ff, #04baab);
background: -moz-linear-gradient(top, #0009ff, #04baab);
background: -ms-linear-gradient(top, #0009ff, #04baab);
background: -o-linear-gradient(top, #0009ff, #04baab);
background-image: -ms-linear-gradient(top, #0009ff 0%, #04baab 100%);
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
-webkit-box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 0px 0;
-moz-box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 0px 0;
box-shadow: rgba(000,000,000,0.9) 0 1px 2px, inset rgba(255,255,255,0.4) 0 0px 0;
font-family: 'Helvetica Neue',Helvetica,sans-serif;
font-size: 2em;
text-decoration: none;
vertical-align: middle;
padding:30px;
width:600px;
text-align: center;
}
.arrow-up {
margin-bottom: 5px;
margin-left: 5px;
width: 0;
height: 0;
border-left: 45px solid transparent;
border-right: 45px solid transparent;
border-bottom: 45px solid lightgreen;
}
.arrow-down {
margin-top: 5px;
margin-left: 5px;
width: 0;
height: 0;
border-left: 45px solid transparent;
border-right: 45px solid transparent;
border-top: 45px solid lightgreen;
}
input, select
{
font-size:2em;
font-weight: bold;
}
select {
-webkit-appearance: none;
width: 100px !important;
}
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
/* display: none; <- Crashes Chrome on hover */
-webkit-appearance: none;
margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}
.right {
float: right;
}
.form-field {
border: 5px solid #000000;
background: #ffffff;
-webkit-border-radius: 15px;
-moz-border-radius: 15px;
border-radius: 15px;
color: #000000;
-webkit-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 0px 0px;
-moz-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 0px 0px;
box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(000,000,000,0.7) 0 0px 0px;
padding:8px;
width:75px;
}
.form-field:focus {
background: #c4f0ff;
color: #ff00e1;
}
.form-container h2 {
text-shadow: #595959 0 1px 0;
font-size: 3em;
margin: 0 0 10px 0;
font-weight:bold;
text-align:center;
}
.form-title {
margin-bottom:10px;
color: #ffffff;
text-shadow: #595959 0 1px 0;
}
.submit-container {
margin:8px 0;
text-align:right;
}
.submit-button {
border: 5px solid #000000;
background: #4f024f;
background: -webkit-gradient(linear, left top, left bottom, from(#bf5abc), to(#4f024f));
background: -webkit-linear-gradient(top, #bf5abc, #4f024f);
background: -moz-linear-gradient(top, #bf5abc, #4f024f);
background: -ms-linear-gradient(top, #bf5abc, #4f024f);
background: -o-linear-gradient(top, #bf5abc, #4f024f);
background-image: -ms-linear-gradient(top, #bf5abc 0%, #4f024f 100%);
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
-webkit-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
-moz-box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
box-shadow: rgba(255,255,255,0.4) 0 1px 0, inset rgba(255,255,255,0.4) 0 1px 0;
text-shadow: #000000 0 1px 0;
color: #000000;
font-family: helvetica, serif;
padding: 8.5px 18px;
font-size: 1.5em;
text-decoration: none;
vertical-align: middle;
}
.submit-button:hover {
border: 5px solid #ffffff;
text-shadow: #ffffff 0 1px 0;
background: #1a011f;
background: -webkit-gradient(linear, left top, left bottom, from(#4d0448), to(#1a011f));
background: -webkit-linear-gradient(top, #4d0448, #1a011f);
background: -moz-linear-gradient(top, #4d0448, #1a011f);
background: -ms-linear-gradient(top, #4d0448, #1a011f);
background: -o-linear-gradient(top, #4d0448, #1a011f);
background-image: -ms-linear-gradient(top, #4d0448 0%, #1a011f 100%);
color: #fff;
}
.submit-button:active {
text-shadow: #ffffff 0 1px 0;
border: 5px solid #ffffff;
background: #190121;
background: -webkit-gradient(linear, left top, left bottom, from(#41024d), to(#1a011f));
background: -webkit-linear-gradient(top, #41024d, #190121);
background: -moz-linear-gradient(top, #41024d, #190121);
background: -ms-linear-gradient(top, #41024d, #190121);
background: -o-linear-gradient(top, #41024d, #190121);
background-image: -ms-linear-gradient(top, #41024d 0%, #190121 100%);
color: #fff;
}
.inline {
display: inline-block;
}
#answer {
font-size: 2.5em;
}
</style>
<script type='text/javascript'>
//Define getPosition outside of function to allow
//removing it from eventlistener.
var getPosition;
function renderCanvas() {
var canvas = $('canvas')[0];
canvas.removeEventListener("mousedown", getPosition, false);
var ctx = canvas.getContext('2d');
//Pull inputs from form.
var starti = -10;//Number($("#rangestart").val());
var endi = 10;//Number($("#rangeend").val());
var start = Number($('#start').val());
var modifier = Number($("#modifier").val());
var op = $('#operation').val();
var w = canvas.width = window.innerWidth - 20;
var h = canvas.height = 300;
//Prevent Divide by zero error
if (op == "/" && modifier == 0) {
ctx.fillStyle = 'purple';
ctx.font = "50px Arial";
ctx.fillText("Divide by Zero Error!",(canvas.width/2)-200,(canvas.height/2)-100);
drawSmiley();
ctx.fillStyle = 'purple';
ctx.fillText("THPPPT!!!",(canvas.width/2)-110,(canvas.height/2)+125);
return;
}
//Extend Range if Calculation goes out of bounds.
if (start < starti) {
starti = start;
} else if (start > endi) {
endi = start;
}
var output;
switch(op) {
case "*":
output = (start * modifier);
break;
case "+":
output = (start + modifier);
break;
case "-":
output = (start - modifier);
break;
case "/":
if (Math.floor(start/modifier) < 0 && start > 0) {
output = start*-1;
} else {
output = start;
}
break;
}
if (output > endi) {
endi = output;
}else if (output < starti) {
starti = output;
}
if (op == "/")
$('#answer').html(" = " + (Math.round((start/modifier) * 10)/10));
else
$('#answer').html(" = " + output);
var totRange = endi - starti;
//Offset h to push numberline to bottom of canvas.
h = h + 150;
var x0 = w/2;
var interval = Math.round((w/totRange)/1.5);//40;
//Graphic: steps.
var step = modifier;
var steps = 1;
//If Multiplication or Division, generate multiple hops.
if (op == '*') {
step = start;
steps = modifier;
}
else if (op == '/') {
steps = Math.floor(start/step);
if (steps < 0) {
steps = Math.ceil(start/step)
}
if ((steps < 0 && start > 0) || (steps > 0 && start < 0)) {
start = start*-1;
steps = steps*-1;
}
}
function drawOperation(x, step, steps) {
drawPoint(x);
var tempSteps = steps;
if (steps < 0) {
tempSteps = steps*-1;
}
var tempInterval;
if ((op == '*' && steps > 0) || op == '+' || (op == '/' && steps < 0)) {
//Multiply or Add hop forwards
tempInterval = interval;
} else {
//Divide and Subtract hop backwards
tempInterval = interval*-1;
}
for(var i = 0; i < tempSteps; i++){
var label = '';
if (op == '*' || op == '/') {
//Label Hop count.
label = i + 1;
//Add remainder for division
if (op == '/') {
//TODO: Not sure if this is correct, but it works for now.
//Problem is negative number modulo require ceil()
//while normally uses floor().
//Seems unneccessarily complex and inelegant.
if (((steps > 0 && i == (steps-1)) || (steps < 0 && i == (steps+1)*-1)) && Math.floor(start % step) > 0) {
label = label + ' (+' + Math.floor(start % step) + ')';
} else if (((steps > 0 && i == (steps-1)) || (steps < 0 && i == (steps+1)*-1)) && Math.ceil(start % step) < 0) {
label = label + ' (' + Math.ceil(start % step) + ')';
}
}
}
drawCurve(x+i*step*tempInterval,x+(i+1)*step*tempInterval,label);
}
//TODO: This is a KLUGE: can't figure out how to place
//the point in the right place for division where
//base number is negative, and modifier positive.
//(but works where base is positive/modifier negative)
//It's probably something simple, but my brain is worn out.
//Also breaks on multiplye: Base positive * modifier negative
if ((op == '/' && tempInterval > 0) || (op == '*' && tempInterval < 0)) {
drawPoint(x+(steps*-1)*step*tempInterval);
} else {
drawPoint(x+steps*step*tempInterval);
}
}
function drawPoint(x1) {
ctx.font="30px Arial";
ctx.beginPath();
ctx.arc(x1,h/2,10,0,2*Math.PI);
ctx.fillStyle="red";
ctx.fill();
ctx.stroke();
ctx.fillStyle="lightgreen";
}
getPosition = function(e) {
var x = e.clientX;
var xe = x - x % interval + 0;
if(x0===0){
drawPoint(xe);
x0=xe;
}
else{
drawPoint(xe);
drawCurve(x0,xe,'');
x0=xe;
}
}
function drawCurve(x1,x2,label) {
ctx.moveTo(x1,h/2);
var bezierY = h/2-Math.abs(x2-x1);
var textY = h/2-Math.abs(x2-x1)*0.5-10;
//Constrain Arc to inside canvas height.
if (((x2-x1) > 0 && (x2-x1) > (h*0.75)) || ((x2-x1) < 0 && (x2-x1) < ((h*0.75)*-1))) {
bezierY = -h*0.30;
//TODO: I have no idea why this works.
textY = bezierY*-0.25;
}
ctx.quadraticCurveTo(x1+(x2-x1)/2,bezierY,x2,h/2);
var text;
if (label != '') {
text = label;
} else {
var s = Math.round((x2 - x1)/interval);
if(s > 0){
text = "+" + s.toString();
}
else {
text = s.toString();
}
}
ctx.fillText(text,x1+(x2-x1)/2,textY);
ctx.stroke();
}
with(ctx) {
ctx.font="25px Arial";
fillStyle = 'indigo';
fillRect(0, 0, w, h);
fill();
beginPath();
lineWidth = 4;
strokeStyle = 'fuchsia';
moveTo(w/7, h/2);
lineTo(6*w/7, h/2);
stroke();
moveTo(w/7, h/2);
lineTo(w/7+10, h/2+20);
stroke();
moveTo(w/7, h/2);
lineTo(w/7+10, h/2-20);
stroke();
moveTo(6*w/7, h/2);
lineTo(6*w/7-10, h/2+20);
stroke();
moveTo(6*w/7, h/2);
lineTo(6*w/7-10, h/2-20);
stroke();
var k = Math.round((totRange/2)-totRange);
for(var i = starti;i <= endi; i++) {
beginPath();
strokeStyle = '#0f0';
lineWidth = 4;
fillStyle = '#0f0';
if(i == start) {
//Highlight the Starting Point
lineWidth = 7;
strokeStyle = '#ff0';
fillStyle = '#ff0';
if (op != '*') {
//Set x0 to starting point
x0 = (h/2, w/2 + k * interval);
}
}
if (i == 0 && op == '*') {
//Set x0 to 0 for multiplication
x0 = (h/2, w/2 + k * interval);
}
//Measuring lines
moveTo(w/2 + k * interval, h/2 - 15);
lineTo(w/2 + k * interval, h/2 + 15);
//Line numbers.
fillText(i, (w/2 + k * interval ) - 10, h/2 + 50);
fill();
stroke();
k++
}
}
function drawSmiley() {
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
var radius = 70;
var eyeRadius = 10;
var eyeXOffset = 25;
var eyeYOffset = 20;
// draw the yellow circle
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'yellow';
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = 'black';
ctx.stroke();
// draw the eyes
ctx.beginPath();
var eyeX = centerX - eyeXOffset;
var eyeY = centerY - eyeXOffset;
ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
var eyeX = centerX + eyeXOffset;
ctx.arc(eyeX, eyeY, eyeRadius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'black';
ctx.fill();
// draw the mouth
ctx.beginPath();
ctx.arc(centerX, centerY, 50, 0, Math.PI, false);
ctx.stroke();
}
canvas.addEventListener("mousedown", getPosition, false);
drawOperation(x0,step, steps);
}
$(window).load(function (){renderCanvas();});
$(window).load(function(){
// This button will increment the value
$('.increment').click(function(e){
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('#'+fieldName).val());
// If is not undefined
if (!isNaN(currentVal)) {
// Increment
$('#'+fieldName).val(currentVal + 1);
} else {
// Otherwise put a 0 there
$('#'+fieldName).val(0);
}
});
// This button will decrement the value till 0
$(".decrement").click(function(e) {
// Stop acting like a button
e.preventDefault();
// Get the field name
fieldName = $(this).attr('field');
// Get its current value
var currentVal = parseInt($('#'+fieldName).val());
// If it isn't undefined
if (!isNaN(currentVal)) {
// Decrement one
$('#'+fieldName).val(currentVal - 1);
} else {
// Otherwise put a 0 there
$('#'+fieldName).val(0);
}
});
});
</script>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="form-container-container">
<form class="form-container">
<div class="inline">
<div class="arrow-up increment" field="start"></div>
<input class="form-field" type="number" id="start" value="2" />
<div class="arrow-down decrement" field="start"></div>
</div>
<div class="inline">
<select class="form-field" id="operation" />
<option value="+"> + </option>
<option value="-"> - </option>
<option value="*"> * </option>
<option value="/"> / </option>
</select>
</div>
<div class="inline">
<div class="arrow-up increment" field="modifier"></div>
<input class="form-field" type="number" id="modifier" value="2" />
<div class="arrow-down decrement" field="modifier"></div>
</div>
<div class="inline" id="answer">
</div>
<div>
<!--div class="right">
<div class="form-title">Range End</div>
<input type='button' value='-' class='decrement' field='rangeend' />
<input class="form-field" type="number" id="rangeend" value="20" />
<input type='button' value='+' class='increment' field='rangeend' />
<br />
</div>
<div class="form-title">Range Start</div>
<input type='button' value='-' class='decrement' field='rangestart' />
<input class="form-field" type="number" id="rangestart" value="0" />
<input type='button' value='+' class='increment' field='rangestart' />
<br />
</div-->
<div class="submit-container right">
<input class="submit-button" type="button" value="Calculate" onclick="renderCanvas();" />
</div>
</div>
</form>
</div>
<div style="text-align: center;clear: both; padding-top: 10px;">
<h5 style="color: #000000;">Credit to <a href="http://stackoverflow.com/questions/4182494/how-to-draw-number-lines-using-html">user372551</a> on StackOverflow. Part of the <a href="http://ideonexus.github.io/Explorable-Explanations/">Explorable Explanations</a> collection of educational javascript apps.</h5>
</div>
</body>
</html>