Skip to content

Commit 11fe8b0

Browse files
committed
Update fade to use elapsed time to fix inconsistencies
Fixes goldfire#885
1 parent 97391fa commit 11fe8b0

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

src/howler.core.js

+8-16
Original file line numberDiff line numberDiff line change
@@ -1166,28 +1166,20 @@
11661166
_startFadeInterval: function(sound, from, to, len, id, isGroup) {
11671167
var self = this;
11681168
var vol = from;
1169-
var dir = from > to ? 'out' : 'in';
1170-
var diff = Math.abs(from - to);
1171-
var steps = diff / 0.01;
1172-
var stepLen = (steps > 0) ? len / steps : len;
1173-
var stepVol = diff / steps;
1174-
1175-
// Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too.
1176-
if (stepLen < 4) {
1177-
steps = Math.ceil(steps * stepLen * 0.25);
1178-
stepLen = 4;
1179-
stepVol = diff / steps;
1180-
}
1169+
var diff = to - from;
1170+
var steps = Math.abs(diff / 0.01);
1171+
var stepLen = Math.max(4, (steps > 0) ? len / steps : len);
1172+
var lastTick = Date.now();
11811173

11821174
// Store the value being faded to.
11831175
sound._fadeTo = to;
11841176

11851177
// Update the volume value on each interval tick.
11861178
sound._interval = setInterval(function() {
1187-
// Update the volume amount, but only if the volume should change.
1188-
if (steps > 0) {
1189-
vol += (dir === 'in' ? stepVol : -stepVol);
1190-
}
1179+
// Update the volume based on the time since the last tick.
1180+
var tick = (Date.now() - lastTick) / len;
1181+
lastTick = Date.now();
1182+
vol += diff * tick;
11911183

11921184
// Make sure the volume is in the right bounds.
11931185
vol = Math.max(0, vol);

0 commit comments

Comments
 (0)