Skip to content

Commit

Permalink
Effects: Use requestAnimationFrame timestamp if available
Browse files Browse the repository at this point in the history
In some environments that support the requestAnimationFrame timestamp callback
parameter using it results in smoother animations.

Note: the rAF timestamp is using the same API as performance.now() under the
hood so they're compatible with each other. However, some browsers support rAF
(with a timestamp parameter) but not performance.now() so using them both
would introduce an error. This commit stops using rAF in browsers that don't
support performance.now(). From all the browsers jQuery supports this only
affects iOS <9 (currently less than 5% of all iOS users) which will now not
use rAF.

Fixes jquerygh-3143
Closes jquerygh-3151
  • Loading branch information
mgol committed Apr 29, 2019
1 parent cf84696 commit f089df5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 25 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module.exports = function( grunt ) {
"deprecated",
"dimensions",
"effects",
"effects-nostubs",
"event",
"manipulation",
"offset",
Expand Down
23 changes: 15 additions & 8 deletions src/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ define( [
"./manipulation",
"./css",
"./effects/Tween"
], function( jQuery, document, rcssNum, rnothtmlwhite, cssExpand,
isHiddenWithinTree, swap, adjustCSS, cssCamelCase, dataPriv, showHide ) {
], function( jQuery, document, rcssNum, rnothtmlwhite, cssExpand, isHiddenWithinTree,
swap, adjustCSS, cssCamelCase, dataPriv, showHide ) {

"use strict";

Expand All @@ -28,24 +28,31 @@ var
rfxtypes = /^(?:toggle|show|hide)$/,
rrun = /queueHooks$/;

function schedule() {
function schedule( timestamp ) {
if ( inProgress ) {
if ( document.hidden === false && window.requestAnimationFrame ) {
if ( document.hidden === false ) {
window.requestAnimationFrame( schedule );
} else {
window.setTimeout( schedule, jQuery.fx.interval );
}

jQuery.fx.tick();
jQuery.fx.tick( timestamp );
}
}

// We need to be using Date.now() or performance.now() consistently as they return different
// values: performance.now() counter starts on page load.
// Support: IE <10, Safari <8.0, iOS <9, Android <4.4, Node with jsdom 9.4
function getTimestamp() {
return window.performance.now();
}

// Animations created synchronously will run synchronously
function createFxNow() {
window.setTimeout( function() {
fxNow = undefined;
} );
return ( fxNow = Date.now() );
return ( fxNow = getTimestamp() );
}

// Generate parameters to create a standard animation
Expand Down Expand Up @@ -645,12 +652,12 @@ jQuery.each( {
} );

jQuery.timers = [];
jQuery.fx.tick = function() {
jQuery.fx.tick = function( timestamp ) {
var timer,
i = 0,
timers = jQuery.timers;

fxNow = Date.now();
fxNow = timestamp || getTimestamp();

for ( ; i < timers.length; i++ ) {
timer = timers[ i ];
Expand Down
38 changes: 38 additions & 0 deletions test/data/effects/matchingTimestamps.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr" id="html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Effects: matching timestamps</title>
</head>
<body>
<script src="../../jquery.js"></script>
<script src="../iframeTest.js"></script>
<div id="test-div" style="height: 1000px;"></div>
<script>
//<![CDATA[
setTimeout( function () {

// Handle a timeout.
startIframeTest( false, "The test timed out" );
}, 5000 );

var maxNow = 0;

jQuery( "#test-div" ).animate( {
height: '2000px',
}, {
duration: 300,
step: function( now ) {
if ( maxNow - now > 100 ) {
startIframeTest( false, "Animation is stepping back from " + maxNow + " to " + now );
}
maxNow = Math.max( now, maxNow );
},
complete: function() {
startIframeTest( true );
}
} );
//]]>
</script>
</body>
</html>
8 changes: 3 additions & 5 deletions test/unit/animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ if ( !jQuery.fx ) {
return;
}

var oldRaf = window.requestAnimationFrame,
defaultPrefilter = jQuery.Animation.prefilters[ 0 ],
defaultTweener = jQuery.Animation.tweeners[ "*" ][ 0 ],
startTime = 505877050;
var defaultPrefilter = jQuery.Animation.prefilters[ 0 ];
var defaultTweener = jQuery.Animation.tweeners[ "*" ][ 0 ];
var startTime = 505877050;

// This module tests jQuery.Animation and the corresponding 1.8+ effects APIs
QUnit.module( "animation", {
Expand All @@ -26,7 +25,6 @@ QUnit.module( "animation", {
this.sandbox.restore();
jQuery.fx.stop();
jQuery.fx.interval = this._oldInterval;
window.requestAnimationFrame = oldRaf;
return moduleTeardown.apply( this, arguments );
}
} );
Expand Down
46 changes: 38 additions & 8 deletions test/unit/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,42 @@ if ( !jQuery.fx ) {
return;
}

var oldRaf = window.requestAnimationFrame,
hideOptions = {
inline: function() { jQuery.style( this, "display", "none" ); },
cascade: function() { this.className = "hidden"; }
};
var hideOptions = {
inline: function() { jQuery.style( this, "display", "none" ); },
cascade: function() { this.className = "hidden"; }
};

QUnit.module( "effects", {
beforeEach: function() {
this.sandbox = sinon.createSandbox();
this.clock = this.sandbox.useFakeTimers( 505877050 );

this._oldInterval = jQuery.fx.interval;
window.requestAnimationFrame = null;
jQuery.fx.step = {};
jQuery.fx.interval = 10;

// Simulate rAF using the mocked setTimeout as otherwise we couldn't test
// the rAF + performance.now code path.
this.sandbox
.stub( window, "requestAnimationFrame" )
.callsFake( function( callback ) {
return window.setTimeout( callback, jQuery.fx.interval,
Date.now() - 99989.6394 );
} );

// Fake performance.now() returning lower values than Date.now()
// and that its values are fractional.
this.sandbox.stub( window.performance, "now" ).callsFake( function() {
return Date.now() - 99999.6394;
} );
},
afterEach: function() {
this.sandbox.restore();
jQuery.fx.stop();
jQuery.fx.interval = this._oldInterval;
window.requestAnimationFrame = oldRaf;
return moduleTeardown.apply( this, arguments );
}
} );
}, function() {

QUnit[ jQuery.find.compile ? "test" : "skip" ]( "sanity check", function( assert ) {
assert.expect( 1 );
Expand Down Expand Up @@ -2621,4 +2634,21 @@ QUnit.test( "jQuery.speed() - durations", function( assert ) {
jQuery.fx.off = false;
} );

} );

QUnit.module( "effects-nostubs", function() {

testIframe(
"matching timestamp",
"effects/matchingTimestamps.html",
function( assert, framejQuery, frameWin, frameDoc, success, failureReason ) {
assert.expect( 1 );

assert.ok( success, failureReason );
}
);

} );

} )();

4 changes: 0 additions & 4 deletions test/unit/tween.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ if ( !jQuery.fx ) {
return;
}

var oldRaf = window.requestAnimationFrame;

QUnit.module( "tween", {
beforeEach: function() {
this.sandbox = sinon.createSandbox();
this.clock = this.sandbox.useFakeTimers( 505877050 );
this._oldInterval = jQuery.fx.interval;
window.requestAnimationFrame = null;
jQuery.fx.step = {};
jQuery.fx.interval = 10;
},
afterEach: function() {
this.sandbox.restore();
jQuery.fx.stop();
jQuery.fx.interval = this._oldInterval;
window.requestAnimationFrame = oldRaf;
return moduleTeardown.apply( this, arguments );
}
} );
Expand Down

0 comments on commit f089df5

Please sign in to comment.