Skip to content

Commit c7223b9

Browse files
committed
Easings: Rewrote all easings to only rely on state and reduce code size. Fixes #8115 - Easings: Simplify equations to only rely on state.
(cherry picked from commit c0093b5) Conflicts: ui/jquery.effects.core.js
1 parent 5e935ea commit c7223b9

File tree

2 files changed

+53
-213
lines changed

2 files changed

+53
-213
lines changed

demos/effect/easing.html

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,33 +15,31 @@
1515
</style>
1616
<script>
1717
$(function() {
18-
if ( !$( "<canvas/>" )[0].getContext ) {
19-
$( "<div/>" ).text(
20-
"Your browser doesn't support canvas, which is required for this demo. " +
21-
"Give Firefox 3 a try!"
18+
if ( !$( "<canvas>" )[0].getContext ) {
19+
$( "<div>" ).text(
20+
"Your browser doesn't support canvas, which is required for this demo."
2221
).appendTo( "#graphs" );
2322
return;
2423
}
2524

2625
var i = 0,
2726
width = 100,
2827
height = 100;
28+
2929
$.each( $.easing, function( name, impl ) {
30-
// skip linear/jswing and any non functioning implementation
31-
if ( !$.isFunction( impl ) || /jswing/.test( name ) ) {
32-
return;
33-
}
34-
var graph = $( "<div/>" ).addClass( "graph" ).appendTo( "#graphs" ),
35-
text = $( "<div/>" ).text( ++i + ". " + name ).appendTo( graph ),
36-
wrap = $( "<div/>" ).appendTo( graph ).css( 'overflow', 'hidden' ),
37-
canvas = $( "<canvas/>" ).appendTo( wrap )[ 0 ];
30+
var graph = $( "<div>" ).addClass( "graph" ).appendTo( "#graphs" ),
31+
text = $( "<div>" ).text( ++i + ". " + name ).appendTo( graph ),
32+
wrap = $( "<div>" ).appendTo( graph ).css( 'overflow', 'hidden' ),
33+
canvas = $( "<canvas>" ).appendTo( wrap )[ 0 ];
34+
3835
canvas.width = width;
3936
canvas.height = height;
4037
var drawHeight = height * 0.8,
4138
cradius = 10;
4239
ctx = canvas.getContext( "2d" );
4340
ctx.fillStyle = "black";
4441

42+
// draw background
4543
ctx.beginPath();
4644
ctx.moveTo( cradius, 0 );
4745
ctx.quadraticCurveTo( 0, 0, 0, cradius );
@@ -53,31 +51,34 @@
5351
ctx.lineTo( cradius, 0 );
5452
ctx.fill();
5553

54+
// draw bottom line
5655
ctx.strokeStyle = "#555";
5756
ctx.beginPath();
5857
ctx.moveTo( width * 0.1, drawHeight + .5 );
5958
ctx.lineTo( width * 0.9, drawHeight + .5 );
6059
ctx.stroke();
6160

61+
// draw top line
6262
ctx.strokeStyle = "#555";
6363
ctx.beginPath();
6464
ctx.moveTo( width * 0.1, drawHeight * .3 - .5 );
6565
ctx.lineTo( width * 0.9, drawHeight * .3 - .5 );
6666
ctx.stroke();
67-
67+
68+
// plot easing
6869
ctx.strokeStyle = "white";
6970
ctx.beginPath();
7071
ctx.lineWidth = 2;
7172
ctx.moveTo( width * 0.1, drawHeight );
7273
$.each( new Array( width ), function( position ) {
73-
var val = impl( 0, position, 0, 1, height );
74-
if ( /linear|jswing/.test( name ) ) {
75-
val = position / width;
76-
}
74+
var state = position / width,
75+
val = impl( state, position, 0, 1, width );
7776
ctx.lineTo( position * 0.8 + width * 0.1,
7877
drawHeight - drawHeight * val * 0.7 );
7978
});
8079
ctx.stroke();
80+
81+
// animate on click
8182
graph.click(function() {
8283
wrap
8384
.animate( { height: "hide" }, 2000, name )

ui/jquery.effects.core.js

Lines changed: 35 additions & 196 deletions
Original file line numberDiff line numberDiff line change
@@ -564,210 +564,49 @@ $.fn.extend({
564564
/*********************************** EASING ***********************************/
565565
/******************************************************************************/
566566

567-
/*
568-
* jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
569-
*
570-
* Uses the built in easing capabilities added In jQuery 1.1
571-
* to offer multiple easing options
572-
*
573-
* TERMS OF USE - jQuery Easing
574-
*
575-
* Open source under the BSD License.
576-
*
577-
* Copyright 2008 George McGinley Smith
578-
* All rights reserved.
579-
*
580-
* Redistribution and use in source and binary forms, with or without modification,
581-
* are permitted provided that the following conditions are met:
582-
*
583-
* Redistributions of source code must retain the above copyright notice, this list of
584-
* conditions and the following disclaimer.
585-
* Redistributions in binary form must reproduce the above copyright notice, this list
586-
* of conditions and the following disclaimer in the documentation and/or other materials
587-
* provided with the distribution.
588-
*
589-
* Neither the name of the author nor the names of contributors may be used to endorse
590-
* or promote products derived from this software without specific prior written permission.
591-
*
592-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
593-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
594-
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
595-
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
596-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
597-
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
598-
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
599-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
600-
* OF THE POSSIBILITY OF SUCH DAMAGE.
601-
*
602-
*/
567+
// based on easing equations from Robert Penner (http://www.robertpenner.com/easing)
603568

604-
// t: current time, b: begInnIng value, c: change In value, d: duration
605-
$.easing.jswing = $.easing.swing;
569+
var baseEasings = {};
606570

607-
$.extend($.easing,
608-
{
609-
def: 'easeOutQuad',
610-
swing: function (x, t, b, c, d) {
611-
//alert($.easing.default);
612-
return $.easing[$.easing.def](x, t, b, c, d);
613-
},
614-
easeInQuad: function (x, t, b, c, d) {
615-
return c*(t/=d)*t + b;
616-
},
617-
easeOutQuad: function (x, t, b, c, d) {
618-
return -c *(t/=d)*(t-2) + b;
619-
},
620-
easeInOutQuad: function (x, t, b, c, d) {
621-
if ((t/=d/2) < 1) return c/2*t*t + b;
622-
return -c/2 * ((--t)*(t-2) - 1) + b;
623-
},
624-
easeInCubic: function (x, t, b, c, d) {
625-
return c*(t/=d)*t*t + b;
626-
},
627-
easeOutCubic: function (x, t, b, c, d) {
628-
return c*((t=t/d-1)*t*t + 1) + b;
629-
},
630-
easeInOutCubic: function (x, t, b, c, d) {
631-
if ((t/=d/2) < 1) return c/2*t*t*t + b;
632-
return c/2*((t-=2)*t*t + 2) + b;
633-
},
634-
easeInQuart: function (x, t, b, c, d) {
635-
return c*(t/=d)*t*t*t + b;
636-
},
637-
easeOutQuart: function (x, t, b, c, d) {
638-
return -c * ((t=t/d-1)*t*t*t - 1) + b;
639-
},
640-
easeInOutQuart: function (x, t, b, c, d) {
641-
if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
642-
return -c/2 * ((t-=2)*t*t*t - 2) + b;
643-
},
644-
easeInQuint: function (x, t, b, c, d) {
645-
return c*(t/=d)*t*t*t*t + b;
646-
},
647-
easeOutQuint: function (x, t, b, c, d) {
648-
return c*((t=t/d-1)*t*t*t*t + 1) + b;
649-
},
650-
easeInOutQuint: function (x, t, b, c, d) {
651-
if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
652-
return c/2*((t-=2)*t*t*t*t + 2) + b;
653-
},
654-
easeInSine: function (x, t, b, c, d) {
655-
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
656-
},
657-
easeOutSine: function (x, t, b, c, d) {
658-
return c * Math.sin(t/d * (Math.PI/2)) + b;
659-
},
660-
easeInOutSine: function (x, t, b, c, d) {
661-
return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
662-
},
663-
easeInExpo: function (x, t, b, c, d) {
664-
return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
665-
},
666-
easeOutExpo: function (x, t, b, c, d) {
667-
return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
668-
},
669-
easeInOutExpo: function (x, t, b, c, d) {
670-
if (t==0) return b;
671-
if (t==d) return b+c;
672-
if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
673-
return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
674-
},
675-
easeInCirc: function (x, t, b, c, d) {
676-
return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
677-
},
678-
easeOutCirc: function (x, t, b, c, d) {
679-
return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
680-
},
681-
easeInOutCirc: function (x, t, b, c, d) {
682-
if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
683-
return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
684-
},
685-
easeInElastic: function (x, t, b, c, d) {
686-
var s=1.70158;var p=0;var a=c;
687-
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
688-
if (a < Math.abs(c)) { a=c; var s=p/4; }
689-
else var s = p/(2*Math.PI) * Math.asin (c/a);
690-
return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
691-
},
692-
easeOutElastic: function (x, t, b, c, d) {
693-
var s=1.70158;var p=0;var a=c;
694-
if (t==0) return b; if ((t/=d)==1) return b+c; if (!p) p=d*.3;
695-
if (a < Math.abs(c)) { a=c; var s=p/4; }
696-
else var s = p/(2*Math.PI) * Math.asin (c/a);
697-
return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
698-
},
699-
easeInOutElastic: function (x, t, b, c, d) {
700-
var s=1.70158;var p=0;var a=c;
701-
if (t==0) return b; if ((t/=d/2)==2) return b+c; if (!p) p=d*(.3*1.5);
702-
if (a < Math.abs(c)) { a=c; var s=p/4; }
703-
else var s = p/(2*Math.PI) * Math.asin (c/a);
704-
if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
705-
return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
706-
},
707-
easeInBack: function (x, t, b, c, d, s) {
708-
if (s == undefined) s = 1.70158;
709-
return c*(t/=d)*t*((s+1)*t - s) + b;
710-
},
711-
easeOutBack: function (x, t, b, c, d, s) {
712-
if (s == undefined) s = 1.70158;
713-
return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
571+
$.each( [ "Quad", "Cubic", "Quart", "Quint", "Expo" ], function( i, name ) {
572+
baseEasings[ name ] = function( p ) {
573+
return Math.pow( p, i + 2 );
574+
};
575+
});
576+
577+
$.extend( baseEasings, {
578+
Sine: function ( p ) {
579+
return 1 - Math.cos( p * Math.PI / 2 );
714580
},
715-
easeInOutBack: function (x, t, b, c, d, s) {
716-
if (s == undefined) s = 1.70158;
717-
if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
718-
return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
581+
Circ: function ( p ) {
582+
return 1 - Math.sqrt( 1 - p * p );
719583
},
720-
easeInBounce: function (x, t, b, c, d) {
721-
return c - $.easing.easeOutBounce (x, d-t, 0, c, d) + b;
584+
Elastic: function( p ) {
585+
return p === 0 || p === 1 ? p :
586+
-Math.pow( 2, 8 * (p - 1) ) * Math.sin( ( (p - 1) * 80 - 7.5 ) * Math.PI / 15 );
722587
},
723-
easeOutBounce: function (x, t, b, c, d) {
724-
if ((t/=d) < (1/2.75)) {
725-
return c*(7.5625*t*t) + b;
726-
} else if (t < (2/2.75)) {
727-
return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
728-
} else if (t < (2.5/2.75)) {
729-
return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
730-
} else {
731-
return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
732-
}
588+
Back: function( p ) {
589+
return p * p * ( 3 * p - 2 );
733590
},
734-
easeInOutBounce: function (x, t, b, c, d) {
735-
if (t < d/2) return $.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
736-
return $.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
591+
Bounce: function ( p ) {
592+
var pow2,
593+
bounce = 4;
594+
595+
while ( p < ( ( pow2 = Math.pow( 2, --bounce ) ) - 1 ) / 11 ) {}
596+
return 1 / Math.pow( 4, 3 - bounce ) - 7.5625 * Math.pow( ( pow2 * 3 - 2 ) / 22 - p, 2 );
737597
}
738598
});
739599

740-
/*
741-
*
742-
* TERMS OF USE - EASING EQUATIONS
743-
*
744-
* Open source under the BSD License.
745-
*
746-
* Copyright 2001 Robert Penner
747-
* All rights reserved.
748-
*
749-
* Redistribution and use in source and binary forms, with or without modification,
750-
* are permitted provided that the following conditions are met:
751-
*
752-
* Redistributions of source code must retain the above copyright notice, this list of
753-
* conditions and the following disclaimer.
754-
* Redistributions in binary form must reproduce the above copyright notice, this list
755-
* of conditions and the following disclaimer in the documentation and/or other materials
756-
* provided with the distribution.
757-
*
758-
* Neither the name of the author nor the names of contributors may be used to endorse
759-
* or promote products derived from this software without specific prior written permission.
760-
*
761-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
762-
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
763-
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
764-
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
765-
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
766-
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
767-
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
768-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
769-
* OF THE POSSIBILITY OF SUCH DAMAGE.
770-
*
771-
*/
600+
$.each( baseEasings, function( name, easeIn ) {
601+
$.easing[ "easeIn" + name ] = easeIn;
602+
$.easing[ "easeOut" + name ] = function( p ) {
603+
return 1 - easeIn( 1 - p );
604+
};
605+
$.easing[ "easeInOut" + name ] = function( p ) {
606+
return p < .5 ?
607+
easeIn( p * 2 ) / 2 :
608+
easeIn( p * -2 + 2 ) / -2 + 1;
609+
};
610+
});
772611

773612
})(jQuery);

0 commit comments

Comments
 (0)