Inheritance: DependencyObject, IEasingFunctionBase
 public static async Task FadeInAsync(this UIElement element, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.Opacity < 1.0)
     {
         await AnimateDoublePropertyAsync(element, "Opacity", element.Opacity, 1.0, duration, easingFunction);
     }
 }
        public void Draw(EasingFunctionBase easingFunction)
        {
            canvas1.Children.Clear();


            var pathSegments = new PathSegmentCollection();

            for (double i = 0; i < 1; i += SamplingInterval)
            {
                double x = i * canvas1.Width;
                double y = easingFunction.Ease(i) * canvas1.Height;

                var segment = new LineSegment();
                segment.Point = new Point(x, y);

                pathSegments.Add(segment);

            }
            var p = new Path();
            p.Stroke = new SolidColorBrush(Colors.Black);
            p.StrokeThickness = 3;
            var figures = new PathFigureCollection();
            figures.Add(new PathFigure() { Segments = pathSegments });
            p.Data = new PathGeometry() { Figures = figures };
            canvas1.Children.Add(p);
        }
        /// <summary>
        /// Starts animating a dependency property of a framework element to a 
        /// target value.
        /// </summary>
        /// <param name="target">The element to animate.</param>
        /// <param name="animatingDependencyProperty">The dependency property to
        /// animate.</param>
        /// <param name="propertyPath">The path of the dependency property to 
        /// animate.</param>
        /// <param name="targetValue">The value to animate the dependency
        /// property to.</param>
        /// <param name="timeSpan">The duration of the animation.</param>
        /// <param name="easingFunction">The easing function to uses to
        /// transition the data points.</param>
        public static void BeginAnimation(
            this FrameworkElement target,
            DependencyProperty animatingDependencyProperty,
            string propertyPath,
            object targetValue,
            TimeSpan timeSpan,
            EasingFunctionBase easingFunction)
        {
            Storyboard storyBoard = target.Resources[GetStoryboardKey(propertyPath)] as Storyboard;

            if (storyBoard != null)
            {
                // Save current value
                object currentValue = target.GetValue(animatingDependencyProperty);
                storyBoard.Stop();
                // RestoreAsync that value so it doesn't snap back to its starting value
                target.SetValue(animatingDependencyProperty, currentValue);
                target.Resources.Remove(GetStoryboardKey(propertyPath));
            }

            storyBoard = CreateStoryboard(target, animatingDependencyProperty, propertyPath, ref targetValue, timeSpan, easingFunction);

            storyBoard.Completed += 
                (source, args) =>
                    {
                        storyBoard.Stop();
                        target.SetValue(animatingDependencyProperty, targetValue);
                        target.Resources.Remove(GetStoryboardKey(propertyPath));
                    };

            target.Resources.Add(GetStoryboardKey(propertyPath), storyBoard);
            storyBoard.Begin();
        }
 public static async Task AnimateHeightAsync(this FrameworkElement element, double height, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.Height != height)
     {
         await AnimateDoublePropertyAsync(element, "Height", element.ActualHeight, height, duration, easingFunction);
     }
 }
 public static async Task AnimateWidthAsync(this FrameworkElement element, double width, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.ActualWidth != width)
     {
         await AnimateDoublePropertyAsync(element, "Width", element.ActualWidth, width, duration, easingFunction);
     }
 }
 public static async Task AnimateYAsync(this FrameworkElement element, double y, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.GetTranslateY() != y)
     {
         await AnimateDoublePropertyAsync(element.GetCompositeTransform(), "TranslateY", element.GetTranslateY(), y, duration, easingFunction);
     }
 }
        private void StartAnimation(EasingFunctionBase easingFunction)
        {
            // show the chart
            chartControl.Draw(easingFunction);

            // animation
#if WPF
            NameScope.SetNameScope(translate1, new NameScope());
#endif

            var storyboard = new Storyboard();
            var ellipseMove = new DoubleAnimation();
            ellipseMove.EasingFunction = easingFunction;
            ellipseMove.Duration = new Duration(TimeSpan.FromSeconds(AnimationTimeSeconds));
            ellipseMove.From = 0;
            ellipseMove.To = 460;
#if WPF
            Storyboard.SetTargetName(ellipseMove, nameof(translate1));
            Storyboard.SetTargetProperty(ellipseMove, new PropertyPath(TranslateTransform.XProperty));
#else
            Storyboard.SetTarget(ellipseMove, translate1);
            Storyboard.SetTargetProperty(ellipseMove, "X");
#endif

            ellipseMove.BeginTime = TimeSpan.FromSeconds(0.5); // start animation in 0.5 seconds
            ellipseMove.FillBehavior = FillBehavior.HoldEnd; // keep position after animation

            storyboard.Children.Add(ellipseMove);
#if WPF
            storyboard.Begin(this);
#else
            storyboard.Begin();
#endif
        }
Exemplo n.º 8
0
 public static async Task AnimateScaleXAsync(this FrameworkElement element, double x, double duration = 150, EasingFunctionBase easingFunction = null)
 {
     if (element.GetScaleX() != x)
     {
         await AnimateDoublePropertyAsync(element.GetCompositeTransform(), "ScaleX", element.GetScaleX(), x, duration, easingFunction);
     }
 }
Exemplo n.º 9
0
 public static Storyboard AnimateScaleX(this FrameworkElement element, double x, double duration = 150, EasingFunctionBase easingFunction = null)
 {
     if (element.GetScaleX() != x)
     {
         return AnimateDoubleProperty(element.GetCompositeTransform(), "ScaleX", element.GetScaleX(), x, duration, easingFunction);
     }
     return null;
 }
 public static Storyboard FadeIn(this UIElement element, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.Opacity < 1.0)
     {
         return AnimateDoubleProperty(element, "Opacity", element.Opacity, 1.0, duration, easingFunction);
     }
     return null;
 }
 public static Storyboard AnimateY(this FrameworkElement element, double y, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.GetTranslateY() != y)
     {
         return AnimateDoubleProperty(element.GetCompositeTransform(), "TranslateY", element.GetTranslateY(), y, duration, easingFunction);
     }
     return null;
 }
 public static Storyboard AnimateWidth(this FrameworkElement element, double width, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.ActualWidth != width)
     {
         return AnimateDoubleProperty(element, "Width", element.ActualWidth, width, duration, easingFunction);
     }
     return null;
 }
 public static Storyboard AnimateHeight(this FrameworkElement element, double height, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     if (element.Height != height)
     {
         return AnimateDoubleProperty(element, "Height", element.ActualHeight, height, duration, easingFunction);
     }
     return null;
 }
Exemplo n.º 14
0
 /// <summary>
 /// 创建动画
 /// </summary> 
 public static DoubleAnimation CreatAnimation(DependencyObject obj, string path, int duration, EasingFunctionBase easing = null)
 {
     DoubleAnimation da = new DoubleAnimation();
     da.EasingFunction = easing;
     da.Duration = TimeSpan.FromMilliseconds(duration);
     Storyboard.SetTarget(da, obj);
     Storyboard.SetTargetProperty(da, path);
     return da;
 }
Exemplo n.º 15
0
 public static DoubleAnimation CreateDoubleAnimation(double? from, double? to, Duration duration, string targetProperty, DependencyObject target, EasingFunctionBase easing = null)
 {
     var db = new DoubleAnimation();
     db.To = to;
     db.From = from;
     db.EasingFunction = easing;
     db.Duration = duration;
     Storyboard.SetTarget(db, target);
     Storyboard.SetTargetProperty(db, targetProperty);
     return db;
 }
Exemplo n.º 16
0
 /// <summary>
 /// 创建动画
 /// </summary> 
 public static DoubleAnimation CreatAnimation(DependencyObject obj, Storyboard storyboard, string path, int duration, double? toValue, EasingFunctionBase easing, bool isEnableDependency)
 {
     DoubleAnimation da = new DoubleAnimation();
     da.EasingFunction = easing;
     da.Duration = TimeSpan.FromMilliseconds(duration);
     da.To = toValue;
     da.EnableDependentAnimation = isEnableDependency;
     Storyboard.SetTarget(da, obj);
     Storyboard.SetTargetProperty(da, path);
     storyboard.Children.Add(da);
     return da;
 }
Exemplo n.º 17
0
        public static void AddAnimation(this Storyboard storyboard, DependencyObject element,
                                        int duration, double toValue, String propertyPath,
                                        EasingFunctionBase easingFunction = null)
        {
            DoubleAnimation timeline = new DoubleAnimation();
            timeline.To = toValue;
            timeline.Duration = TimeSpan.FromMilliseconds(duration);
            if (easingFunction != null)
                timeline.EasingFunction = easingFunction;

            storyboard.Children.Add(timeline);

            Storyboard.SetTarget(timeline, element);
            Storyboard.SetTargetProperty(timeline, propertyPath);
        }
        /// <summary>
        /// Fades the element in using a custom DoubleAnimation of the Opacity property.
        /// </summary>
        /// <param name="dob"></param>
        /// <param name="duration"></param>
        /// <param name="easingFunction"> </param>
        /// <returns></returns>
        public static async Task FadeInCustom(this DependencyObject dob, TimeSpan? duration = null, EasingFunctionBase easingFunction = null)
        {
            var fadeInStoryboard = new Storyboard();
            var fadeInAnimation = new DoubleAnimation();

            if (duration == null)
                duration = TimeSpan.FromSeconds(0.4);

            fadeInAnimation.Duration = duration.Value;
            fadeInAnimation.To = 1.0;
            fadeInAnimation.EasingFunction = easingFunction;

            Storyboard.SetTarget(fadeInAnimation, dob);
            Storyboard.SetTargetProperty(fadeInAnimation, "Opacity");
            fadeInStoryboard.Children.Add(fadeInAnimation);
            await fadeInStoryboard.BeginAsync();
        }
Exemplo n.º 19
0
        public static void Animate(this DependencyObject element, string propertyPath, 
                                int duration, double toValue,EasingFunctionBase easingFunction = null, EventHandler<Object> completed = null)
        {
            DoubleAnimation timeline = new DoubleAnimation();
            timeline.To = toValue;
            timeline.Duration = TimeSpan.FromMilliseconds(duration);
            if (easingFunction != null)
                timeline.EasingFunction = easingFunction;

            Storyboard sb = new Storyboard();
            if (completed != null)
                sb.Completed += completed;

            sb.Children.Add(timeline);
            Storyboard.SetTarget(sb, element);
            Storyboard.SetTargetProperty(sb, propertyPath);
            sb.Begin();
        }
Exemplo n.º 20
0
        public static void To(AnimatableUserControl target, string property, double to, int duration,
            EasingFunctionBase easing = null, EventHandler<object> callback = null)
        {
            var transform = (CompositeTransform) target.RenderTransform;
            double from = 0.0;
            switch (property)
            {
                case "X":
                    from = transform.TranslateX;
                    break;
                case "Y":
                    from = transform.TranslateY;
                    break;
                case "ScaleX":
                    from = transform.ScaleX;
                    break;
                case "ScaleY":
                    from = transform.ScaleY;
                    break;
                case "Alpha":
                    from = target.Opacity;
                    break;
                default:
                    throw new Exception("Unsupported proptery");

            }

            easing = easing ?? new QuadraticEase();
            var storyboard = new Storyboard();

            if (callback != null)
                storyboard.Completed += callback;

            var anim = new DoubleAnimation();
            anim.EnableDependentAnimation = true;
            anim.Duration = TimeSpan.FromMilliseconds(duration);
            anim.EasingFunction = easing;
            anim.From = from;
            anim.To = to;
            storyboard.Children.Add(anim);
            Storyboard.SetTargetProperty(anim, property);
            Storyboard.SetTarget(anim, target);
            storyboard.Begin();
        }
Exemplo n.º 21
0
        public static void AnimateY(UIElement pin, double fromY, double toY, int duration, EasingFunctionBase easingFunction)
        {
            pin.RenderTransform = new TranslateTransform();

            var sb = new Storyboard();
            var animation = new DoubleAnimation()
            {
                From = fromY,
                To = toY,
                Duration = new TimeSpan(0, 0, 0, 0, duration),
                EasingFunction = easingFunction
            };

            Storyboard.SetTargetProperty(animation, "(UIElement.RenderTransform).(TranslateTransform.Y)");
            Storyboard.SetTarget(animation, pin);

            sb.Children.Add(animation);
            sb.Begin();
        }
Exemplo n.º 22
0
        public static void FromTo(DependencyObject target, string property, double from, double to, int duration,
            EasingFunctionBase easing = null, EventHandler<object> callback = null)
        {
            easing = easing ?? new QuadraticEase();
            var storyboard = new Storyboard();

            if (callback != null)
                storyboard.Completed += callback;

            var anim = new DoubleAnimation();
            anim.EnableDependentAnimation = true;
            anim.Duration = TimeSpan.FromMilliseconds(duration);
            anim.EasingFunction = easing;
            anim.From = from;
            anim.To = to;
            storyboard.Children.Add(anim);
            Storyboard.SetTargetProperty(anim, property);
            Storyboard.SetTarget(anim, target);
            storyboard.Begin();
        }
        protected void AnimateLeftPanel(DependencyObject target, string property, double? from, double to, TimeSpan durationTimeSpane, EasingFunctionBase easingFunction)
        {
            var doubleAnimation = new DoubleAnimation
            {
                To = to,
                From = from,
                EasingFunction= easingFunction,
                EnableDependentAnimation = true,
            };

            var storyBoard = new Storyboard
            {
                BeginTime = TimeSpan.FromSeconds(0),
                Duration = doubleAnimation.Duration = new Duration(durationTimeSpane),
            };

            storyBoard.Children.Add(doubleAnimation);
            Storyboard.SetTarget(doubleAnimation, target);
            Storyboard.SetTargetProperty(doubleAnimation, property);
            storyBoard.Begin();
        }
Exemplo n.º 24
0
        public static Storyboard CreateDPAnimation(DependencyObject shape, string dp, Duration duration, double from, double to, bool loop = false, bool autoReverse = false, EasingFunctionBase easing = null, Storyboard st = null)
        {
            if(st == null)
                st = new Storyboard();

            var d = new DoubleAnimation {
                From = from,
                To = to,
                Duration = duration,
                EasingFunction = easing,
                AutoReverse = autoReverse
            };

            if(loop)
                d.RepeatBehavior = RepeatBehavior.Forever;

            st.Children.Add(d);
            Storyboard.SetTarget(d, shape);
            Storyboard.SetTargetProperty(d, dp);
            return st;
        }
Exemplo n.º 25
0
        public static Storyboard AddToStoryboard(this Storyboard storyboard, Double fromOffset, double toOffset,
                Duration animationDuration, DependencyObject target, string targetProperty, EasingFunctionBase easingFunction = null)
        {

            var animationSnap = new DoubleAnimation
            {
                
                EnableDependentAnimation = true,
                From = fromOffset,
                To = toOffset,
                Duration = animationDuration,
                EasingFunction = easingFunction ?? new ExponentialEase { EasingMode = EasingMode.EaseOut, Exponent = 8d }
            };

            storyboard.Children.Add(animationSnap);

            Storyboard.SetTarget(animationSnap, target);
            Storyboard.SetTargetProperty(animationSnap, targetProperty);

            return storyboard;
        }
Exemplo n.º 26
0
 /// <summary>
 /// Initializes a new instance of the EasingDoubleKeyFrame class with the specified Double value, key time, and easing function.
 /// </summary>
 /// <param name="value">The initial Double value.</param>
 /// <param name="keyTime">The initial key time.</param>
 /// <param name="easingFunction">The easing function.</param>
 public EasingDoubleKeyFrame(double value, KeyTime keyTime, EasingFunctionBase easingFunction)
     : base(value, keyTime)
 {
     EasingFunction = easingFunction;
 }
Exemplo n.º 27
0
        public void SetValues(double x, int zIndex, double r, double z, double s, Duration d, EasingFunctionBase ease, bool useAnimation) {
            if (useAnimation) {
                if (!isAnimating && Canvas.GetLeft(this) != x)
                    Canvas.SetLeft(this, this.x);

                rotationKeyFrame.Value = r;
                offestZKeyFrame.Value = z;
                scaleYKeyFrame.Value = s;
                scaleXKeyFrame.Value = s;
                xAnimation.To = x;

                if (duration != d) {
                    duration = d;
                    rotationKeyFrame.KeyTime = KeyTime.FromTimeSpan(d.TimeSpan);
                    offestZKeyFrame.KeyTime = KeyTime.FromTimeSpan(d.TimeSpan);
                    scaleYKeyFrame.KeyTime = KeyTime.FromTimeSpan(d.TimeSpan);
                    scaleXKeyFrame.KeyTime = KeyTime.FromTimeSpan(d.TimeSpan);
                    xAnimation.Duration = d;
                }

                if (easingFunction != ease) {
                    easingFunction = ease;
                    rotationKeyFrame.EasingFunction = ease;
                    offestZKeyFrame.EasingFunction = ease;
                    scaleYKeyFrame.EasingFunction = ease;
                    scaleXKeyFrame.EasingFunction = ease;
                    xAnimation.EasingFunction = ease;
                }

                isAnimating = true;
                Animation.Begin();
                Canvas.SetZIndex(this, zIndex);
            }

            this.x = x;
        }
        /// <summary>
        /// Fades the element out using a custom DoubleAnimation of the Opacity property.
        /// </summary>
        /// <param name="element"></param>
        /// <param name="duration"></param>
        /// <param name="easingFunction"> </param>
        /// <returns></returns>
        public static async Task FadeOutCustom(this UIElement element, TimeSpan? duration = null, EasingFunctionBase easingFunction = null)
        {
            CleanUpPreviousFadeStoryboard(element); 
            
            var fadeOutStoryboard = new Storyboard();
            var fadeOutAnimation = new DoubleAnimation();

            if (duration == null)
                duration = TimeSpan.FromSeconds(0.4);

            fadeOutAnimation.Duration = duration.Value;
            fadeOutAnimation.To = 0.0;
            fadeOutAnimation.EasingFunction = easingFunction;

            Storyboard.SetTarget(fadeOutAnimation, element);
            Storyboard.SetTargetProperty(fadeOutAnimation, "Opacity");
            fadeOutStoryboard.Children.Add(fadeOutAnimation);
            SetAttachedFadeStoryboard(element, fadeOutStoryboard);
            await fadeOutStoryboard.BeginAsync();
            element.Opacity = 0.0;
            fadeOutStoryboard.Stop();
        } 
        public static Storyboard GetStoryboard(this DependencyObject target, string property, double from, double to, double duration = 250, EasingFunctionBase easingFunction = null)
        {
            var storyboard = new Storyboard();
            var animation = new DoubleAnimation
            {
                From = from,
                To = to,
                Duration = TimeSpan.FromMilliseconds(duration),
                EasingFunction = easingFunction ?? new SineEase(),
                FillBehavior = FillBehavior.HoldEnd,
                EnableDependentAnimation = true
            };

            Storyboard.SetTarget(animation, target);
            Storyboard.SetTargetProperty(animation, property);

            storyboard.Children.Add(animation);
            storyboard.FillBehavior = FillBehavior.HoldEnd;
            //storyboard.Begin();

            return storyboard;
        }
 public static Task AnimateDoublePropertyAsync(this DependencyObject target, string property, double from, double to, double duration = 250, EasingFunctionBase easingFunction = null)
 {
     TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
     Storyboard storyboard = AnimateDoubleProperty(target, property, from, to, duration, easingFunction);
     storyboard.Completed += (sender, e) =>
     {
         tcs.SetResult(true);
     };
     return tcs.Task;
 }
        private void AnimatePanel(Duration duration, EasingFunctionBase ease, double to)
        {
            // Be sure not to run past the first or last items
            double newTo = Math.Max(_minimumPanelScroll, Math.Min(_maximumPanelScroll, to));
            if (to != newTo)
            {
                // Adjust the duration

                double originalDelta;
                double modifiedDelta;

                if (Orientation == Orientation.Vertical)
                {
                    originalDelta = Math.Abs(_panningTransform.Y - to);
                    modifiedDelta = Math.Abs(_panningTransform.Y - newTo);
                }
                else
                {
                    originalDelta = Math.Abs(_panningTransform.X - to);
                    modifiedDelta = Math.Abs(_panningTransform.X - newTo);
                }

                double factor = modifiedDelta / originalDelta;

                duration = new Duration(TimeSpan.FromMilliseconds(duration.TimeSpan.Milliseconds * factor));

                to = newTo;
            }

            double from;

            if (Orientation == Orientation.Vertical)
                from = _panningTransform.Y;
            else
                from = _panningTransform.X;

            StopAnimation();
            CompositionTarget.Rendering += AnimationPerFrameCallback;

            _panelAnimation.Duration = duration;
            _panelAnimation.EasingFunction = ease;
            _panelAnimation.From = from;
            _panelAnimation.To = to;

            _panelStoryboard.Begin();
            _panelStoryboard.SeekAlignedToLastTick(TimeSpan.Zero);

            _isAnimating = true;
        }