My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/**
* PennerDoubleAnimation
* Animates the value of a double property between two target values using
* Robert Penner's easing equations for interpolation over a specified Duration.
*
* @author Darren David darren-code@lookorfeel.com
* @version 1.0
*
* Credit/Thanks:
* Robert Penner - The easing equations we all know and love
* (http://robertpenner.com/easing/) [See License.txt for license info]
*
* Lee Brimelow - initial port of Penner's equations to WPF
* (http://thewpfblog.com/?p=12)
*
* Zeh Fernando - additional equations (out/in) from
* caurina.transitions.Tweener (http://code.google.com/p/tweener/)
* [See License.txt for license info]
*/


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using System.ComponentModel;
using System.Globalization;

namespace LookOrFeel.Animation
{
/// <summary>
/// Animates the value of a double property between two target values using
/// Robert Penner's easing equations for interpolation over a specified Duration.
/// </summary>
/// <example>
/// <code>
/// // C#
/// PennerDoubleAnimation anim = new PennerDoubleAnimation();
/// anim.Type = PennerDoubleAnimation.Equations.Linear;
/// anim.From = 1;
/// anim.To = 0;
/// myControl.BeginAnimation( OpacityProperty, anim );
///
/// // XAML
/// <Storyboard x:Key="AnimateXamlRect">
/// <animation:PennerDoubleAnimation
/// Storyboard.TargetName="myControl"
/// Storyboard.TargetProperty="(Canvas.Left)"
/// From="0"
/// To="600"
/// Equation="BackEaseOut"
/// Duration="00:00:05" />
/// </Storyboard>
///
/// <Control.Triggers>
/// <EventTrigger RoutedEvent="FrameworkElement.Loaded">
/// <BeginStoryboard Storyboard="{StaticResource AnimateXamlRect}"/>
/// </EventTrigger>
/// </Control.Triggers>
/// </code>
/// </example>
public class PennerDoubleAnimation : DoubleAnimationBase
{
/// <summary>
/// Enumeration of all easing equations.
/// </summary>
public enum Equations
{
Linear,
QuadEaseOut, QuadEaseIn, QuadEaseInOut, QuadEaseOutIn,
ExpoEaseOut, ExpoEaseIn, ExpoEaseInOut, ExpoEaseOutIn,
CubicEaseOut, CubicEaseIn, CubicEaseInOut, CubicEaseOutIn,
QuartEaseOut, QuartEaseIn, QuartEaseInOut, QuartEaseOutIn,
QuintEaseOut, QuintEaseIn, QuintEaseInOut, QuintEaseOutIn,
CircEaseOut, CircEaseIn, CircEaseInOut, CircEaseOutIn,
SineEaseOut, SineEaseIn, SineEaseInOut, SineEaseOutIn,
ElasticEaseOut, ElasticEaseIn, ElasticEaseInOut, ElasticEaseOutIn,
BounceEaseOut, BounceEaseIn, BounceEaseInOut, BounceEaseOutIn,
BackEaseOut, BackEaseIn, BackEaseInOut, BackEaseOutIn
}

#region Fields

private MethodInfo _EasingMethod;

#endregion

#region Dependency Properties

public static readonly DependencyProperty EquationProperty =
DependencyProperty.Register(
"Equation", typeof( Equations ), typeof( PennerDoubleAnimation ),
new PropertyMetadata( Equations.Linear, new PropertyChangedCallback( HandleEquationChanged ) ) );

public static readonly DependencyProperty FromProperty =
DependencyProperty.Register(
"From", typeof( double ), typeof( PennerDoubleAnimation ), new PropertyMetadata( 0.0 ) );

public static readonly DependencyProperty ToProperty =
DependencyProperty.Register(
"To", typeof( double ), typeof( PennerDoubleAnimation ), new PropertyMetadata( 0.0 ) );

#endregion

#region Constructors

public PennerDoubleAnimation()
{
}

public PennerDoubleAnimation( Equations type, double from, double to )
{
Equation = type;
From = from;
To = to;
}

public PennerDoubleAnimation( Equations type, double from, double to, Duration duration )
{
Equation = type;
From = from;
To = to;
Duration = duration;
}

#endregion

#region Abstract Member Implementations

protected override double GetCurrentValueCore( double startValue, double targetValue, AnimationClock clock )
{
try
{
// might be able to speed this up by caching it, but who knows
object[] methodParams = new object[4] { clock.CurrentTime.Value.TotalSeconds, From, To - From, Duration.TimeSpan.TotalSeconds };

return ( double ) _EasingMethod.Invoke( this, methodParams );
}
catch
{
return From;
}
}

protected override Freezable CreateInstanceCore()
{
return new PennerDoubleAnimation();
}

#endregion

#region Equations

// These methods are all public to enable reflection in GetCurrentValueCore.

#region Linear

/// <summary>
/// Easing equation function for a simple linear tweening, with no easing.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double Linear( double t, double b, double c, double d )
{
return c * t / d + b;
}

#endregion

#region Expo

/// <summary>
/// Easing equation function for an exponential (2^t) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ExpoEaseOut( double t, double b, double c, double d )
{
return ( t == d ) ? b + c : c * ( -Math.Pow( 2, -10 * t / d ) + 1 ) + b;
}

/// <summary>
/// Easing equation function for an exponential (2^t) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ExpoEaseIn( double t, double b, double c, double d )
{
return ( t == 0 ) ? b : c * Math.Pow( 2, 10 * ( t / d - 1 ) ) + b;
}

/// <summary>
/// Easing equation function for an exponential (2^t) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ExpoEaseInOut( double t, double b, double c, double d )
{
if ( t == 0 )
return b;

if ( t == d )
return b + c;

if ( ( t /= d / 2 ) < 1 )
return c / 2 * Math.Pow( 2, 10 * ( t - 1 ) ) + b;

return c / 2 * ( -Math.Pow( 2, -10 * --t ) + 2 ) + b;
}

/// <summary>
/// Easing equation function for an exponential (2^t) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ExpoEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return ExpoEaseOut( t * 2, b, c / 2, d );

return ExpoEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Circular

/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CircEaseOut( double t, double b, double c, double d )
{
return c * Math.Sqrt( 1 - ( t = t / d - 1 ) * t ) + b;
}

/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CircEaseIn( double t, double b, double c, double d )
{
return -c * ( Math.Sqrt( 1 - ( t /= d ) * t ) - 1 ) + b;
}

/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CircEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return -c / 2 * ( Math.Sqrt( 1 - t * t ) - 1 ) + b;

return c / 2 * ( Math.Sqrt( 1 - ( t -= 2 ) * t ) + 1 ) + b;
}

/// <summary>
/// Easing equation function for a circular (sqrt(1-t^2)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CircEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return CircEaseOut( t * 2, b, c / 2, d );

return CircEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Quad

/// <summary>
/// Easing equation function for a quadratic (t^2) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuadEaseOut( double t, double b, double c, double d )
{
return -c * ( t /= d ) * ( t - 2 ) + b;
}

/// <summary>
/// Easing equation function for a quadratic (t^2) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuadEaseIn( double t, double b, double c, double d )
{
return c * ( t /= d ) * t + b;
}

/// <summary>
/// Easing equation function for a quadratic (t^2) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuadEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t + b;

return -c / 2 * ( ( --t ) * ( t - 2 ) - 1 ) + b;
}

/// <summary>
/// Easing equation function for a quadratic (t^2) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuadEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return QuadEaseOut( t * 2, b, c / 2, d );

return QuadEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Sine

/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double SineEaseOut( double t, double b, double c, double d )
{
return c * Math.Sin( t / d * ( Math.PI / 2 ) ) + b;
}

/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double SineEaseIn( double t, double b, double c, double d )
{
return -c * Math.Cos( t / d * ( Math.PI / 2 ) ) + c + b;
}

/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double SineEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * ( Math.Sin( Math.PI * t / 2 ) ) + b;

return -c / 2 * ( Math.Cos( Math.PI * --t / 2 ) - 2 ) + b;
}

/// <summary>
/// Easing equation function for a sinusoidal (sin(t)) easing in/out:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double SineEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return SineEaseOut( t * 2, b, c / 2, d );

return SineEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Cubic

/// <summary>
/// Easing equation function for a cubic (t^3) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CubicEaseOut( double t, double b, double c, double d )
{
return c * ( ( t = t / d - 1 ) * t * t + 1 ) + b;
}

/// <summary>
/// Easing equation function for a cubic (t^3) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CubicEaseIn( double t, double b, double c, double d )
{
return c * ( t /= d ) * t * t + b;
}

/// <summary>
/// Easing equation function for a cubic (t^3) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CubicEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t + b;

return c / 2 * ( ( t -= 2 ) * t * t + 2 ) + b;
}

/// <summary>
/// Easing equation function for a cubic (t^3) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double CubicEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return CubicEaseOut( t * 2, b, c / 2, d );

return CubicEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Quartic

/// <summary>
/// Easing equation function for a quartic (t^4) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuartEaseOut( double t, double b, double c, double d )
{
return -c * ( ( t = t / d - 1 ) * t * t * t - 1 ) + b;
}

/// <summary>
/// Easing equation function for a quartic (t^4) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuartEaseIn( double t, double b, double c, double d )
{
return c * ( t /= d ) * t * t * t + b;
}

/// <summary>
/// Easing equation function for a quartic (t^4) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuartEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t * t + b;

return -c / 2 * ( ( t -= 2 ) * t * t * t - 2 ) + b;
}

/// <summary>
/// Easing equation function for a quartic (t^4) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuartEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return QuartEaseOut( t * 2, b, c / 2, d );

return QuartEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Quintic

/// <summary>
/// Easing equation function for a quintic (t^5) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuintEaseOut( double t, double b, double c, double d )
{
return c * ( ( t = t / d - 1 ) * t * t * t * t + 1 ) + b;
}

/// <summary>
/// Easing equation function for a quintic (t^5) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuintEaseIn( double t, double b, double c, double d )
{
return c * ( t /= d ) * t * t * t * t + b;
}

/// <summary>
/// Easing equation function for a quintic (t^5) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuintEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) < 1 )
return c / 2 * t * t * t * t * t + b;
return c / 2 * ( ( t -= 2 ) * t * t * t * t + 2 ) + b;
}

/// <summary>
/// Easing equation function for a quintic (t^5) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double QuintEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return QuintEaseOut( t * 2, b, c / 2, d );
return QuintEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Elastic

/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ElasticEaseOut( double t, double b, double c, double d )
{
if ( ( t /= d ) == 1 )
return b + c;

double p = d * .3;
double s = p / 4;

return ( c * Math.Pow( 2, -10 * t ) * Math.Sin( ( t * d - s ) * ( 2 * Math.PI ) / p ) + c + b );
}

/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ElasticEaseIn( double t, double b, double c, double d )
{
if ( ( t /= d ) == 1 )
return b + c;

double p = d * .3;
double s = p / 4;

return -( c * Math.Pow( 2, 10 * ( t -= 1 ) ) * Math.Sin( ( t * d - s ) * ( 2 * Math.PI ) / p ) ) + b;
}

/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ElasticEaseInOut( double t, double b, double c, double d )
{
if ( ( t /= d / 2 ) == 2 )
return b + c;

double p = d * ( .3 * 1.5 );
double s = p / 4;

if ( t < 1 )
return -.5 * ( c * Math.Pow( 2, 10 * ( t -= 1 ) ) * Math.Sin( ( t * d - s ) * ( 2 * Math.PI ) / p ) ) + b;
return c * Math.Pow( 2, -10 * ( t -= 1 ) ) * Math.Sin( ( t * d - s ) * ( 2 * Math.PI ) / p ) * .5 + c + b;
}

/// <summary>
/// Easing equation function for an elastic (exponentially decaying sine wave) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double ElasticEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return ElasticEaseOut( t * 2, b, c / 2, d );
return ElasticEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Bounce

/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BounceEaseOut( double t, double b, double c, double d )
{
if ( ( t /= d ) < ( 1 / 2.75 ) )
return c * ( 7.5625 * t * t ) + b;
else if ( t < ( 2 / 2.75 ) )
return c * ( 7.5625 * ( t -= ( 1.5 / 2.75 ) ) * t + .75 ) + b;
else if ( t < ( 2.5 / 2.75 ) )
return c * ( 7.5625 * ( t -= ( 2.25 / 2.75 ) ) * t + .9375 ) + b;
else
return c * ( 7.5625 * ( t -= ( 2.625 / 2.75 ) ) * t + .984375 ) + b;
}

/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BounceEaseIn( double t, double b, double c, double d )
{
return c - BounceEaseOut( d - t, 0, c, d ) + b;
}

/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BounceEaseInOut( double t, double b, double c, double d )
{
if ( t < d / 2 )
return BounceEaseIn( t * 2, 0, c, d ) * .5 + b;
else
return BounceEaseOut( t * 2 - d, 0, c, d ) * .5 + c * .5 + b;
}

/// <summary>
/// Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BounceEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return BounceEaseOut( t * 2, b, c / 2, d );
return BounceEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#region Back

/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out:
/// decelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BackEaseOut( double t, double b, double c, double d )
{
return c * ( ( t = t / d - 1 ) * t * ( ( 1.70158 + 1 ) * t + 1.70158 ) + 1 ) + b;
}

/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in:
/// accelerating from zero velocity.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BackEaseIn( double t, double b, double c, double d )
{
return c * ( t /= d ) * t * ( ( 1.70158 + 1 ) * t - 1.70158 ) + b;
}

/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in/out:
/// acceleration until halfway, then deceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BackEaseInOut( double t, double b, double c, double d )
{
double s = 1.70158;
if ( ( t /= d / 2 ) < 1 )
return c / 2 * ( t * t * ( ( ( s *= ( 1.525 ) ) + 1 ) * t - s ) ) + b;
return c / 2 * ( ( t -= 2 ) * t * ( ( ( s *= ( 1.525 ) ) + 1 ) * t + s ) + 2 ) + b;
}

/// <summary>
/// Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out/in:
/// deceleration until halfway, then acceleration.
/// </summary>
/// <param name="t">Current time in seconds.</param>
/// <param name="b">Starting value.</param>
/// <param name="c">Final value.</param>
/// <param name="d">Duration of animation.</param>
/// <returns>The correct value.</returns>
public static double BackEaseOutIn( double t, double b, double c, double d )
{
if ( t < d / 2 )
return BackEaseOut( t * 2, b, c / 2, d );
return BackEaseIn( ( t * 2 ) - d, b + c / 2, c / 2, d );
}

#endregion

#endregion

#region Event Handlers

private static void HandleEquationChanged( object sender, DependencyPropertyChangedEventArgs e )
{
PennerDoubleAnimation pda = sender as PennerDoubleAnimation;

// cache method so we avoid lookup while animating
pda._EasingMethod = typeof( PennerDoubleAnimation ).GetMethod( e.NewValue.ToString() );
}

#endregion

#region Properties

/// <summary>
/// The easing equation to use.
/// </summary>
[TypeConverter( typeof( PennerDoubleAnimationTypeConverter ) )]
public Equations Equation
{
get { return ( Equations ) GetValue( EquationProperty ); }
set
{
SetValue( EquationProperty, value );

// cache method so we avoid lookup while animating
_EasingMethod = this.GetType().GetMethod( value.ToString() );
}
}

/// <summary>
/// Starting value for the animation.
/// </summary>
public double From
{
get { return ( double ) GetValue( FromProperty ); }
set { SetValue( FromProperty, value ); }
}

/// <summary>
/// Ending value for the animation.
/// </summary>
public double To
{
get { return ( double ) GetValue( ToProperty ); }
set { SetValue( ToProperty, value ); }
}

#endregion
}

public class PennerDoubleAnimationTypeConverter : TypeConverter
{
public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
{
return sourceType == typeof( string );
}

public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType )
{
return destinationType == typeof( Enum );
}

public override object ConvertFrom( ITypeDescriptorContext context, CultureInfo culture, object value )
{
foreach ( int i in Enum.GetValues( typeof( PennerDoubleAnimation.Equations ) ) )
{
if ( Enum.GetName( typeof( PennerDoubleAnimation.Equations ), i ) == value.ToString() )
return ( PennerDoubleAnimation.Equations ) i;
}
return null;
}

public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType )
{
if ( value != null )
{
return ( ( PennerDoubleAnimation.Equations ) value ).ToString();
}
return null;
}
}
}
Show details Hide details

Change log

r10 by darrendavid on Aug 15, 2007   Diff
migrating files into WPF dir
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by darrendavid on May 15, 2007   Diff
cleaning up source tree
r4 by darrendavid on May 15, 2007   Diff
changed HandleEquationChanged to cache
easing method directly rather than
call the CLR setter.
r2 by darrendavid on May 14, 2007   Diff
Initial commit.
All revisions of this file

File info

Size: 37890 bytes, 945 lines

File properties

svn:executable
*
Hosted by Google Code