-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html~
232 lines (196 loc) · 11.2 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
<html>
<head>
<link rel="stylesheet" type="text/css" href="reset.css" />
</head>
<body>
<script>
var Calculator = function (p) {
var core = new function() {
var currentDisplay = '';
var mem = null;
var currentOp;
var con = true;
var calculate = function (op) {
switch (op) {
case '+':
return parseFloat(mem) + parseFloat(currentDisplay);
break;
case '-':
return parseFloat(mem) - parseFloat(currentDisplay);
break;
case 'x':
return parseFloat(mem) * parseFloat(currentDisplay);
break;
case '%':
return parseFloat(mem) / parseFloat(currentDisplay);
break;
case 's':
return Math.pow(parseFloat(currentDisplay), 2);
break;
case 'r':
return Math.sqrt(parseFloat(currentDisplay));
break;
}
}
this.buttonPush = function(symbol) {
console.log(symbol);
if (parseInt(symbol) || symbol == 0 || symbol == '.') {
console.log('number');
if (con) {
currentDisplay += symbol;
} else {
mem = currentDisplay;
currentDisplay = symbol;
con = true;
}
} else {
if (symbol == 'C') {
if (currentDisplay == '') {
if (currentOp) {
currentOp = null;
} else {
mem = null;
}
} else {
currentDisplay = '';
}
} else if (symbol == '=') {
if (currentOp && mem) {
currentDisplay = calculate(currentOp);
currentOp = null;
}
} else {
if (symbol == 's' || symbol == 'r' && currentDisplay != '') {
currentDisplay = calculate(symbol);
mem = '';
} else if (mem && currentOp) {
mem = calculate(currentOp);
currentDisplay = mem;
} else {
currentOp = symbol;
}
con = null;
}
}
display.innerText = currentDisplay;
}
};
var Button = function (p) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var pixel = ctx.createImageData(1, 1);
pixel.data[4] = 0;
var img, hoverImg, clickImg, symbol, container;
var text;
var cornerCutter = function () {
ctx.putImageData(pixel, 0, 0);
ctx.putImageData(pixel, 0, canvas.height - 1);
ctx.putImageData(pixel, canvas.width - 1, 0);
ctx.putImageData(pixel, canvas.width - 1, canvas.height - 1);
}
var makeImg = function (p) {
var img = new Image();
img.style.position = 'absolute';
img.style.top = '0px';
img.style.left = '0px';
img.style.opacity = '0';
ctx.save();
ctx.fillStyle = p.inner;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = p.outter;
ctx.strokeRect(0, 0, canvas.width, canvas.height);
cornerCutter();
img.src = canvas.toDataURL("image/png");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.restore();
return img;
};
var hoverIn = function () { hoverImg.style.opacity = '1'; };
var hoverOut = function () { hoverImg.style.opacity = '0'; };
var mouseDown = function () {
clickImg.style.opacity = '1';
core.buttonPush(p.symbol);
};
var mouseUp = function () { clickImg.style.opacity = '0'; };
canvas.width = p.width;
canvas.height = p.height;
img = makeImg( {inner: '#DDDDFF', outter: '#BBBBFF'} );
hoverImg = makeImg( {inner: '#EEEE00', outter: '#BBBBFF'} );
clickImg = makeImg( {inner: '#AAAA00', outter: '#0000DD'} );
symbol = document.createElement('span');
symbol.innerHTML = p.display ? p.display : p.symbol;
symbol.style.zIndex = 2;
symbol.style.position = 'relative';
symbol.style.lineHeight = p.height + 'px';
container = document.createElement('li');
container.appendChild(img);
container.appendChild(hoverImg);
container.appendChild(clickImg);
container.appendChild(symbol);
container.style.position = 'relative';
container.style.width = p.width;
container.style.height = p.height;
container.style.textAlign = 'center';
container.style.display = 'inline-block';
container.style.margin = p.margin;
img.style.opacity = '1';
container.onmouseover = hoverIn;
container.onmouseout = hoverOut;
container.onmousedown = mouseDown;
container.onmouseup = mouseUp;
return container;
};
var calculator = document.createElement('div');
calculator.style.position = 'absolute';
calculator.style.top = '0px';
calculator.style.left = '0px';
calculator.style.width = window.innerWidth * .98; //'200px';
calculator.style.height = window.innerHeight * .98;//'300px';
calculator.style.border = '1px solid #000000';
var display = document.createElement('div');
display.style.top = '0px';
display.style.width = '100%';
display.style.height = parseInt(calculator.style.height) / 5;
display.style.textAlign = 'right';
display.style.fontSize = .95 * parseInt(display.style.height);
calculator.appendChild(display);
var keypad = document.createElement('ul');
keypad.style.listStyle = 'none';
keypad.style.position = 'relative';
keypad.style.left = '0px';
keypad.style.width = calculator.style.width;
keypad.style.height = parseInt(calculator.style.height) - parseInt(display.style.height);
calculator.appendChild(keypad);
var buttons = [];
var buttonMargin = 1;
var buttonWidth = (parseInt(keypad.style.width) / 4) - (buttonMargin * 2);
var buttonHeight = (parseInt(keypad.style.height) / 5) - (buttonMargin * 2);
buttons.push(new Button({symbol: '1', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '2', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '3', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '+', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '4', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '5', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '6', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '-', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '7', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '8', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '9', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '%', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '=', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '0', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: '.', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: 'x', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({display: 'x^2', symbol: 's', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({display: 'Sqrt', symbol: 'r', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: 'C', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
buttons.push(new Button({symbol: 'x', width: buttonWidth, height: buttonHeight, margin: buttonMargin}));
for (var i = 0; i < buttons.length; i++) {
keypad.appendChild(buttons[i]);
}
return calculator;
}
document.getElementsByTagName("body")[0].appendChild(new Calculator());
</script>
</body>
</html>