Skip to content

Commit 6f74bc0

Browse files
goldfirebhaskarp-vg
authored andcommitted
Update fade to use elapsed time to fix inconsistencies
Fixes goldfire#885
1 parent 1bd3401 commit 6f74bc0

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
@@ -1165,28 +1165,20 @@
11651165
_startFadeInterval: function(sound, from, to, len, id, isGroup) {
11661166
var self = this;
11671167
var vol = from;
1168-
var dir = from > to ? 'out' : 'in';
1169-
var diff = Math.abs(from - to);
1170-
var steps = diff / 0.01;
1171-
var stepLen = (steps > 0) ? len / steps : len;
1172-
var stepVol = diff / steps;
1173-
1174-
// Since browsers clamp timeouts to 4ms, we need to clamp our steps to that too.
1175-
if (stepLen < 4) {
1176-
steps = Math.ceil(steps * stepLen * 0.25);
1177-
stepLen = 4;
1178-
stepVol = diff / steps;
1179-
}
1168+
var diff = to - from;
1169+
var steps = Math.abs(diff / 0.01);
1170+
var stepLen = Math.max(4, (steps > 0) ? len / steps : len);
1171+
var lastTick = Date.now();
11801172

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

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

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

0 commit comments

Comments
 (0)