Thursday, September 13, 2012

HTML5 Calculator with Tape

Here's an HTML5 calculator I created today. It performs the usual core operations and adapts its size for desktop, tablet, or phone using CSS media queries. At desktop or tablet size, it displays a results tape.This is client-side only HTML5, CSS, and JavaScript, so it doesn't require anything more than minimal web serving (I'm hosting these files out of low-cost Windows Azure blob storage).
View online demo  Download source code


The HTML includes the typical meta tag used in mobile web apps to prevent mobile browsers from zooming the page. The calculator body, display, buttons, and tape are all just styled divs. Keypress and mousedown(tap) events are handled with JavaScript code.

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
<!doctype html>
<html>
<head>
<title>Calculator</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<!--<meta name="viewport" content="width=100%">-->
<link rel="stylesheet" href="styles.css" />
</head>
<body onkeypress="javascript:keypress(event);">
 
<div class="calculator blackgradient largeshadow">
    <div id="result" class="display graygradient smallshadow">0</div>
    <div class="row">
        <div class="button smallshadow" onmousedown="javascript:allclear();"><div class="button-label">AC</div></div>
        <div class="button smallshadow" onmousedown="javascript:clearentry();"><div class="button-label">CE</div></div>
        <div class="button smallshadow" onmousedown="javascript:backspace();"><div class="button-label">BS</div></div>
        <div class="button blue smallshadow last" onmousedown="javascript:divide();"><div class="button-symbol">÷</div></div>
    </div>
    <div class="row">
        <div class="button smallshadow" onmousedown="javascript:seven();"><div class="button-label">7</div></div>
        <div class="button smallshadow" onmousedown="javascript:eight();"><div class="button-label">8</div></div>
        <div class="button smallshadow" onmousedown="javascript:nine();"><div class="button-label">9</div></div>
        <div class="button blue smallshadow raise last" onmousedown="javascript:multiply();"><div class="button-symbol">×</div></div>
    </div>
    <div class="row">
        <div class="button smallshadow" onmousedown="javascript:four();"><div class="button-label">4</div></div>
        <div class="button smallshadow" onmousedown="javascript:five();"><div class="button-label">5</div></div>
        <div class="button smallshadow" onmousedown="javascript:six();"><div class="button-label">6</div></div>
        <div class="button blue smallshadow raise last" onmousedown="javascript:subtract();"><div class="button-symbol">-</div></div>
    </div>
    <div class="row">
        <div class="button smallshadow" onmousedown="javascript:one();"><div class="button-label">1</div></div>
        <div class="button smallshadow" onmousedown="javascript:two();"><div class="button-label">2</div></div>
        <div class="button smallshadow" onmousedown="javascript:three();"><div class="button-label">3</div></div>
        <div class="button blue smallshadow last" onmousedown="javascript:add();"><div class="button-symbol">+</div></div>
    </div>
    <div class="row">
        <div class="button smallshadow" onmousedown="javascript:zero();"><div class="button-label">0</div></div>
        <div class="button smallshadow" onmousedown="javascript:decimal();"><div class="button-label">.</div></div>
        <div class="button blue smallshadow" onmousedown="javascript:percent();"><div class="button-label">%</div></div>
        <div class="button blue smallshadow raise last" onmousedown="javascript:equals();"><div class="button-symbol">=</div></div>
    </div>
</div>
   
<div id="tape" class="tape"></div>
 
<script src="default.js" ></script>
 
</body>
</html>

The CSS styling is below. CSS media queries choose one of three sizes based on screen dimensions. I used the Voces font from Google Web Fonts for the result display.
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
@font-face {
  font-family: 'Voces';
  font-style: normal;
  font-weight: 400;
  src: url(voces.eot);
  src: local('Voces'), local('Voces-Regular'), url(http://themes.googleusercontent.com/static/fonts/voces/v1/UOrhtr4tgkrQ7HpJQ9-OIQ.eot) format('embedded-opentype'), url(http://themes.googleusercontent.com/static/fonts/voces/v1/ePr7eRI28zZaOtQ8CcT7rA.woff) format('woff');
}
 
 
body
{
    font-family: Segoe UI;
    font-size: 1.0em;
}
 
 
/* Containers */
 
.calculator {
    display: inline-block;
    text-align: left;
    border: 1px solid black;
    padding: 16px;
    border-radius: 8px;
}
 
.display {
    display: inline-block;
    width: 364px;
    text-align: right;
    border: 1px solid black inset;
    color: black;
    font-family: Voces, 'Segoe UI Semibold', Calibri, Arial, Helvetica;
    font-size: 2.0em;
    padding: 0px 4px 1px 4px;
    border-radius: 2px;
    margin: 0 0 12px 0;
}
 
.row {
    margin: 8px 0 0 0;
}
 
.button {
    display: inline-block;
    vertical-align: middle;
    width: 80px;
    height: 50px;
    border: 1px solid black inset;
    background: ghostwhite;
    color: black;
    font-family: 'Segoe UI Semibold', Calibri, Arial, Helvetica;
    border-radius: 8px;
    padding: 0px 4px 1px 4px;
    border-radius: 8px;
    margin: 0 4px 0 0;
}
 
.button :active {
    margin-top: 2px;
    margin-left: 2px;
    margin-right: 8px;
    margin-bottom: -2px;
}
 
 
.last {
    margin-right: -4px;
}
 
.button-label {
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 50px;
    width: 80px;
    font-size: 1.4em;
}
 
.button-symbol {
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 50px;
    width: 80px;
    font-size: 1.8em;
    font-weight: bold;
}
 
.blue {
    background-color: lightblue;
}
 
 
/* Shadows */
 
.largeshadow {
    -moz-box-shadow: 3px 3px 4px #000;
    -webkit-box-shadow: 3px 3px 4px #000;
    -ms-box-shadow: 3px 3px 4px #000;
    box-shadow: 3px 3px 4px #000;
}
 
.smallshadow {
    -moz-box-shadow: 3px 3px 3px #666666;
    -webkit-box-shadow: 3px 3px 3px #666666;
    box-shadow: 3px 3px 3px #666666;
}
 
 
/* Gradients */
 
.navy {
background-color: navy;
}
 
.blackgradient {
    background: rgb(149,149,149); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(149,149,149,1) 0%, rgba(13,13,13,1) 46%, rgba(1,1,1,1) 50%, rgba(10,10,10,1) 53%, rgba(78,78,78,1) 76%, rgba(56,56,56,1) 87%, rgba(27,27,27,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(149,149,149,1)), color-stop(46%,rgba(13,13,13,1)), color-stop(50%,rgba(1,1,1,1)), color-stop(53%,rgba(10,10,10,1)), color-stop(76%,rgba(78,78,78,1)), color-stop(87%,rgba(56,56,56,1)), color-stop(100%,rgba(27,27,27,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(149,149,149,1) 0%,rgba(13,13,13,1) 46%,rgba(1,1,1,1) 50%,rgba(10,10,10,1) 53%,rgba(78,78,78,1) 76%,rgba(56,56,56,1) 87%,rgba(27,27,27,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(149,149,149,1) 0%,rgba(13,13,13,1) 46%,rgba(1,1,1,1) 50%,rgba(10,10,10,1) 53%,rgba(78,78,78,1) 76%,rgba(56,56,56,1) 87%,rgba(27,27,27,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(149,149,149,1) 0%,rgba(13,13,13,1) 46%,rgba(1,1,1,1) 50%,rgba(10,10,10,1) 53%,rgba(78,78,78,1) 76%,rgba(56,56,56,1) 87%,rgba(27,27,27,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(149,149,149,1) 0%,rgba(13,13,13,1) 46%,rgba(1,1,1,1) 50%,rgba(10,10,10,1) 53%,rgba(78,78,78,1) 76%,rgba(56,56,56,1) 87%,rgba(27,27,27,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#959595', endColorstr='#1b1b1b',GradientType=0 ); /* IE6-9 */
}
 
.darkbluegradient {
    background: rgb(63,76,107); /* Old browsers */
    background: -moz-linear-gradient(top, rgba(63,76,107,1) 0%, rgba(63,76,107,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(63,76,107,1)), color-stop(100%,rgba(63,76,107,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(63,76,107,1) 0%,rgba(63,76,107,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(63,76,107,1) 0%,rgba(63,76,107,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(63,76,107,1) 0%,rgba(63,76,107,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(63,76,107,1) 0%,rgba(63,76,107,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#3f4c6b', endColorstr='#3f4c6b',GradientType=0 ); /* IE6-9 */
}
 
.bluegradient {
    background: rgb(30,87,153); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(30,87,153,1) 0%, rgba(41,137,216,1) 50%, rgba(32,124,202,1) 51%, rgba(125,185,232,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(30,87,153,1)), color-stop(50%,rgba(41,137,216,1)), color-stop(51%,rgba(32,124,202,1)), color-stop(100%,rgba(125,185,232,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(30,87,153,1) 0%,rgba(41,137,216,1) 50%,rgba(32,124,202,1) 51%,rgba(125,185,232,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#1e5799', endColorstr='#7db9e8',GradientType=0 ); /* IE6-9 */
}
 
.graygradient {
    background: rgb(238,238,238); /* Old browsers */
    background: -moz-linear-gradient(top,  rgba(238,238,238,1) 0%, rgba(238,238,238,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(238,238,238,1)), color-stop(100%,rgba(238,238,238,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top,  rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom,  rgba(238,238,238,1) 0%,rgba(238,238,238,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#eeeeee', endColorstr='#eeeeee',GradientType=0 ); /* IE6-9 */
}
 
.yellowgradient {
    background: rgb(255,255,136); /* Old browsers */
    background: -moz-linear-gradient(top, rgba(255,255,136,1) 0%, rgba(255,255,136,1) 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,136,1)), color-stop(100%,rgba(255,255,136,1))); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(255,255,136,1) 0%,rgba(255,255,136,1) 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, rgba(255,255,136,1) 0%,rgba(255,255,136,1) 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, rgba(255,255,136,1) 0%,rgba(255,255,136,1) 100%); /* IE10+ */
    background: linear-gradient(to bottom, rgba(255,255,136,1) 0%,rgba(255,255,136,1) 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffff88', endColorstr='#ffff88',GradientType=0 ); /* IE6-9 */
}
 
 
.tape {
    display: block;
    width: 396px;
    margin: 16px 0 0 0;
    min-height: 200px;
    background-color: lightyellow;
    color: black;
    text-align: right;
    padding: 8px;
    border: 1px solid black;
}
 
 
/* Windows Phone-size phone */
 
@media screen and (min-width:321px) and (max-width: 480px) {
 
    .display {
        width: 284px;
    }
 
    .button {
        width: 60px;
        height: 38px;
    }
 
    .button-label {
        line-height: 38px;
        width: 60px;
    }
 
    .button-symbol {
        line-height: 38px;
        width: 60px;
    }
 
    .tape {
        display: none;
    }
}
 
 
/* iPhone-size smartphone, portrait */
 
@media screen and (max-width : 320px) {
 
    .display {
        width: 204px;
        font-size: 1.2em;
    }
 
    .button {
        width: 40px;
        height: 25px;
    }
 
    .button-label {
        line-height: 25px;
        width: 40px;
        font-size: 1.0em;
    }
 
    .button-symbol {
        line-height: 25px;
        width: 40px;
        font-size: 1.2em;
    }
 
    .tape {
        display: none;
    }
}
 
 
/* iPhone-size smartphone / landscape */
 
@media screen and (max-height : 320px) and (orientation : landscape) {
 
    .display {
        width: 204px;
        font-size: 1.2em;
    }
 
    .button {
        width: 40px;
        height: 25px;
    }
 
    .button-label {
        line-height: 25px;
        width: 40px;
        font-size: 1.0em;
    }
 
    .button-symbol {
        line-height: 25px;
        width: 40px;
        font-size: 1.2em;
    }
 
    .tape {
        display: none;
    }
}

The JavaScript responds to mousedown (click/tap) events as well as keypress events to store numbers and operators and perform operations.

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
var number = '';
var result = document.getElementById('result');
var tape = document.getElementById('tape');
tape.scrollTop = 10000;
 
 
function keypress(e) {
    switch (e.keyCode) {
        case 8: // backspace
            backspace();
            break;
        case 67: // C
        case 99: // c
            allclear();
            break;
        case 46: // .
            decimal();
            break;
        case 48: // 0
        case 49:
        case 50:
        case 51:
        case 52:
        case 53:
        case 54:
        case 55:
        case 56:
        case 57: // 9
            enter(String.fromCharCode(e.keyCode));
            break;
        case 42: // *
        case 120: // x
        case 88: // X
            multiply();
            break;
        case 47: // /
            divide();
            break;
        case 43: // +
            add();
            break;
        case 45: // -
            subtract();
            break;
        case 13: // ENTER
        case 32: // SPACE
        case 61: // =
            equals();
            break;
    }
}
 
 
function allclear() {
    number = '';
    operator = null;
    result.innerText = "0";
    tape.innerHTML = '';
}
 
function backspace() {
    if (number.length > 0) {
        number = number.substr(0, number.length - 1);
        if (number.length === 0) {
            result.innerText = '0';
        }
        else {
            result.innerText = number;
        }
    }
}
 
function clearentry() {
    number = '';
    result.innerText = '0';
}
 
 
function nine() {
    enter('9');
}
 
function eight() {
    enter('8');
}
function seven() {
    enter('7');
}
 
function six() {
    enter('6');
}
 
function five() {
    enter('5');
}
 
function four() {
    enter('4');
}
 
function three() {
    enter('3');
}
 
function two() {
    enter('2');
}
 
function one() {
    enter('1');
}
 
function zero() {
    enter('0');
}
 
function decimal() {
    if (number && number.indexOf('.')===-1) {
        enter('.');
    }
}
 
 
 
function enter(digit) {
    number = number + digit;
    result.innerText = number;
}
 
 
function multiply() {
    if (!number) {
        number = parseFloat(result.innerText);
    }
    number1 = number;
    operator = "x";
    number = '';
    result.innerText = '0';
    tape.innerHTML += number1.toString() + "
× ";
}
 
function divide() {
    if (!number) {
        number = parseFloat(result.innerText);
    }
    number1 = number;
    operator = '/';
    number = '';
    result.innerText = '0';
    tape.innerHTML += number1.toString() + "
÷ ";
}
 
function add() {
    if (!number) {
        number = parseFloat(result.innerText);
    }
    number1 = number;
    operator = "+";
    number = '';
    result.innerText = '0';
    tape.innerHTML += number1.toString() + "
+ ";
}
 
function subtract() {
    if (!number) {
        number = parseFloat(result.innerText);
    }
 
    // If entered with no number entered, this is a leading minus.
 
    if (number.length == 0) {
        enter('-');
        return;
    }
 
    // Subtraction operator.
 
    number1 = number;
    operator = '-';
    number = '';
    result.innerText = '0';
    tape.innerHTML += number1.toString() + "
- ";
}
 
function percent() {
    if (!operator) {
        return;
    }
    number = (parseFloat(number1) * (parseFloat(number)/100)).toString();
    result.innerText = number;
}
 
 
function equals() {
    if (!operator) {
        return;
    }
    try
    {
        switch (operator) {
            case 'x':
                result.innerText = (parseFloat(number1) * parseFloat(number)).toString();
                tape.innerHTML += number.toString() + "
--------------------
= " + result.innerText + "
&nbsp;
";
                number = '';
                break;
            case '/':
                result.innerText = (parseFloat(number1) / parseFloat(number)).toString();
                tape.innerHTML += number.toString() + "
--------------------
= " + result.innerText + "
&nbsp;
";
                number = '';
                break;
            case '+':
                result.innerText = (parseFloat(number1) + parseFloat(number)).toString();
                tape.innerHTML += number.toString() + "
--------------------
= " + result.innerText + "
&nbsp;
";
                number = '';
                break;
            case '-':
                result.innerText = (parseFloat(number1) - parseFloat(number)).toString();
                tape.innerHTML += number.toString() + "
--------------------
= " + result.innerText + "
&nbsp;
";
                number = '';
                break;
        }
    }
    catch (e) {
        result.innerText = "Error";
    }
    operator = null;
    number = '';
}

I'm writing this and other small HTML5 apps in order to hone my skills in making mobile web apps a good experience that perform well.

1 comment:

Jason Haley said...

Should it work on a win phone 7? Works fine on my desktop, but not on my wp7.