-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
189 lines (141 loc) · 3.92 KB
/
index.html
File metadata and controls
189 lines (141 loc) · 3.92 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
background: #f5f5f5;
color: #4d4d4d;
font-weight: 300;
}
#ball {
width: 50px;
height: 50px;
border-radius: 50%;
background-color: lightgrey;
position: absolute;
}
</style>
<body>
<div id="ball"></div>
<script type="text/javascript">
var div = document.getElementById("ball");
//t = time, b = begin, c = change, d = duration
var linear = function(t, b, c, d) {
return c * t / d + b;
}
var easeIn = function(t, b, c, d) {
return c * (t /= d) * t + b;
}
var strongEaseIn = function(t, b, c, d) {
return c * (t /= d ) * t * t * t * t + b;
}
var strongEaseOut = function(t, b, c, d) {
return c * ((t = t/d - 1) * t * t * t * t + 1) + b;
}
var sineaseIn = function(t, b, c, d) {
return c * (t /= d) * t * t + b;
}
var sineaseOut = function(t, b, c, d) {
return c * ((t = t/d - 1) * t * t + 1) + b;
}
var tween = {
linear: linear,
easeIn: easeIn,
strongEaseIn: strongEaseIn,
strongEaseOut: strongEaseOut,
sineaseIn: sineaseIn,
sineaseOut: sineaseOut
}
var Animate = function(dom) {
this.dom = dom;
this.startTime = 0;
this.startPos = 0;
this.endPos = 0;
this.propertName = null;
this.easing = null;
this.duration = null;
}
Animate.prototype.start = function(propertName, endPos, duration, easing) {
this.startTime = +new Date;
this.startPos = this.dom.getBoundingClientRect()[propertName];
this.propertName = propertName;
this.endPos = endPos;
this.duration = duration;
this.easing = tween[easing];
//similar to while loop
var timeId = setInterval(function() {
if (this.step() === false) {
clearInterval(timeId);
}
}.bind(this), 19);
}
Animate.prototype.step = function() {
var t = +new Date();
if (t > this.startTime + this.duration) {
this.update(this.endPos);
return false;
}
var pos = this.easing(t - this.startTime, this.startPos, this. endPos - this.startPos, this.duration);
this.update(pos);
}
Animate.prototype.update = function(pos) {
this.dom.style[this.propertName] = pos + "px";
}
Animate.prototype.startRAF = function(propertName, endPos, duration, easing) {
this.startTime = +new Date;
this.startPos = this.dom.getBoundingClientRect()[propertName];
this.propertName = propertName;
this.endPos = endPos;
this.duration = duration;
this.easing = tween[easing];
this.paintByStep();
}
Animate.prototype.paintByStep = function() {
var t = + new Date();
if (t > this.startTime + this.duration) {
this.update(this.endPos);
return false;
} else {
var pos = this.easing(t - this.startTime, this.startPos, this. endPos - this.startPos, this.duration);
this.update(pos);
requestAnimationFrame(this.paintByStep.bind(this));
}
}
var animate = new Animate(div);
animate.startRAF("left", 500, 1000, "strongEaseOut");
</script>
<script type="text/javascript">
// var GetRegularGasPrice = function() {}
// GetRegularGasPrice.prototype.calculate = function(gallons) {
// return gallons * 1.9;
// }
// var GetMidgradeGasPrice = function() {}
// GetMidgradeGasPrice.prototype.calculate = function(gallons) {
// return gallons * 2.3;
// }
// var GetPremiumGasPrice = function() {}
// GetPremiumGasPrice.prototype.calculate = function(gallons) {
// return gallons * 2.9;
// }
// var GetDieselGasPrice = function() {}
// GetDieselGasPrice.prototype.calculate = function(gallons) {
// return gallons * 2.1;
// }
// var GasCalculator = function() {
// this.gallons = 0;
// this.strategy = null;
// }
// GasCalculator.prototype.setGallons = function(gallons) {
// this.gallons = gallons;
// }
// GasCalculator.prototype.setStrategy = function(strategy) {
// this.strategy = strategy;
// }
// GasCalculator.prototype.getPrice = function() {
// return this.strategy.calculate(this.gallons);
// }
// var gasCalculator = new GasCalculator();
// gasCalculator.setGallons(10);
// gasCalculator.setStrategy(new GetRegularGasPrice());
// console.log(gasCalculator.getPrice());
</script>