wip animate dezimalstellen
This commit is contained in:
1 parent
14c571a77a
commit
7876c7ab3f
3 files changed
+25
-39
No files matched your search
@@ -1,4 +1,5 @@
|
||||
function animate(element, end, duration = 500) {
|
||||
function animate(element, end, dezimal = 0) {
|
||||
var duration = 500;
|
||||
var element = document.getElementById(element);
|
||||
|
||||
const startTime = performance.now();
|
||||
@@ -20,7 +21,18 @@ function animate(element, end, duration = 500) {
|
||||
// Calculate current value and round to integer
|
||||
const currentCount = Math.round(end * easedProgress * 100) / 100;
|
||||
|
||||
element.innerHTML = currentCount;
|
||||
switch (dezimal) {
|
||||
case 1:
|
||||
element.innerHTML = currentCount.toFixed(1);
|
||||
break;
|
||||
case 2:
|
||||
element.innerHTML = currentCount.toFixed(2);
|
||||
break;
|
||||
|
||||
default:
|
||||
element.innerHTML = currentCount;
|
||||
break;
|
||||
}
|
||||
|
||||
if (progress < 1) {
|
||||
requestAnimationFrame(updateCount);
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
function animateValue(id, start, end, duration) {
|
||||
// assumes integer values for start and end
|
||||
|
||||
var obj = document.getElementById(id);
|
||||
var range = end - start;
|
||||
// no timer shorter than 50ms (not really visible any way)
|
||||
var minTimer = 50;
|
||||
// calc step time to show all interediate values
|
||||
var stepTime = Math.abs(Math.floor(duration / range));
|
||||
|
||||
// never go below minTimer
|
||||
stepTime = Math.max(stepTime, minTimer);
|
||||
|
||||
// get current time and calculate desired end time
|
||||
var startTime = new Date().getTime();
|
||||
var endTime = startTime + duration;
|
||||
var timer;
|
||||
|
||||
function run() {
|
||||
var now = new Date().getTime();
|
||||
var remaining = Math.max((endTime - now) / duration, 0);
|
||||
var value = Math.round(end - remaining * range);
|
||||
obj.innerHTML = value;
|
||||
if (value == end) {
|
||||
clearInterval(timer);
|
||||
}
|
||||
}
|
||||
|
||||
timer = setInterval(run, stepTime);
|
||||
run();
|
||||
}
|
||||
Reference in new issue
Block a user