public async static Task StartClipAnimation(this FrameworkElement element, ClipAnimationDirection direction, float to,
            double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null)
        {
            CompositionScopedBatch batch = null;

            var visual = element.Visual();
            // After we get the Visual of the View, we need to SIZE it 'cause by design the
            // Size is (0,0). Without doing this, clipping will not work.
            visual.Size = new Vector2(element.ActualWidth.ToFloat(), element.ActualHeight.ToFloat());
            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            if (completed != null)
            {
                batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (delay > 0)
            {
                await Task.Delay(delay);
            }

            visual.Clip.StartAnimation($"{direction.ToString()}Inset", compositor.CreateScalarKeyFrameAnimation(to, duration, easing));

            if (batch != null)
            {
                batch.End();
            }
        }
        public async static Task StartOffsetAnimation(this UIElement element, AnimationAxis axis, float to = 0,
          double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null)
        {
            CompositionScopedBatch batch = null;

            var visual = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (delay > 0)
            {
                await Task.Delay(delay);
            }

            visual.StartAnimation($"Offset.{axis.ToString()}", compositor.CreateScalarKeyFrameAnimation(to, duration, easing));

            if (batch != null)
            {
                batch.End();
            }
        }
        public async static Task StartOffsetAnimation(this UIElement element, Vector2? to = null,
          double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null)
        {
            CompositionScopedBatch batch = null;

            var visual = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (to == null)
            {
                to = Vector2.Zero;
            }

            if (delay > 0)
            {
                await Task.Delay(delay);
            }

            visual.StartAnimation("Offset", compositor.CreateVector3KeyFrameAnimation(to.Value, duration, easing));

            if (batch != null)
            {
                batch.End();
            }
        }
        // CompositorExtensions
        public static ScalarKeyFrameAnimation CreateScalarKeyFrameAnimation(this Compositor compositor, float to, float? from, TimeSpan duration, TimeSpan? delay, CompositionEasingFunction ease)
        {
            var ani = compositor.CreateScalarKeyFrameAnimation();
            // Set duration and delay time
            ani.Duration = duration;
            if (delay.HasValue)
                ani.DelayTime = delay.Value;

            // Insert "to" and "from" keyframes
            ani.InsertKeyFrame(1, to, ease ?? compositor.CreateLinearEasingFunction());
            if (from.HasValue)
            {
                ani.InsertKeyFrame(0, from.Value);
            }
            return ani;
        }
Esempio n. 5
0
 // CompositionObject Vector3 animation
 public static void BeginVector3Animation(this CompositionObject compObj, String propertyPath, Vector3 to, Vector3?from, TimeSpan duration, TimeSpan?delay, CompositionEasingFunction ease)
 {
     compObj.StartAnimation(propertyPath, compObj.Compositor.CreateVector3KeyFrameAnimation(to, from, duration, delay, ease));
 }
Esempio n. 6
0
 static public void TranslateY(this UIElement element, double milliseconds, double start, double end, CompositionEasingFunction easingFunction = null)
 {
     ElementCompositionPreview.SetIsTranslationEnabled(element, true);
     element.StartAnimation("Translation.Y", CreateScalarAnimation(milliseconds, start, end, easingFunction));
 }
Esempio n. 7
0
        static public void Blur(this UIElement element, double milliseconds, double start, double end, CompositionEasingFunction easingFunction = null)
        {
            var brush = CreateBlurEffectBrush();

            element.SetBrush(brush);
            brush.StartAnimation("Blur.BlurAmount", CreateScalarAnimation(milliseconds, start, end, easingFunction));
        }
        public static Task StartOpacityAnimationAsync(this Visual visual, float?from = null, float to = 1.0f,
                                                      double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                      AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;
            var compositor = visual.Compositor;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;


            visual.StartAnimation("Opacity",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
        public static Task StartTranslationAnimationAsync(this UIElement element, AnimationAxis axis, float?from = null, float to = 0,
                                                          double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                          AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
            var visual     = element.Visual();
            var compositor = visual.Compositor;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            visual.StartAnimation($"Translation.{axis}", compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
 /// <summary>
 /// Inserts a KeyFrame in the KeyFrameAnimation by converting
 /// the given Expression to appropriate string
 /// </summary>
 /// <param name="normalizedProgressKey">The time the key frame should occur at, expressed as
 /// a percentage of the animation Duration. Allowed value is from 0.0 to 1.0.</param>
 /// <param name="expression">The expression which has to be converted to string to calculate the value of the KeyFrame.</param>
 /// <param name="easingFunction">The easing function to use when interpolating between frames.</param>
 public void InsertExpressionKeyFrame(float normalizedProgressKey, Expression <CompositionLambda <T> > expression,
                                      CompositionEasingFunction easingFunction = null)
 {
     Animation.InsertExpressionKeyFrame(normalizedProgressKey, expression, easingFunction);
 }
Esempio n. 11
0
        public static Task StartSizeAnimationAsync(this Visual visual, Vector2?from = null, Vector2?to = null,
                                                   double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                   AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            var compositor = visual.Compositor;

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            if (to == null)
            {
                to = Vector2.One;
            }

            visual.StartAnimation("Size",
                                  compositor.CreateVector2KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Esempio n. 12
0
        public static ScalarKeyFrameAnimation CreateScalarKeyFrameAnimation(this Compositor compositor, float to, double duration, CompositionEasingFunction easing)
        {
            var animation = compositor.CreateScalarKeyFrameAnimation();

            animation.Duration = TimeSpan.FromMilliseconds(duration);
            animation.InsertKeyFrame(1.0f, to, easing);

            return animation;
        }
Esempio n. 13
0
        public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector2 to, double duration, CompositionEasingFunction easing)
        {
            var animation = compositor.CreateVector3KeyFrameAnimation();

            animation.Duration = TimeSpan.FromMilliseconds(duration);
            animation.InsertKeyFrame(1.0f, new Vector3(to, 1.0f), easing);

            return animation;
        }
Esempio n. 14
0
 public static QuaternionKeyFrameAnimation AddKeyFrame(this QuaternionKeyFrameAnimation animation, float normalizedProgressKey, Quaternion value, CompositionEasingFunction ease = null)
 {
     animation.InsertKeyFrame(normalizedProgressKey, value, ease);
     return animation;
 }
        public static void StartTranslationAnimation(this UIElement element, AnimationAxis axis, float?from = null, float to = 0,
                                                     double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                     AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.StartAnimation($"Translation.{axis}", compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Esempio n. 16
0
 /// <summary>
 /// Adds a Vector3KeyFrame with X Y & Z components.
 /// </summary>
 /// <param name="animation"></param>
 /// <param name="normalizedProgressKey"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 /// <param name="z"></param>
 /// <param name="ease"></param>
 /// <returns></returns>
 public static Vector3KeyFrameAnimation AddKeyFrame(this Vector3KeyFrameAnimation animation, float normalizedProgressKey, float x, float y, float z, CompositionEasingFunction ease = null)
 {
     animation.InsertKeyFrame(normalizedProgressKey, new Vector3(x, y, z), ease);
     return animation;
 }
Esempio n. 17
0
 /// <summary>
 /// Adds a Vector3KeyFrame where the X & Y components are set to the input value and the Z component defaults to 1f.
 /// </summary>
 /// <param name="animation"></param>
 /// <param name="normalizedProgressKey"></param>
 /// <param name="value"></param>
 /// <param name="ease"></param>
 /// <returns></returns>
 public static Vector3KeyFrameAnimation AddScaleKeyFrame(this Vector3KeyFrameAnimation animation, float normalizedProgressKey, float value, CompositionEasingFunction ease = null)
 {
     animation.InsertKeyFrame(normalizedProgressKey, new Vector3(value, value, 1f), ease);
     return animation;
 }
Esempio n. 18
0
 public static T AddKeyFrame<T>(this T animation, float normalizedProgressKey, string expression, CompositionEasingFunction ease = null) where T : KeyFrameAnimation
 {
     animation.InsertExpressionKeyFrame(normalizedProgressKey, expression, ease);
     return animation;
 }
        public static Task StartClipAnimationAsync(this Visual visual, ClipAnimationDirection direction, float to,
                                                   double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                   AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            if (visual.Size.X.Equals(0) || visual.Size.Y.Equals(0))
            {
                throw new ArgumentException("The visual is not properly sized.");
            }

            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Esempio n. 20
0
        // Create Vector2 animation from compositor
        public static Vector2KeyFrameAnimation CreateVector2KeyFrameAnimation(this Compositor compositor, Vector2 to, Vector2?from, TimeSpan duration, TimeSpan?delay, CompositionEasingFunction ease)
        {
            // Set duration and delay time
            Vector2KeyFrameAnimation ani = compositor.CreateVector2KeyFrameAnimation();

            ani.Duration = duration;
            if (delay.HasValue)
            {
                ani.DelayTime = delay.Value;
            }

            // Insert "to" and "from" keyframes
            ani.InsertKeyFrame(1, to, ease ?? compositor.CreateLinearEasingFunction());
            if (from.HasValue)
            {
                ani.InsertKeyFrame(0, from.Value);
            }
            return(ani);
        }
        public static Task StartClipAnimationAsync(this FrameworkElement element, ClipAnimationDirection direction, float to,
                                                   double duration = 800, int delay = 0, CompositionEasingFunction easing = null,
                                                   AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch;

            var visual = element.Visual();

            // After we get the Visual of the View, we need to SIZE it 'cause by design the
            // Size is (0,0). Without doing this, clipping will not work.
            visual.Size = element.RenderSize.ToVector2();
            var compositor = visual.Compositor;

            if (visual.Clip == null)
            {
                var clip = compositor.CreateInsetClip();
                visual.Clip = clip;
            }

            var taskSource = new TaskCompletionSource <bool>();

            void Completed(object o, CompositionBatchCompletedEventArgs e)
            {
                batch.Completed -= Completed;
                taskSource.SetResult(true);
            }

            batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
            batch.Completed += Completed;

            visual.Clip.StartAnimation($"{direction}Inset",
                                       compositor.CreateScalarKeyFrameAnimation(null, to, duration, delay, easing, iterationBehavior));

            batch.End();

            return(taskSource.Task);
        }
Esempio n. 22
0
            PathKeyFrameAnimation CreatePathKeyFrameAnimation(float initialProgress, CompositionPath initialValue, CompositionEasingFunction initialEasingFunction)
            {
                var result = _c.CreatePathKeyFrameAnimation();

                result.Duration = TimeSpan.FromTicks(c_durationTicks);
                result.InsertKeyFrame(initialProgress, initialValue, initialEasingFunction);
                return(result);
            }
 public KeyFrame(float normalizedProgressKey, T value, CompositionEasingFunction easing = null)
 {
     Key    = normalizedProgressKey;
     Value  = value;
     Easing = easing;
 }
        private static KeyFrameAnimation CreateAnimationByType(Compositor compositor, VisualPropertyType type,
                                                               double duration = 800, double delay = 0, CompositionEasingFunction easing = null)
        {
            KeyFrameAnimation animation;

            switch (type)
            {
            case VisualPropertyType.Offset:
            case VisualPropertyType.Scale:
                animation = compositor.CreateVector3KeyFrameAnimation();
                break;

            case VisualPropertyType.Size:
                animation = compositor.CreateVector2KeyFrameAnimation();
                break;

            case VisualPropertyType.Opacity:
            case VisualPropertyType.RotationAngleInDegrees:
                animation = compositor.CreateScalarKeyFrameAnimation();
                break;

            default:
                return(null);
            }

            animation.InsertExpressionKeyFrame(1.0f, "this.FinalValue", easing);
            animation.Duration  = TimeSpan.FromMilliseconds(duration);
            animation.DelayTime = TimeSpan.FromMilliseconds(delay);
            animation.Target    = type.ToString();

            return(animation);
        }
        public static void StartOpacityAnimation(this Visual visual, float?from = null, float to = 1.0f,
                                                 double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                 AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            visual.StartAnimation("Opacity",
                                  compositor.CreateScalarKeyFrameAnimation(from, to, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
Esempio n. 26
0
        static public CompositionAnimation CreateVector3Animation(double milliseconds, Vector3 start, Vector3 end, CompositionEasingFunction easingFunction = null)
        {
            var animation = Window.Current.Compositor.CreateVector3KeyFrameAnimation();

            animation.InsertKeyFrame(0.0f, start);
            animation.InsertKeyFrame(1.0f, end);
            animation.Duration = TimeSpan.FromMilliseconds(milliseconds);
            return(animation);
        }
        /// <summary>
        /// Inserts a KeyFrame in the KeyFrameAnimation by converting
        /// the given Expression to appropriate string
        /// </summary>
        /// <typeparam name="T">Type of the Expression</typeparam>
        /// <param name="animation">KeyFrameAnimation</param>
        /// <param name="normalizedProgressKey">The time the key frame should occur at, expressed as
        /// a percentage of the animation Duration. Allowed value is from 0.0 to 1.0.</param>
        /// <param name="expression">The expression used to calculate the value of the key frame.</param>
        /// <param name="easingFunction">Easing Function (optional)</param>
        /// <returns>KeyFrameAnimation</returns>
        public static KeyFrameAnimation InsertExpressionKeyFrame <T>(this KeyFrameAnimation animation, float normalizedProgressKey,
                                                                     Expression <CompositionExpression <T> > expression, CompositionEasingFunction easingFunction = null)
        {
            var result = CompositionExpressionEngine.CreateCompositionExpression(expression);

            animation.InsertExpressionKeyFrame(normalizedProgressKey, result.Expression, easingFunction);
            animation.SetParameters(result.Parameters);

            return(animation);
        }
        public static void StartTranslationAnimation(this UIElement element, Vector3?from = null, Vector3?to = null,
                                                     double duration = 800, int delay = 0, CompositionEasingFunction easing = null, Action completed = null,
                                                     AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            CompositionScopedBatch batch = null;

            ElementCompositionPreview.SetIsTranslationEnabled(element, true);
            var visual     = element.Visual();
            var compositor = visual.Compositor;

            if (completed != null)
            {
                batch            = compositor.CreateScopedBatch(CompositionBatchTypes.Animation);
                batch.Completed += (s, e) => completed();
            }

            if (to == null)
            {
                to = Vector3.Zero;
            }

            visual.StartAnimation("Translation", compositor.CreateVector3KeyFrameAnimation(from, to.Value, duration, delay, easing, iterationBehavior));

            batch?.End();
        }
        public static void EnableImplicitAnimation(this Visual visual, VisualPropertyType typeToAnimate,
                                                   double duration = 800, double delay = 0, CompositionEasingFunction easing = null)
        {
            var compositor = visual.Compositor;

            var animationCollection = compositor.CreateImplicitAnimationCollection();

            foreach (var type in GetValues <VisualPropertyType>())
            {
                if (!typeToAnimate.HasFlag(type))
                {
                    continue;
                }

                var animation = CreateAnimationByType(compositor, type, duration, delay, easing);

                if (animation != null)
                {
                    animationCollection[type.ToString()] = animation;
                }
            }

            visual.ImplicitAnimations = animationCollection;
        }
Esempio n. 30
0
            ColorKeyFrameAnimation CreateColorKeyFrameAnimation(float initialProgress, Color initialValue, CompositionEasingFunction initialEasingFunction)
            {
                var result = _c.CreateColorKeyFrameAnimation();

                result.Duration = TimeSpan.FromTicks(c_durationTicks);
                result.InterpolationColorSpace = CompositionColorSpace.Rgb;
                result.InsertKeyFrame(initialProgress, initialValue, initialEasingFunction);
                return(result);
            }
Esempio n. 31
0
        static public CompositionAnimation CreateScalarAnimation(double milliseconds, double start, double end, CompositionEasingFunction easingFunction = null)
        {
            var animation = Window.Current.Compositor.CreateScalarKeyFrameAnimation();

            animation.InsertKeyFrame(0.0f, (float)start, easingFunction);
            animation.InsertKeyFrame(1.0f, (float)end, easingFunction);
            animation.Duration = TimeSpan.FromMilliseconds(milliseconds);
            return(animation);
        }
 public static void BeginVector3Animation(UIElement element, string propertyPath, Vector3 to, Vector3? from, TimeSpan duration, TimeSpan? delay, CompositionEasingFunction ease)
 {
     element.GetVisual().BeginVector3Animation(propertyPath, to, from, duration, delay, ease);
 }
Esempio n. 33
0
 static public void Fade(this UIElement element, double milliseconds, double start, double end, CompositionEasingFunction easingFunction = null)
 {
     element.StartAnimation(nameof(Visual.Opacity), CreateScalarAnimation(milliseconds, start, end, easingFunction));
 }
 public static void BeginVector3Animation(this CompositionObject compObj, string propertyPath, Vector3 to, Vector3? from, TimeSpan duration, TimeSpan? delay, CompositionEasingFunction ease)
 {
     compObj.StartAnimation(propertyPath,
         compObj.Compositor.CreateVector3KeyFrameAnimation(to, from, duration, delay, ease));
 }
Esempio n. 35
0
        static public void Scale(this FrameworkElement element, double milliseconds, double start, double end, CompositionEasingFunction easingFunction = null)
        {
            element.SetCenterPoint(element.ActualWidth / 2.0, element.ActualHeight / 2.0);
            var vectorStart = new Vector3((float)start, (float)start, 0);
            var vectorEnd   = new Vector3((float)end, (float)end, 0);

            element.StartAnimation(nameof(Visual.Scale), CreateVector3Animation(milliseconds, vectorStart, vectorEnd, easingFunction));
        }
Esempio n. 36
0
        public static Vector3KeyFrameAnimation CreateVector3KeyFrameAnimation(this Compositor compositor, Vector3?from, Vector3 to,
                                                                              double duration, double delay = 0, CompositionEasingFunction easing = null, AnimationIterationBehavior iterationBehavior = AnimationIterationBehavior.Count)
        {
            var animation = compositor.CreateVector3KeyFrameAnimation();

            animation.Duration  = TimeSpan.FromMilliseconds(duration);
            animation.DelayTime = TimeSpan.FromMilliseconds(delay);
            if (from.HasValue)
            {
                animation.InsertKeyFrame(0.0f, from.Value, easing);
            }
            animation.InsertKeyFrame(1.0f, to, easing);
            animation.IterationBehavior = iterationBehavior;

            return(animation);
        }
        /// <summary> Inserts a KeyFrame whose value is calculated using the specified ExpressionNode. </summary>
        /// <param name="normalizedProgressKey">The time the key frame should occur at, expressed as a percentage of the animation Duration. Allowed value is from 0.0 to 1.0.</param>
        /// <param name="expressionNode">The root ExpressionNode that represents the ExpressionAnimation.</param>
        /// <param name="easing">The easing function to use when interpolating between frames.</param>
        public static void InsertExpressionKeyFrame(this KeyFrameAnimation keyframeAnimation, float normalizedProgressKey, ExpressionNode expressionNode, CompositionEasingFunction easing = null)
        {
            keyframeAnimation.InsertExpressionKeyFrame(normalizedProgressKey, expressionNode.ToExpressionString(), easing);

            expressionNode.SetAllParameters(keyframeAnimation);
        }
Esempio n. 38
0
 // UIElement Vector3 animation
 public static void BeginVector3Animation(UIElement element, String propertyPath, Vector3 to, Vector3?from, TimeSpan duration, TimeSpan?delay, CompositionEasingFunction ease)
 {
     element.GetVisual().BeginVector3Animation(propertyPath, to, from, duration, delay, ease);
 }