Ejemplo n.º 1
0
        private void SetUpAnimationBehavior()
        {
            //setup Implicit Animation for BlurAmount change and Color Change.

            var implicitAnimations = _compositor.CreateImplicitAnimationCollection();

            //Define animations to animate blur and color change.
            ScalarKeyFrameAnimation blurAnimation = _compositor.CreateScalarKeyFrameAnimation();

            blurAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
            blurAnimation.Duration = TimeSpan.FromSeconds(1);
            blurAnimation.Target   = "Blur.BlurAmount";

            ColorKeyFrameAnimation tintAnimation = _compositor.CreateColorKeyFrameAnimation();

            tintAnimation.InsertExpressionKeyFrame(1.0f, "this.FinalValue");
            tintAnimation.Duration = TimeSpan.FromSeconds(1);
            tintAnimation.Target   = "Tint.Color";

            implicitAnimations["Blur.BlurAmount"] = blurAnimation;
            implicitAnimations["Tint.Color"]      = tintAnimation;

            //Associate implicit animations to property sets.
            _brush.Properties.ImplicitAnimations = implicitAnimations;
        }
Ejemplo n.º 2
0
        ColorKeyFrameAnimation GetColorKeyFrameAnimation(ColorKeyFrameAnimation obj)
        {
            if (GetExisting(obj, out var result))
            {
                return(result);
            }

            result = CacheAndInitializeKeyframeAnimation(obj, _c.CreateColorKeyFrameAnimation());
            foreach (var kf in obj.KeyFrames)
            {
                switch (kf.Type)
                {
                case KeyFrameAnimation <Wui.Color> .KeyFrameType.Expression:
                    var expressionKeyFrame = (KeyFrameAnimation <Wui.Color> .ExpressionKeyFrame)kf;
                    result.InsertExpressionKeyFrame(kf.Progress, expressionKeyFrame.Expression, GetCompositionEasingFunction(kf.Easing));
                    break;

                case KeyFrameAnimation <Wui.Color> .KeyFrameType.Value:
                    var valueKeyFrame = (KeyFrameAnimation <Wui.Color> .ValueKeyFrame)kf;
                    result.InsertKeyFrame(kf.Progress, valueKeyFrame.Value, GetCompositionEasingFunction(kf.Easing));
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            StartAnimationsAndFreeze(obj, result);
            return(result);
        }
Ejemplo n.º 3
0
    public BasicAnimationSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      var titleSafeArea = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;
      float minX = titleSafeArea.Left + 100;
      float maxX = titleSafeArea.Right - 100;

      // A from/to/by animation that animates a float value from minX to maxX over 2 seconds
      // using an easing function smooth movement.
      SingleFromToByAnimation xAnimation = new SingleFromToByAnimation
      {
        From = minX,
        To = maxX,
        Duration = TimeSpan.FromSeconds(2),
        EasingFunction = new CubicEase { Mode = EasingMode.EaseInOut },
      };

      // From-To-By animations do not loop by default.
      // Use an AnimationClip to turn the 2 second xAnimation into an animation that
      // oscillates forever.
      _oscillatingXAnimation = new AnimationClip<float>(xAnimation)
      {
        LoopBehavior = LoopBehavior.Oscillate,
        Duration = TimeSpan.MaxValue,
      };


      // A color key-frame animation.
      ColorKeyFrameAnimation colorAnimation = new ColorKeyFrameAnimation
      {
        //EnableInterpolation = true,  // Interpolation is enabled by default.
      };
      // Add the key-frames.
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0.0), Color.Red));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0.5), Color.Orange));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1.0), Color.Yellow));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1.5), Color.Green));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(2.0), Color.Blue));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(2.5), Color.Indigo));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(3.0), Color.Violet));

      // The last-key frame defines the length of the animation (3.5 seconds). This is a
      // "loop frame", which means the value is the same as in the first key to create a looping
      // animation.
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(3.5), Color.Red));

      // If the key-frames are not sorted, call Sort().
      //colorAnimation.KeyFrames.Sort();

      // Use an AnimationClip to turn the 3.5 second colorAnimation into an animation that
      // loops forever.
      _loopedColorAnimation = new AnimationClip<Color>(colorAnimation)
      {
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };

      StartAnimations();
    }
        private void StartColorAnimation(Color endValue)
        {
            // Create and start a color animation from begin to end values
            ColorKeyFrameAnimation animation = _compositor.CreateColorKeyFrameAnimation();

            animation.InsertKeyFrame(1.0f, endValue);
            animation.Duration = new TimeSpan(0, 0, ANIMATION_DURATION);
            _shadow.StartAnimation("Color", animation);
        }
Ejemplo n.º 5
0
        private void DialogDismissedHandler(IUICommand command)
        {
            // Start a scoped batch so we can register to completion event and hide the destination layer
            _scopeBatch = _compositor.CreateScopedBatch(CompositionBatchTypes.Animation);

            // Start the hide animation to fade out the destination effect
            ScalarKeyFrameAnimation hideAnimation = _compositor.CreateScalarKeyFrameAnimation();

            hideAnimation.InsertKeyFrame(0f, 1f);
            hideAnimation.InsertKeyFrame(1.0f, 0f);
            hideAnimation.Duration = TimeSpan.FromMilliseconds(1000);
            _destinationSprite.StartAnimation("Opacity", hideAnimation);

            // Use whichever effect is currently selected
            ComboBoxItem item = EffectSelection.SelectedValue as ComboBoxItem;

            switch ((EffectTypes)item.Tag)
            {
            case EffectTypes.Mask:
            {
                CompositionSurfaceBrush  brush          = ((CompositionEffectBrush)_destinationSprite.Brush).GetSourceParameter("SecondSource") as CompositionSurfaceBrush;
                Vector2KeyFrameAnimation scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
                scaleAnimation.InsertKeyFrame(1f, new Vector2(2.0f, 2.0f));
                scaleAnimation.Duration = TimeSpan.FromMilliseconds(1000);
                brush.StartAnimation("Scale", scaleAnimation);
                break;
            }

            case EffectTypes.VividLight:
            {
                CompositionEffectBrush brush         = (CompositionEffectBrush)_destinationSprite.Brush;
                ColorKeyFrameAnimation coloAnimation = _compositor.CreateColorKeyFrameAnimation();
                coloAnimation.InsertKeyFrame(1f, Color.FromArgb(255, 100, 100, 100));
                coloAnimation.Duration = TimeSpan.FromMilliseconds(1500);
                brush.StartAnimation("Base.Color", coloAnimation);
                break;
            }

            case EffectTypes.Hue:
            {
                CompositionEffectBrush  brush           = (CompositionEffectBrush)_destinationSprite.Brush;
                ScalarKeyFrameAnimation rotateAnimation = _compositor.CreateScalarKeyFrameAnimation();
                rotateAnimation.InsertKeyFrame(1f, 0f);
                rotateAnimation.Duration = TimeSpan.FromMilliseconds(1500);
                brush.StartAnimation("Hue.Angle", rotateAnimation);
                break;
            }

            default:
                break;
            }

            //Scoped batch completed event
            _scopeBatch.Completed += ScopeBatch_Completed;
            _scopeBatch.End();
        }
Ejemplo n.º 6
0
 protected override void OnDisconnected()
 {
     if (CompositionBrush != null)
     {
         CompositionBrush.Dispose();
         CompositionBrush = null;
         ColorImplicitAnimations.Dispose();
         ColorImplicitAnimations = null;
         ColorAnimation.Dispose();
         ColorAnimation = null;
     }
 }
Ejemplo n.º 7
0
        XElement FromColorKeyFrameAnimation(ColorKeyFrameAnimation obj)
        {
            return(new XElement(GetCompositionObjectName(obj), GetContents()));

            IEnumerable <XObject> GetContents()
            {
                foreach (var item in GetCompositionObjectContents(obj))
                {
                    yield return(item);
                }
            }
        }
        protected override void OnDisconnected()
        {
            if (CompositionBrush != null)
            {
                IsConnected         = false;
                TransitionCompleted = null;

                ColorAnimation.Dispose();
                ColorAnimation = null;

                CompositionBrush.Dispose();
                CompositionBrush = null;
            }
        }
Ejemplo n.º 9
0
        // when the first visual is clicked, it switches the brush on the visual from warm to cool colors and animates those color stops
        private void OnClick(object sender, RoutedEventArgs e)
        {
            if (_isAnimationOn == false)
            {
                // animate the first stop of the first visual
                _stop1Anim = _compositor.CreateColorKeyFrameAnimation();
                _stop1Anim.InsertKeyFrame(0, _warmColor1);
                _stop1Anim.InsertKeyFrame(.5f, _warmColor3);
                _stop1Anim.InsertKeyFrame(1, _warmColor1);
                _stop1Anim.Duration          = TimeSpan.FromSeconds(4);
                _stop1Anim.IterationBehavior = AnimationIterationBehavior.Forever;

                _MBGradientStop1.StartAnimation("Color", _stop1Anim);

                // animate the second stop of the first visual
                _stop2Anim = _compositor.CreateColorKeyFrameAnimation();
                _stop2Anim.InsertKeyFrame(0, _warmColor3);
                _stop2Anim.InsertKeyFrame(1, _warmColor2);
                _stop2Anim.Duration          = TimeSpan.FromSeconds(2);
                _stop2Anim.IterationBehavior = AnimationIterationBehavior.Forever;

                _MBGradientStop2.StartAnimation("Color", _stop2Anim);

                _isAnimationOn = true;
            }
            else
            {
                // animate the first stop of the first visual
                _stop1Anim = _compositor.CreateColorKeyFrameAnimation();
                _stop1Anim.InsertKeyFrame(0, _coolColor1);
                _stop1Anim.InsertKeyFrame(.5f, _coolColor3);
                _stop1Anim.InsertKeyFrame(1, _coolColor1);
                _stop1Anim.Duration          = TimeSpan.FromSeconds(4);
                _stop1Anim.IterationBehavior = AnimationIterationBehavior.Forever;

                _MBGradientStop1.StartAnimation("Color", _stop1Anim);

                // animate the second stop of the first visual
                _stop2Anim.InsertKeyFrame(0, _coolColor3);
                _stop2Anim.InsertKeyFrame(1, _coolColor2);
                _stop2Anim.Duration          = TimeSpan.FromSeconds(2);
                _stop2Anim.IterationBehavior = AnimationIterationBehavior.Forever;

                _MBGradientStop2.StartAnimation("Color", _stop2Anim);

                _isAnimationOn = false;
            }
        }
Ejemplo n.º 10
0
        override protected void OnConnected()
        {
            Compositor compositor = Window.Current.Compositor;

            // CompositionCapabilities: Are HostBackdrop Effects supported?
            bool usingFallback = !CompositionCapabilities.GetForCurrentView().AreEffectsFast();

            if (usingFallback)
            {
                // If Effects are not supported, use Fallback Solid Color
                CompositionBrush = compositor.CreateColorBrush(FallbackColor);
                return;
            }

            // Define Effect graph
            var graphicsEffect = new BlendEffect
            {
                Mode       = BlendEffectMode.Overlay,
                Background = new CompositionEffectSourceParameter("Backdrop"),
                Foreground = new ColorSourceEffect()
                {
                    Name  = "OverlayColor",
                    Color = OverlayColor,
                },
            };

            // Create EffectFactory and EffectBrush
            CompositionEffectFactory effectFactory = compositor.CreateEffectFactory(graphicsEffect, new[] { "OverlayColor.Color" });
            CompositionEffectBrush   effectBrush   = effectFactory.CreateBrush();

            // Create HostBackdropBrush, a kind of BackdropBrush that provides blurred backdrop content
            CompositionBackdropBrush hostBrush = compositor.CreateHostBackdropBrush();

            effectBrush.SetSourceParameter("Backdrop", hostBrush);

            // Set EffectBrush as the brush that XamlCompBrushBase paints onto Xaml UIElement
            CompositionBrush = effectBrush;

            // When the Window loses focus, animate HostBackdrop to FallbackColor
            Window.Current.CoreWindow.Activated += CoreWindow_Activated;

            // Configure color animation to for state change
            _stateChangeAnimation = compositor.CreateColorKeyFrameAnimation();
            _stateChangeAnimation.InsertKeyFrame(0, OverlayColor);
            _stateChangeAnimation.InsertKeyFrame(1, FallbackColor);
            _stateChangeAnimation.Duration = TimeSpan.FromSeconds(1);
        }
Ejemplo n.º 11
0
        protected override void OnConnected()
        {
            if (CompositionBrush == null)
            {
                CompositionBrush = Window.Current.Compositor.CreateColorBrush(Color);

                ColorAnimation = Window.Current.Compositor.CreateColorKeyFrameAnimation();
                ColorAnimation.InsertExpressionKeyFrame(1f, "this.FinalValue");
                ColorAnimation.Duration = Duration;
                ColorAnimation.Target   = "Color";

                ColorImplicitAnimations          = Window.Current.Compositor.CreateImplicitAnimationCollection();
                ColorImplicitAnimations["Color"] = ColorAnimation;

                CompositionBrush.ImplicitAnimations = ColorImplicitAnimations;
            }
        }
 protected override void OnConnected()
 {
     if (CompositionBrush == null)
     {
         IsConnected    = true;
         ColorAnimation = Compositor.CreateColorKeyFrameAnimation();
         ColorAnimation.InsertExpressionKeyFrame(0f, "this.StartingValue");
         ColorAnimation.InsertExpressionKeyFrame(1f, "Color");
         ColorAnimation.Duration = Duration;
         if (BaseBrush is SolidColorBrush colorBrush)
         {
             CompositionBrush = Compositor.CreateColorBrush(colorBrush.Color);
         }
         else
         {
             CompositionBrush = Compositor.CreateColorBrush();
         }
     }
 }
Ejemplo n.º 13
0
        private void ChangeBlurColor(Color newColor)
        {
            lastBlurColor = blurColor;
            blurColor     = newColor;

            if (IsGlassOn)
            {
                effectBrush.Properties.InsertColor("NewColor.Color", newColor);

                ColorKeyFrameAnimation colorAnimation = compositor.CreateColorKeyFrameAnimation();
                colorAnimation.InsertKeyFrame(0.0f, lastBlurColor);
                colorAnimation.InsertKeyFrame(1.0f, blurColor);
                colorAnimation.Duration = TimeSpan.FromSeconds(5);
                effectBrush.StartAnimation("NewColor.Color", colorAnimation);
            }
            else
            {
                TurnOnGlass();
            }
        }
Ejemplo n.º 14
0
        private void StartAnimation_Click(object sender, RoutedEventArgs e)
        {
            ColorKeyFrameAnimation colorAnimation = compositor.CreateColorKeyFrameAnimation();

            colorAnimation.InsertKeyFrame(1.0f, Colors.Purple);
            colorAnimation.InterpolationColorSpace = CompositionColorSpace.Hsl;
            colorAnimation.Duration = TimeSpan.FromSeconds(5);

            //targetVisual.StartAnimation("SolidColorBrush.Color", colorAnimation);
            targetVisual.Brush.StartAnimation("Color", colorAnimation);



            //Vector3KeyFrameAnimation offsetAnimation = compositor.CreateVector3KeyFrameAnimation();
            //offsetAnimation.InsertKeyFrame(1.0f, new Vector3(50, 50, 0));
            //offsetAnimation.Target = "offset";


            //CompositionAnimationGroup group = compositor.CreateAnimationGroup();
            //group.Add(offsetAnimation);
            //targetVisual.StartAnimationGroup(group);
        }
Ejemplo n.º 15
0
        // Main circle
        private void CreateTopCircleButton()
        {
            CompositionRoundedRectangleGeometry circleGeometry = compositor.CreateRoundedRectangleGeometry();

            circleGeometry.Size         = new Vector2(40, 40);
            circleGeometry.CornerRadius = new Vector2(20, 20);
            CompositionSpriteShape compositionSpriteShape = compositor.CreateSpriteShape(circleGeometry);

            circleGradientBrush = compositor.CreateRadialGradientBrush();

            // Color animation
            ColorStop1 = compositor.CreateColorGradientStop(1, Colors.White);
            circleGradientBrush.ColorStops.Add(ColorStop1);
            color1Animation          = compositor.CreateColorKeyFrameAnimation();
            color1Animation.Duration = TimeSpan.FromSeconds(0.5);
            color1Animation.InsertKeyFrame(1.0f, Color.FromArgb(255, 247, 211, 156));
            color1Animation.InsertKeyFrame(0.0f, Colors.White);
            color2Animation          = compositor.CreateColorKeyFrameAnimation();
            color2Animation.Duration = TimeSpan.FromSeconds(0.5);
            color2Animation.InsertKeyFrame(1.0f, Colors.White);
            color2Animation.InsertKeyFrame(0.0f, Color.FromArgb(255, 247, 211, 156));

            compositionSpriteShape.FillBrush = circleGradientBrush;
            compositionSpriteShape.Offset    = new Vector2(5, 5);

            shapeVisualCircle      = compositor.CreateShapeVisual();
            shapeVisualCircle.Size = new Vector2(50, 50);
            shapeVisualCircle.Shapes.Add(compositionSpriteShape);
            shapeVisualCircle.Offset = new Vector3(sliderMargins.X, 0, 0);

            // Return animation
            _leftoffsetAnimation = compositor.CreateVector3KeyFrameAnimation();
            _leftoffsetAnimation.InsertKeyFrame(1.0f, new Vector3(0 + sliderMargins.X, 0, 0));
            _leftoffsetAnimation.Duration = TimeSpan.FromSeconds(0.2f);
            _rightoffsetAnimation         = compositor.CreateVector3KeyFrameAnimation();
            _rightoffsetAnimation.InsertKeyFrame(1.0f, new Vector3(100 - sliderMargins.Y, 0, 0));
            _rightoffsetAnimation.Duration = TimeSpan.FromSeconds(0.2f);
        }
Ejemplo n.º 16
0
        void DrawShape()
        {
            // handle border shape geometry
            using var geo          = _compositor.CreateRoundedRectangleGeometry();
            geo.Size               = _size;
            geo.CornerRadius       = new Vector2(2);
            _shapecolorbrush       = _compositor.CreateColorBrush(Color.FromArgb(30, 0xff, 0xff, 0xff));
            _spriteShape           = _compositor.CreateSpriteShape(geo);
            _spriteShape.FillBrush = _shapecolorbrush;
            _shape.Shapes.Add(_spriteShape);

            // Handle Visibility on composition
            _visible          = _compositor.CreateColorKeyFrameAnimation();
            _visible.Duration = TimeSpan.FromMilliseconds(75);
            _visible.InsertKeyFrame(0f, Color.FromArgb(30, 0xff, 0xff, 0xff));
            _visible.InsertKeyFrame(0.5f, Color.FromArgb(0x40, 0x40, 0x40, 0x40));
            _visible.InsertKeyFrame(1f, Color.FromArgb(0xcc, 0x26, 0x2a, 0x2f));

            _hidden          = _compositor.CreateColorKeyFrameAnimation();
            _hidden.Duration = TimeSpan.FromMilliseconds(75);
            _hidden.InsertKeyFrame(0f, Color.FromArgb(0xcc, 0x26, 0x2a, 0x2f));
            _hidden.InsertKeyFrame(0.5f, Color.FromArgb(0x40, 0x40, 0x40, 0x40));
            _hidden.InsertKeyFrame(1f, Color.FromArgb(30, 0xff, 0xff, 0xff));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Initializes the Composition elements
        /// </summary>
        private void InitComposition()
        {
            // Compositor
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;
            // CompositionGenerator
            _generator = CompositionGeneratorFactory.GetCompositionGenerator(_compositor);
            
            // Fade Out Animation
            _fadeOutAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _fadeOutAnimation.InsertKeyFrame(1f, 0);
            _fadeOutAnimation.Duration = TransitionDuration;
            // Fade In Animation
            _fadeInAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _fadeInAnimation.InsertKeyFrame(1f, 1);
            _fadeInAnimation.Duration = TransitionDuration;
            // Color Animation
            _colorAnimation = _compositor.CreateColorKeyFrameAnimation();
            _colorAnimation.Duration = TransitionDuration;
            // Offset Animation
            _offsetAnimation = _compositor.CreateVector3KeyFrameAnimation();
            _offsetAnimation.Target = "Offset";
            _offsetAnimation.Duration = TransitionDuration;
            _offsetAnimation.InsertKeyFrame(1f, Vector3.Zero);
            // Alignment animations
            _alignXAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _alignXAnimation.Duration = AlignmentTransitionDuration;
            _alignYAnimation = _compositor.CreateScalarKeyFrameAnimation();
            _alignYAnimation.Duration = AlignmentTransitionDuration;

            // ZoomIn Animation Group
            _scaleAnimation = _compositor.CreateVector3KeyFrameAnimation();
            _scaleAnimation.Target = "Scale";
            _scaleAnimation.InsertKeyFrame(1f, Vector3.One);
            _scaleAnimation.Duration = TransitionDuration;
            _zoomInAnimationGroup = _compositor.CreateAnimationGroup();
            _zoomInAnimationGroup.Add(_scaleAnimation);
            _zoomInAnimationGroup.Add(_offsetAnimation);

            // Visuals
            _rootContainer = _compositor.CreateContainerVisual();
            _frameLayer = _compositor.CreateLayerVisual();
            _frameBackgroundVisual = _compositor.CreateSpriteVisual();
            _frameContentVisual = _compositor.CreateSpriteVisual();
            _placeholderContentVisual = _compositor.CreateSpriteVisual();
            _placeholderBackgroundVisual = _compositor.CreateSpriteVisual();
            _nextVisualContent = _compositor.CreateSpriteVisual();

            _frameLayer.Children.InsertAtTop(_frameBackgroundVisual);
            _frameLayer.Children.InsertAtTop(_frameContentVisual);
            _frameLayer.Children.InsertAtTop(_placeholderBackgroundVisual);
            _frameLayer.Children.InsertAtTop(_placeholderContentVisual);
            _frameLayer.Children.InsertAtTop(_nextVisualContent);

            // Placeholder content
            _placeholderContentMask = _generator.CreateGeometrySurface(PlaceholderSize, GetPlaceHolderGeometry(),
                PlaceholderColor, PlaceholderBackground);
            _placeholderContentBrush = _compositor.CreateSurfaceBrush(_placeholderContentMask.Surface);
            _placeholderContentVisual.Brush = _placeholderContentBrush;
            // Placeholder background
            _placeholderBackgroundVisual.Brush = _compositor.CreateColorBrush(PlaceholderBackground);

            // By default placeholder visual will not be visible
            HidePlaceholder();

            // Shadow visual
            _shadowVisual = _compositor.CreateSpriteVisual();

            _rootContainer.Children.InsertAtBottom(_shadowVisual);
            _rootContainer.Children.InsertAtTop(_frameLayer);

            _frameBackgroundVisual.Brush = _compositor.CreateColorBrush(FrameBackground);

            // Create the effect to create the opacity mask
            var layerEffect = new CompositeEffect
            {
                // CanvasComposite.DestinationIn - Intersection of source and mask. 
                // Equation: O = MA * S
                // where O - Output pixel, MA - Mask Alpha, S - Source pixel.
                Mode = CanvasComposite.DestinationIn,
                Sources =
                        {
                            new CompositionEffectSourceParameter("source"),
                            new CompositionEffectSourceParameter("mask")
                        }
            };

            var layerEffectFactory = _compositor.CreateEffectFactory(layerEffect);
            _layerEffectBrush = layerEffectFactory.CreateBrush();

            // The mask for the imageFrame
            _frameLayerMask = _generator.CreateMaskSurface(new Size(0, 0), null);
            _layerEffectBrush.SetSourceParameter("mask", _compositor.CreateSurfaceBrush(_frameLayerMask.Surface));
            // Apply the mask effect to the frameLayer
            _frameLayer.Effect = _layerEffectBrush;

            ElementCompositionPreview.SetElementChildVisual(this, _rootContainer);
        }
        public void TraitsTest()
        {
            var animationEx = new ColorKeyFrameAnimation();

            Assert.AreEqual(ColorTraits.Instance, animationEx.Traits);
        }
Ejemplo n.º 19
0
    public AnimatableObjectSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

      // ----- Create three named AnimatableSprite instances.
      _animatableSpriteA = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
      {
        Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
        Color = Color.Red,
      };

      _animatableSpriteB = new AnimatableSprite("SpriteB", SpriteBatch, Logo)
      {
        Position = new Vector2(bounds.Center.X, bounds.Center.Y),
        Color = Color.Green,
      };

      _animatableSpriteC = new AnimatableSprite("SpriteC", SpriteBatch, Logo)
      {
        Position = new Vector2(bounds.Center.X, 3.0f * bounds.Center.Y / 2.0f),
        Color = Color.Blue,
      };

      // Create a looping color key-frame animation.
      ColorKeyFrameAnimation colorAnimation = new ColorKeyFrameAnimation
      {
        TargetProperty = "Color",
        EnableInterpolation = true,
      };
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0.0), Color.Red));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0.5), Color.Orange));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1.0), Color.Yellow));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1.5), Color.Green));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(2.0), Color.Blue));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(2.5), Color.Indigo));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(3.0), Color.Violet));
      colorAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(3.5), Color.Red));
      AnimationClip<Color> loopedColorAnimation = new AnimationClip<Color>(colorAnimation)
      {
        TargetObject = "SpriteB",
        LoopBehavior = LoopBehavior.Cycle,
        Duration = TimeSpan.MaxValue,
      };

      // Create a oscillating from/to animation for the position.
      Vector2FromToByAnimation vector2Animation = new Vector2FromToByAnimation
      {
        TargetProperty = "Position",
        From = new Vector2(bounds.Left + 100, _animatableSpriteC.Position.Y),
        To = new Vector2(bounds.Right - 100, _animatableSpriteC.Position.Y),
        Duration = TimeSpan.FromSeconds(2),
        EasingFunction = new HermiteEase { Mode = EasingMode.EaseInOut },
      };
      AnimationClip<Vector2> oscillatingVector2Animation = new AnimationClip<Vector2>(vector2Animation)
      {
        LoopBehavior = LoopBehavior.Oscillate,
        Duration = TimeSpan.MaxValue,
      };

      // Create a timeline group that contains both animations.
      TimelineGroup spriteCAnimation = new TimelineGroup { TargetObject = "SpriteC", };
      spriteCAnimation.Add(loopedColorAnimation);
      spriteCAnimation.Add(oscillatingVector2Animation);

      // Create a timeline group that contains the animations for SpriteB and SpriteC.
      TimelineGroup allSpriteAnimations = new TimelineGroup();
      allSpriteAnimations.Add(loopedColorAnimation);
      allSpriteAnimations.Add(spriteCAnimation);

      // There are several ways to apply animations to animatable objects and properties:
      //
      // Method 1: Apply to an IAnimatableProperty directly.
      // We either have direct access to a IAnimatableProperty or we can ask the IAnimatableObject
      // to give us a named IAnimatableProperty. 
      // For example:
      //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteB.GetAnimatableProperty<Color>("Color"));
      //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteC.GetAnimatableProperty<Color>("Color"));
      //     AnimationService.StartAnimation(oscillatingLeftRightAnimation, _animatableSpriteC.GetAnimatableProperty<Vector2>("Position"));
      // In this case, the "TargetObject" and the "TargetProperty" values of the timelines/animations do not matter. 
      //
      // Method 2: Apply to an IAnimatableObject directly.
      // For example: 
      //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteB);
      //     AnimationService.StartAnimation(spriteCAnimation, _animatableSpriteC);
      // The animation service checks the TargetProperty value of the animations to see which
      // IAnimatableProperty of the IAnimatableObjects should be animated by the animation.
      // The "TargetObject" values of the timelines are ignored.
      // 
      // Method 3: Apply a timeline to a collection of IAnimatableObjects.
      // For example: 
      var animationController = AnimationService.StartAnimation(allSpriteAnimations, new[] { _animatableSpriteA, _animatableSpriteB, _animatableSpriteC });
      // The "TargetObject" and "TargetProperty" values are used to check which objects and
      // properties must be animated by which animation.
      // combinedSpriteAnimation is a "tree" of timelines:
      // 
      //                                  Type                         TargetObject   TargetProperty
      // --------------------------------------------------------------------------------------------------
      // allSpriteAnimations              TimelineGroup                -              -
      //   loopedColorAnimation           AnimationClip<Color>         "SpriteB"      -
      //     colorAnimation               ColorKeyFrameAnimation       -              "Color"
      //   spriteCAnimation               TimelineGroup                "SpriteC"      -
      //     loopedColorAnimation         AnimationClip<Color>         "SpriteB"      - 
      //       colorAnimation             ColorKeyFrameAnimation       -             "Color"
      //     oscillatingVector2Animation  AnimationClip<Vector2>       -             -
      //       vector2Animation           FromToByAnimation            -             "Position"
      //
      // No animation specifies "SpriteA" as its TargetObject, therefore no animation
      // are applied to SpriteA.
      //
      // The first loopedColorAnimation is applied to "SpriteB". And the contained 
      // colorAnimation is applied to the Color property of SpriteB.
      //
      // The spriteCAnimation is applied to "SpriteC". Therefore, all "children" of 
      // spriteCAnimation are also applied to this object! The TargetObject property of
      // the second loopedColorAnimation is ignored! The second loopedColorAnimation is 
      // applied to SpriteC!

      animationController.UpdateAndApply();
    }
Ejemplo n.º 20
0
        protected override void OnConnected()
        {
            if (DesignMode.DesignModeEnabled)
            {
                return;
            }
            compositor = ElementCompositionPreview.GetElementVisual(Window.Current.Content as UIElement).Compositor;
            var tintOpacity = Convert.ToSingle(TintOpacity);

            if (tintOpacity < 0f)
            {
                tintOpacity = 0f;
            }
            if (tintOpacity > 1f)
            {
                tintOpacity = 1f;
            }
            var arithmeticEffect = new ArithmeticCompositeEffect()
            {
                Name           = "arithmetic",
                MultiplyAmount = 0,
                Source1Amount  = 1f - tintOpacity,
                Source2Amount  = tintOpacity,
                Source1        = new ArithmeticCompositeEffect()
                {
                    MultiplyAmount = 0f,
                    Source1Amount  = 0.95f,
                    Source2Amount  = 0.05f,
                    Source1        = new GaussianBlurEffect()
                    {
                        Name         = "blur",
                        BlurAmount   = Convert.ToSingle(BlurAmount),
                        BorderMode   = EffectBorderMode.Hard,
                        Optimization = EffectOptimization.Balanced,
                        Source       = new CompositionEffectSourceParameter("source"),
                    },
                    Source2 = new BorderEffect()
                    {
                        Source  = new CompositionEffectSourceParameter("image"),
                        ExtendX = CanvasEdgeBehavior.Wrap,
                        ExtendY = CanvasEdgeBehavior.Wrap,
                    }
                },
                Source2 = new ColorSourceEffect()
                {
                    Name  = "tintcolor",
                    Color = TintColor
                }
            };

            Brush = compositor.CreateEffectFactory(arithmeticEffect, new[] { "arithmetic.Source1Amount", "arithmetic.Source2Amount", "tintcolor.Color" }).CreateBrush();

            var imagesurface = LoadedImageSurface.StartLoadFromUri(new Uri("ms-appx:///FluentDesignSystem/Sketch/SketchTexture.jpg"));

            var imagebrush = compositor.CreateSurfaceBrush(imagesurface);

            imagebrush.Stretch = CompositionStretch.None;
            Brush.SetSourceParameter("image", imagebrush);

            switch (BackgroundSource)
            {
            case AcrylicBackgroundSource.Backdrop:
                Brush.SetSourceParameter("source", compositor.CreateBackdropBrush());
                break;

            case AcrylicBackgroundSource.Hostbackdrop:
                Brush.SetSourceParameter("source", compositor.CreateHostBackdropBrush());
                break;
            }

            CompositionBrush = Brush;

            var line = compositor.CreateLinearEasingFunction();

            TintOpacityFillAnimation = compositor.CreateScalarKeyFrameAnimation();
            TintOpacityFillAnimation.InsertExpressionKeyFrame(0f, "TintOpacity", line);
            TintOpacityFillAnimation.InsertKeyFrame(1f, 1f, line);
            TintOpacityFillAnimation.Duration = TimeSpan.FromSeconds(0.1d);
            TintOpacityFillAnimation.Target   = "arithmetic.Source2Amount";

            HostOpacityZeroAnimation = compositor.CreateScalarKeyFrameAnimation();
            HostOpacityZeroAnimation.InsertExpressionKeyFrame(0f, "1f - TintOpacity", line);
            HostOpacityZeroAnimation.InsertKeyFrame(1f, 0f, line);
            HostOpacityZeroAnimation.Duration = TimeSpan.FromSeconds(0.1d);
            HostOpacityZeroAnimation.Target   = "arithmetic.Source1Amount";

            TintToFallBackAnimation = compositor.CreateColorKeyFrameAnimation();
            TintToFallBackAnimation.InsertKeyFrame(0f, TintColor, line);
            TintToFallBackAnimation.InsertKeyFrame(1f, FallbackColor, line);
            TintToFallBackAnimation.Duration = TimeSpan.FromSeconds(0.1d);
            TintToFallBackAnimation.Target   = "tintcolor.Color";

            //Window.Current.Activated += Current_Activated;
            //Window.Current.VisibilityChanged += Current_VisibilityChanged;
            CoreWindow.GetForCurrentThread().Activated         += AcrylicBrush_Activated;
            CoreWindow.GetForCurrentThread().VisibilityChanged += AcrylicBrush_VisibilityChanged;
        }
Ejemplo n.º 21
0
    public BlendedAnimationSample(Microsoft.Xna.Framework.Game game)
      : base(game)
    {
      Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

      // Create the animatable object.
      _animatableSprite = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
      {
        Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
        Color = Color.Red,
      };

      Vector2FromToByAnimation slowLeftRightAnimation = new Vector2FromToByAnimation
      {
        TargetProperty = "Position",
        From = new Vector2(bounds.Left + 100, bounds.Top + 200),
        To = new Vector2(bounds.Right - 100, bounds.Top + 200),
        Duration = TimeSpan.FromSeconds(4),
        EasingFunction = new HermiteEase { Mode = EasingMode.EaseInOut },
      };

      ColorKeyFrameAnimation redGreenAnimation = new ColorKeyFrameAnimation
      {
        TargetProperty = "Color",
        EnableInterpolation = true,
      };
      redGreenAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0), Color.Red));
      redGreenAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(4), Color.Green));

      TimelineGroup animationA = new TimelineGroup
      {
        slowLeftRightAnimation,
        redGreenAnimation,
      };

      Vector2FromToByAnimation fastLeftRightAnimation = new Vector2FromToByAnimation
      {
        TargetProperty = "Position",
        From = new Vector2(bounds.Left + 100, bounds.Bottom - 200),
        To = new Vector2(bounds.Right - 100, bounds.Bottom - 200),
        Duration = TimeSpan.FromSeconds(1),
        EasingFunction = new HermiteEase { Mode = EasingMode.EaseInOut },
      };

      ColorKeyFrameAnimation blackWhiteAnimation = new ColorKeyFrameAnimation
      {
        TargetProperty = "Color",
        EnableInterpolation = true,
      };
      blackWhiteAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(0), Color.Black));
      blackWhiteAnimation.KeyFrames.Add(new KeyFrame<Color>(TimeSpan.FromSeconds(1), Color.White));

      TimelineGroup animationB = new TimelineGroup
      {
        fastLeftRightAnimation,
        blackWhiteAnimation,
      };

      // Create a BlendGroup that blends animationA and animationB. 
      // The BlendGroup uses the TargetProperty values of the contained animations to 
      // to match the animations that should be blended:
      //   slowLeftRightAnimation with fastLeftRightAnimation
      //   redGreenAnimation with blackWhiteAnimation
      _blendedAnimation = new BlendGroup
      {
        LoopBehavior = LoopBehavior.Oscillate,
        Duration = TimeSpan.MaxValue,
      };
      _blendedAnimation.Add(animationA, 1);
      _blendedAnimation.Add(animationB, 0);
      _blendedAnimation.SynchronizeDurations();

      // Start blended animation.
      AnimationService.StartAnimation(_blendedAnimation, _animatableSprite).UpdateAndApply();
    }
Ejemplo n.º 22
0
        private async void ThumbnailList_ItemClick(object sender, ItemClickEventArgs e)
        {
            Thumbnail thumbnail = (Thumbnail)e.ClickedItem;

            // If we're in the middle of an animation, cancel it now
            if (_scopeBatch != null)
            {
                CleanupScopeBatch();
            }

            // We're starting our transition, show the destination sprite
            _destinationSprite.IsVisible = true;

            // Animate from transparent to fully opaque
            ScalarKeyFrameAnimation showAnimation = _compositor.CreateScalarKeyFrameAnimation();

            showAnimation.InsertKeyFrame(0f, 0f);
            showAnimation.InsertKeyFrame(1f, 1f);
            showAnimation.Duration = TimeSpan.FromMilliseconds(1500);
            _destinationSprite.StartAnimation("Opacity", showAnimation);

            // Use whichever effect is currently selected
            ComboBoxItem item = EffectSelection.SelectedValue as ComboBoxItem;

            switch ((EffectTypes)item.Tag)
            {
            case EffectTypes.Mask:
            {
                CompositionSurfaceBrush  brush          = ((CompositionEffectBrush)_destinationSprite.Brush).GetSourceParameter("SecondSource") as CompositionSurfaceBrush;
                Vector2KeyFrameAnimation scaleAnimation = _compositor.CreateVector2KeyFrameAnimation();
                scaleAnimation.InsertKeyFrame(0f, new Vector2(1.25f, 1.25f));
                scaleAnimation.InsertKeyFrame(1f, new Vector2(0f, 0f));
                scaleAnimation.Duration = TimeSpan.FromMilliseconds(2000);
                brush.StartAnimation("Scale", scaleAnimation);
                break;
            }

            case EffectTypes.VividLight:
            {
                CompositionEffectBrush brush         = (CompositionEffectBrush)_destinationSprite.Brush;
                ColorKeyFrameAnimation coloAnimation = _compositor.CreateColorKeyFrameAnimation();
                coloAnimation.InsertKeyFrame(0f, Color.FromArgb(255, 255, 255, 255));
                coloAnimation.InsertKeyFrame(0f, Color.FromArgb(255, 30, 30, 30));
                coloAnimation.Duration = TimeSpan.FromMilliseconds(4000);
                brush.StartAnimation("Base.Color", coloAnimation);
                break;
            }

            case EffectTypes.Hue:
            {
                CompositionEffectBrush  brush           = (CompositionEffectBrush)_destinationSprite.Brush;
                ScalarKeyFrameAnimation rotateAnimation = _compositor.CreateScalarKeyFrameAnimation();
                rotateAnimation.InsertKeyFrame(0f, 0f);
                rotateAnimation.InsertKeyFrame(1f, (float)Math.PI);
                rotateAnimation.Duration = TimeSpan.FromMilliseconds(4000);
                brush.StartAnimation("Hue.Angle", rotateAnimation);
                break;
            }

            case EffectTypes.RainbowBlur:
            {
                CompositionEffectBrush brush          = (CompositionEffectBrush)_destinationSprite.Brush;
                ColorKeyFrameAnimation colorAnimation = _compositor.CreateColorKeyFrameAnimation();
                colorAnimation.InsertKeyFrame(0, Colors.Red);
                colorAnimation.InsertKeyFrame(.16f, Colors.Orange);
                colorAnimation.InsertKeyFrame(.32f, Colors.Yellow);
                colorAnimation.InsertKeyFrame(.48f, Colors.Green);
                colorAnimation.InsertKeyFrame(.64f, Colors.Blue);
                colorAnimation.InsertKeyFrame(.80f, Colors.Purple);
                colorAnimation.InsertKeyFrame(1, Colors.Red);
                colorAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
                colorAnimation.Duration          = TimeSpan.FromMilliseconds(5000);
                brush.StartAnimation("Base.Color", colorAnimation);
                break;
            }

            default:
                break;
            }

            // Create the dialog
            var messageDialog = new MessageDialog(thumbnail.Name);

            messageDialog.Commands.Add(new UICommand("Close", new UICommandInvokedHandler(DialogDismissedHandler)));

            // Show the message dialog
            await messageDialog.ShowAsync();
        }
Ejemplo n.º 23
0
 public void TraitsTest()
 {
     var animationEx = new ColorKeyFrameAnimation();
       Assert.AreEqual(ColorTraits.Instance, animationEx.Traits);
 }
Ejemplo n.º 24
0
 public static ColorKeyFrameAnimation AddKeyFrame(this ColorKeyFrameAnimation animation, float normalizedProgressKey, Color value, CompositionEasingFunction ease = null)
 {
     animation.InsertKeyFrame(normalizedProgressKey, value, ease);
     return animation;
 }
        public BlendedAnimationSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

            // Create the animatable object.
            _animatableSprite = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
            {
                Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
                Color    = Color.Red,
            };

            Vector2FromToByAnimation slowLeftRightAnimation = new Vector2FromToByAnimation
            {
                TargetProperty = "Position",
                From           = new Vector2(bounds.Left + 100, bounds.Top + 200),
                To             = new Vector2(bounds.Right - 100, bounds.Top + 200),
                Duration       = TimeSpan.FromSeconds(4),
                EasingFunction = new HermiteEase {
                    Mode = EasingMode.EaseInOut
                },
            };

            ColorKeyFrameAnimation redGreenAnimation = new ColorKeyFrameAnimation
            {
                TargetProperty      = "Color",
                EnableInterpolation = true,
            };

            redGreenAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0), Color.Red));
            redGreenAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(4), Color.Green));

            TimelineGroup animationA = new TimelineGroup
            {
                slowLeftRightAnimation,
                redGreenAnimation,
            };

            Vector2FromToByAnimation fastLeftRightAnimation = new Vector2FromToByAnimation
            {
                TargetProperty = "Position",
                From           = new Vector2(bounds.Left + 100, bounds.Bottom - 200),
                To             = new Vector2(bounds.Right - 100, bounds.Bottom - 200),
                Duration       = TimeSpan.FromSeconds(1),
                EasingFunction = new HermiteEase {
                    Mode = EasingMode.EaseInOut
                },
            };

            ColorKeyFrameAnimation blackWhiteAnimation = new ColorKeyFrameAnimation
            {
                TargetProperty      = "Color",
                EnableInterpolation = true,
            };

            blackWhiteAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0), Color.Black));
            blackWhiteAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1), Color.White));

            TimelineGroup animationB = new TimelineGroup
            {
                fastLeftRightAnimation,
                blackWhiteAnimation,
            };

            // Create a BlendGroup that blends animationA and animationB.
            // The BlendGroup uses the TargetProperty values of the contained animations to
            // to match the animations that should be blended:
            //   slowLeftRightAnimation with fastLeftRightAnimation
            //   redGreenAnimation with blackWhiteAnimation
            _blendedAnimation = new BlendGroup
            {
                LoopBehavior = LoopBehavior.Oscillate,
                Duration     = TimeSpan.MaxValue,
            };
            _blendedAnimation.Add(animationA, 1);
            _blendedAnimation.Add(animationB, 0);
            _blendedAnimation.SynchronizeDurations();

            // Start blended animation.
            AnimationService.StartAnimation(_blendedAnimation, _animatableSprite).UpdateAndApply();
        }
Ejemplo n.º 26
0
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            // Update Rectangle widths to match text width
            Rectangle1.Width = TextBlock1.ActualWidth;
            Rectangle2.Width = TextBlock2.ActualWidth;
            Rectangle3.Width = TextBlock3.ActualWidth;
            Rectangle4.Width = TextBlock4.ActualWidth;

            // Create the four visuals that will be used to hold the LinearGradient brush
            _vis1 = _compositor.CreateSpriteVisual();
            _vis2 = _compositor.CreateSpriteVisual();
            _vis3 = _compositor.CreateSpriteVisual();
            _vis4 = _compositor.CreateSpriteVisual();

            // Create the linearGradient brush and set the first set of colors to the gradientStops of the Brush
            _linearGradientBrush  = _compositor.CreateLinearGradientBrush();
            _gradientStop1        = _compositor.CreateColorGradientStop();
            _gradientStop1.Offset = 0;
            _gradientStop1.Color  = warmColor1;
            _gradientStop2        = _compositor.CreateColorGradientStop();
            _gradientStop2.Offset = 1;
            _gradientStop2.Color  = warmColor2;
            _linearGradientBrush.ColorStops.Add(_gradientStop1);
            _linearGradientBrush.ColorStops.Add(_gradientStop2);

            // Paint visuals with brushes and set their locations
            _vis1.Brush = _linearGradientBrush;
            _vis1.Scale = new Vector3(0, 1, 0);
            _vis1.Size  = new Vector2((float)Rectangle1.ActualWidth, (float)Rectangle1.ActualHeight);

            _vis2.Brush = _linearGradientBrush;
            _vis2.Scale = new Vector3(0, 1, 0);
            _vis2.Size  = new Vector2((float)Rectangle2.ActualWidth, (float)Rectangle2.ActualHeight);

            _vis3.Brush = _linearGradientBrush;
            _vis3.Scale = new Vector3(0, 1, 0);
            _vis3.Size  = new Vector2((float)Rectangle3.ActualWidth, (float)Rectangle3.ActualHeight);

            _vis4.Brush = _linearGradientBrush;
            _vis4.Scale = new Vector3(0, 1, 0);
            _vis4.Size  = new Vector2((float)Rectangle4.ActualWidth, (float)Rectangle4.ActualHeight);

            // Parent visuals to XAML rectangles
            ElementCompositionPreview.SetElementChildVisual(Rectangle1, _vis1);
            ElementCompositionPreview.SetElementChildVisual(Rectangle2, _vis2);
            ElementCompositionPreview.SetElementChildVisual(Rectangle3, _vis3);
            ElementCompositionPreview.SetElementChildVisual(Rectangle4, _vis4);

            //create the scale & offset animation

            Vector3KeyFrameAnimation offsetAnim_r1 = _compositor.CreateVector3KeyFrameAnimation();

            offsetAnim_r1.InsertKeyFrame(1, new Vector3((float)Rectangle1.ActualWidth, (float)Rectangle1.ActualHeight, 0));
            offsetAnim_r1.Duration = TimeSpan.FromSeconds(6);

            // Instantiate animation
            //TODO update to animate gradient stops instaed of scale
            _scaleAnim = _compositor.CreateVector3KeyFrameAnimation();
            _scaleAnim.InsertKeyFrame(0, new Vector3(0, 1, 0));
            _scaleAnim.InsertKeyFrame(.5f, new Vector3(1, 1, 0));
            _scaleAnim.InsertKeyFrame(1, new Vector3(0, 1, 0));
            _scaleAnim.Duration = TimeSpan.FromSeconds(2);

            // animation of color stops
            _colorAnimGradientStop1 = _compositor.CreateColorKeyFrameAnimation();
            _colorAnimGradientStop1.InsertKeyFrame(.5f, Colors.Honeydew);
            _colorAnimGradientStop1.Duration = TimeSpan.FromSeconds(4);

            _colorAnimGradientStop2 = _compositor.CreateColorKeyFrameAnimation();
            _colorAnimGradientStop2.InsertKeyFrame(.5f, Colors.DeepPink);
            _colorAnimGradientStop2.Duration = TimeSpan.FromSeconds(4);

            _offsetAnim          = _compositor.CreateScalarKeyFrameAnimation();
            _offsetAnim.Duration = TimeSpan.FromSeconds(1);

            // when the buttons of text are pressed, the brush will change colors. Below is the set up for animation
            _changeb1stop1 = _compositor.CreateColorKeyFrameAnimation();
            _changeb1stop1.InsertKeyFrame(.5f, Colors.LightSkyBlue);
            _changeb1stop1.Duration = TimeSpan.FromSeconds(2);

            _changeb1stop2 = _compositor.CreateColorKeyFrameAnimation();
            _changeb1stop2.InsertKeyFrame(.5f, Colors.Teal);
            _changeb1stop2.Duration = TimeSpan.FromSeconds(2);
        }
Ejemplo n.º 27
0
        public BasicAnimationSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            var   titleSafeArea = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;
            float minX          = titleSafeArea.Left + 100;
            float maxX          = titleSafeArea.Right - 100;

            // A from/to/by animation that animates a float value from minX to maxX over 2 seconds
            // using an easing function smooth movement.
            SingleFromToByAnimation xAnimation = new SingleFromToByAnimation
            {
                From           = minX,
                To             = maxX,
                Duration       = TimeSpan.FromSeconds(2),
                EasingFunction = new CubicEase {
                    Mode = EasingMode.EaseInOut
                },
            };

            // From-To-By animations do not loop by default.
            // Use an AnimationClip to turn the 2 second xAnimation into an animation that
            // oscillates forever.
            _oscillatingXAnimation = new AnimationClip <float>(xAnimation)
            {
                LoopBehavior = LoopBehavior.Oscillate,
                Duration     = TimeSpan.MaxValue,
            };


            // A color key-frame animation.
            ColorKeyFrameAnimation colorAnimation = new ColorKeyFrameAnimation
            {
                //EnableInterpolation = true,  // Interpolation is enabled by default.
            };

            // Add the key-frames.
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0.0), Color.Red));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0.5), Color.Orange));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1.0), Color.Yellow));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1.5), Color.Green));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(2.0), Color.Blue));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(2.5), Color.Indigo));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(3.0), Color.Violet));

            // The last-key frame defines the length of the animation (3.5 seconds). This is a
            // "loop frame", which means the value is the same as in the first key to create a looping
            // animation.
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(3.5), Color.Red));

            // If the key-frames are not sorted, call Sort().
            //colorAnimation.KeyFrames.Sort();

            // Use an AnimationClip to turn the 3.5 second colorAnimation into an animation that
            // loops forever.
            _loopedColorAnimation = new AnimationClip <Color>(colorAnimation)
            {
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };

            StartAnimations();
        }
Ejemplo n.º 28
0
 internal ColorKeyFrameTransitionAnimationFluentContext([NotNull] StoryBoardFluentContext parentStoryBoard, [NotNull] ColorKeyFrameAnimation animation, [NotNull] String targetProperty) : base(parentStoryBoard, animation, targetProperty)
 {
 }
        public static CompositionAnimation GetAnimation <TKeyFrame>(
            CompositionObject target,
            string property,
            TimeSpan?delay,
            TimeSpan duration,
            RepeatOption repeat,
            ArraySegment <TKeyFrame> keyFrames)
            where TKeyFrame : struct, IKeyFrameInfo
        {
            KeyFrameAnimation animation;

            if (typeof(T) == typeof(bool))
            {
                BooleanKeyFrameAnimation boolAnimation = target.Compositor.CreateBooleanKeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(boolAnimation, duration))
                    {
                        continue;
                    }

                    boolAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <bool>());
                }

                animation = boolAnimation;
            }
            else if (typeof(T) == typeof(float))
            {
                ScalarKeyFrameAnimation scalarAnimation = target.Compositor.CreateScalarKeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(scalarAnimation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <float>());
                    }
                    else
                    {
                        scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <float>(), easingFunction);
                    }
                }

                animation = scalarAnimation;
            }
            else if (typeof(T) == typeof(double))
            {
                ScalarKeyFrameAnimation scalarAnimation = target.Compositor.CreateScalarKeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(scalarAnimation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), (float)keyFrame.GetValueAs <double>());
                    }
                    else
                    {
                        scalarAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), (float)keyFrame.GetValueAs <double>(), easingFunction);
                    }
                }

                animation = scalarAnimation;
            }
            else if (typeof(T) == typeof(Vector2))
            {
                Vector2KeyFrameAnimation vector2Animation = target.Compositor.CreateVector2KeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(vector2Animation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        vector2Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector2>());
                    }
                    else
                    {
                        vector2Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector2>(), easingFunction);
                    }
                }

                animation = vector2Animation;
            }
            else if (typeof(T) == typeof(Vector3))
            {
                Vector3KeyFrameAnimation vector3Animation = target.Compositor.CreateVector3KeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(vector3Animation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        vector3Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector3>());
                    }
                    else
                    {
                        vector3Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector3>(), easingFunction);
                    }
                }

                animation = vector3Animation;
            }
            else if (typeof(T) == typeof(Vector4))
            {
                Vector4KeyFrameAnimation vector4Animation = target.Compositor.CreateVector4KeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(vector4Animation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        vector4Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector4>());
                    }
                    else
                    {
                        vector4Animation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Vector4>(), easingFunction);
                    }
                }

                animation = vector4Animation;
            }
            else if (typeof(T) == typeof(Color))
            {
                ColorKeyFrameAnimation colorAnimation = target.Compositor.CreateColorKeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(colorAnimation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        colorAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Color>());
                    }
                    else
                    {
                        colorAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Color>(), easingFunction);
                    }
                }

                animation = colorAnimation;
            }
            else if (typeof(T) == typeof(Quaternion))
            {
                QuaternionKeyFrameAnimation quaternionAnimation = target.Compositor.CreateQuaternionKeyFrameAnimation();

                foreach (var keyFrame in keyFrames)
                {
                    if (keyFrame.TryInsertExpressionKeyFrame(quaternionAnimation, duration))
                    {
                        continue;
                    }

                    CompositionEasingFunction?easingFunction = target.Compositor.TryCreateEasingFunction(keyFrame.EasingType, keyFrame.EasingMode);

                    if (easingFunction is null)
                    {
                        quaternionAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Quaternion>());
                    }
                    else
                    {
                        quaternionAnimation.InsertKeyFrame(keyFrame.GetNormalizedProgress(duration), keyFrame.GetValueAs <Quaternion>(), easingFunction);
                    }
                }

                animation = quaternionAnimation;
            }
            else
            {
                throw new InvalidOperationException("Invalid animation type");
            }

            animation.Duration = duration;

            if (delay.HasValue)
            {
                animation.DelayTime = delay !.Value;
            }

            animation.Target = property;
            (animation.IterationBehavior, animation.IterationCount) = repeat.ToBehaviorAndCount();

            return(animation);
        }
Ejemplo n.º 30
0
        private void CreateImageAndLights(Vector2 sizeLightBounds)
        {
            //
            // Image and effect setup
            //

            // Create the effect graph.  We will combine the desaturated image with two diffuse lights
            IGraphicsEffect graphicsEffect = new CompositeEffect()
            {
                Mode    = Microsoft.Graphics.Canvas.CanvasComposite.Add,
                Sources =
                {
                    new SaturationEffect()
                    {
                        Saturation = 0,
                        Source     = new CompositionEffectSourceParameter("ImageSource")
                    },

                    new PointDiffuseEffect()
                    {
                        Name          = "Light1",
                        DiffuseAmount = 1f,
                    },

                    new PointDiffuseEffect()
                    {
                        Name          = "Light2",
                        DiffuseAmount = 1f,
                    },
                }
            };

            // Create the effect factory, we're going to animate the light positions and colors
            CompositionEffectFactory effectFactory = _compositor.CreateEffectFactory(graphicsEffect,
                                                                                     new[] { "Light1.LightPosition", "Light1.LightColor",
                                                                                             "Light2.LightPosition", "Light2.LightColor", });

            // Create the effect brush and bind the normal map
            CompositionEffectBrush brush = effectFactory.CreateBrush();

            // Update the CompositionImage to use the custom effect brush
            ArtistImage.Brush = brush;


            //
            //  Animation setup
            //

            // Setup the first light's position, top and to the left in general
            LinearEasingFunction     linear            = _compositor.CreateLinearEasingFunction();
            Vector3KeyFrameAnimation positionAnimation = _compositor.CreateVector3KeyFrameAnimation();

            positionAnimation.InsertKeyFrame(0f, new Vector3(0f, 0f, 300f), linear);
            positionAnimation.InsertKeyFrame(.33f, new Vector3(sizeLightBounds.X * .5f, sizeLightBounds.Y * .5f, 100f), linear);
            positionAnimation.InsertKeyFrame(.66f, new Vector3(sizeLightBounds.X * .25f, sizeLightBounds.Y * .95f, 100f), linear);
            positionAnimation.InsertKeyFrame(1f, new Vector3(0f, 0f, 300f), linear);
            positionAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            positionAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light1.LightPosition", positionAnimation);


            // Setup the first light's color animation, cycling through some brighter tones
            ColorKeyFrameAnimation colorAnimation = _compositor.CreateColorKeyFrameAnimation();

            colorAnimation.InsertKeyFrame(0f, Colors.MidnightBlue);
            colorAnimation.InsertKeyFrame(.2f, Colors.Indigo);
            colorAnimation.InsertKeyFrame(.3f, Colors.RoyalBlue);
            colorAnimation.InsertKeyFrame(.5f, Colors.CornflowerBlue);
            colorAnimation.InsertKeyFrame(.6f, Colors.Thistle);
            colorAnimation.InsertKeyFrame(.8f, Colors.CornflowerBlue);
            colorAnimation.InsertKeyFrame(.9f, Colors.RoyalBlue);
            colorAnimation.InsertKeyFrame(1f, Colors.Indigo);
            colorAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            colorAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light1.LightColor", colorAnimation);


            // Setup the second light's position, down and to the right in general
            positionAnimation = _compositor.CreateVector3KeyFrameAnimation();
            positionAnimation.InsertKeyFrame(0f, new Vector3(sizeLightBounds.X, sizeLightBounds.Y, 200f), linear);
            positionAnimation.InsertKeyFrame(.33f, new Vector3(sizeLightBounds.X * .7f, sizeLightBounds.Y * .9f, 300f), linear);
            positionAnimation.InsertKeyFrame(.66f, new Vector3(sizeLightBounds.X * .95f, sizeLightBounds.Y * .95f, 100f), linear);
            positionAnimation.InsertKeyFrame(1f, new Vector3(sizeLightBounds.X, sizeLightBounds.Y, 200f), linear);
            positionAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            positionAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light2.LightPosition", positionAnimation);

            // Setup the second light's color animation, cycling through some darker tones
            colorAnimation = _compositor.CreateColorKeyFrameAnimation();
            colorAnimation.InsertKeyFrame(0f, Colors.Firebrick);
            colorAnimation.InsertKeyFrame(.2f, Colors.DarkGoldenrod);
            colorAnimation.InsertKeyFrame(.3f, Colors.Chartreuse);
            colorAnimation.InsertKeyFrame(.5f, Colors.ForestGreen);
            colorAnimation.InsertKeyFrame(.6f, Colors.DarkTurquoise);
            colorAnimation.InsertKeyFrame(.8f, Colors.MidnightBlue);
            colorAnimation.InsertKeyFrame(.9f, Colors.DarkViolet);
            colorAnimation.InsertKeyFrame(1f, Colors.DarkSlateGray);
            colorAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            colorAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light2.LightColor", colorAnimation);
        }
Ejemplo n.º 31
0
        public AnimatableObjectSample(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            Rectangle bounds = GraphicsService.GraphicsDevice.Viewport.TitleSafeArea;

            // ----- Create three named AnimatableSprite instances.
            _animatableSpriteA = new AnimatableSprite("SpriteA", SpriteBatch, Logo)
            {
                Position = new Vector2(bounds.Center.X, bounds.Center.Y / 2.0f),
                Color    = Color.Red,
            };

            _animatableSpriteB = new AnimatableSprite("SpriteB", SpriteBatch, Logo)
            {
                Position = new Vector2(bounds.Center.X, bounds.Center.Y),
                Color    = Color.Green,
            };

            _animatableSpriteC = new AnimatableSprite("SpriteC", SpriteBatch, Logo)
            {
                Position = new Vector2(bounds.Center.X, 3.0f * bounds.Center.Y / 2.0f),
                Color    = Color.Blue,
            };

            // Create a looping color key-frame animation.
            ColorKeyFrameAnimation colorAnimation = new ColorKeyFrameAnimation
            {
                TargetProperty      = "Color",
                EnableInterpolation = true,
            };

            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0.0), Color.Red));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(0.5), Color.Orange));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1.0), Color.Yellow));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(1.5), Color.Green));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(2.0), Color.Blue));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(2.5), Color.Indigo));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(3.0), Color.Violet));
            colorAnimation.KeyFrames.Add(new KeyFrame <Color>(TimeSpan.FromSeconds(3.5), Color.Red));
            AnimationClip <Color> loopedColorAnimation = new AnimationClip <Color>(colorAnimation)
            {
                TargetObject = "SpriteB",
                LoopBehavior = LoopBehavior.Cycle,
                Duration     = TimeSpan.MaxValue,
            };

            // Create a oscillating from/to animation for the position.
            Vector2FromToByAnimation vector2Animation = new Vector2FromToByAnimation
            {
                TargetProperty = "Position",
                From           = new Vector2(bounds.Left + 100, _animatableSpriteC.Position.Y),
                To             = new Vector2(bounds.Right - 100, _animatableSpriteC.Position.Y),
                Duration       = TimeSpan.FromSeconds(2),
                EasingFunction = new HermiteEase {
                    Mode = EasingMode.EaseInOut
                },
            };
            AnimationClip <Vector2> oscillatingVector2Animation = new AnimationClip <Vector2>(vector2Animation)
            {
                LoopBehavior = LoopBehavior.Oscillate,
                Duration     = TimeSpan.MaxValue,
            };

            // Create a timeline group that contains both animations.
            TimelineGroup spriteCAnimation = new TimelineGroup {
                TargetObject = "SpriteC",
            };

            spriteCAnimation.Add(loopedColorAnimation);
            spriteCAnimation.Add(oscillatingVector2Animation);

            // Create a timeline group that contains the animations for SpriteB and SpriteC.
            TimelineGroup allSpriteAnimations = new TimelineGroup();

            allSpriteAnimations.Add(loopedColorAnimation);
            allSpriteAnimations.Add(spriteCAnimation);

            // There are several ways to apply animations to animatable objects and properties:
            //
            // Method 1: Apply to an IAnimatableProperty directly.
            // We either have direct access to a IAnimatableProperty or we can ask the IAnimatableObject
            // to give us a named IAnimatableProperty.
            // For example:
            //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteB.GetAnimatableProperty<Color>("Color"));
            //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteC.GetAnimatableProperty<Color>("Color"));
            //     AnimationService.StartAnimation(oscillatingLeftRightAnimation, _animatableSpriteC.GetAnimatableProperty<Vector2>("Position"));
            // In this case, the "TargetObject" and the "TargetProperty" values of the timelines/animations do not matter.
            //
            // Method 2: Apply to an IAnimatableObject directly.
            // For example:
            //     AnimationService.StartAnimation(loopedColorAnimation, _animatableSpriteB);
            //     AnimationService.StartAnimation(spriteCAnimation, _animatableSpriteC);
            // The animation service checks the TargetProperty value of the animations to see which
            // IAnimatabelProperty of the IAnimtableObjects should be animated by the animation.
            // The "TargetObject" values of the timelines are ignored.
            //
            // Method 3: Apply a timeline to a collection of IAnimatableObjects.
            // For example:
            var animationController = AnimationService.StartAnimation(allSpriteAnimations, new[] { _animatableSpriteA, _animatableSpriteB, _animatableSpriteC });

            // The "TargetObject" and "TargetProperty" values are used to check which objects and
            // properties must be animated by which animation.
            // combinedSpriteAnimation is a "tree" of timelines:
            //
            //                                  Type                         TargetObject   TargetProperty
            // --------------------------------------------------------------------------------------------------
            // allSpriteAnimations              TimelineGroup                -              -
            //   loopedColorAnimation           AnimationClip<Color>         "SpriteB"      -
            //     colorAnimation               ColorKeyFrameAnimation       -              "Color"
            //   spriteCAnimation               TimelineGroup                "SpriteC"      -
            //     loopedColorAnimation         AnimationClip<Color>         "SpriteB"      -
            //       colorAnimation             ColorKeyFrameAnimation       -             "Color"
            //     oscillatingVector2Animation  AnimationClip<Vector2>       -             -
            //       vector2Animation           FromToByAnimation            -             "Position"
            //
            // No animation specifies "SpriteA" as its TargetObject, therefore no animation
            // are applied to SpriteA.
            //
            // The first loopedColorAnimation is applied to "SpriteB". And the contained
            // colorAnimation is applied to the Color property of SpriteB.
            //
            // The spriteCAnimation is applied to "SpriteC". Therefore, all "children" of
            // spriteCAnimation are also applied to this object! The TargetObject property of
            // the second loopedColorAnimation is ignored! The second loopedColorAnimation is
            // applied to SpriteC!

            animationController.UpdateAndApply();
        }
Ejemplo n.º 32
0
        private void TurnOffGlassInternal()
        {
            if (glassVisual != null)
            {
                CleanUp();

                // Create a glass effect, requires Win2D NuGet package
                glassEffect = new GaussianBlurEffect
                {
                    Name       = "Blur",
                    BlurAmount = 0.0f,
                    BorderMode = EffectBorderMode.Hard,
                    Source     = new ArithmeticCompositeEffect
                    {
                        MultiplyAmount = 0,
                        Source1Amount  = 0.5f,
                        Source2Amount  = 0.5f,
                        Source1        = new CompositionEffectSourceParameter("backdropBrush"),
                        Source2        = new ColorSourceEffect
                        {
                            Name  = "NewColor",
                            Color = blurColor
                        }
                    }
                };

                //  Create an instance of the effect and set its source to a CompositionBackdropBrush
                effectFactory = compositor.CreateEffectFactory(glassEffect, new[] { "Blur.BlurAmount", "NewColor.Color" });
                effectBrush   = effectFactory.CreateBrush();

                effectBrush.SetSourceParameter("backdropBrush", backdropBrush);

                // Create a Visual to contain the frosted glass effect
                glassVisual       = compositor.CreateSpriteVisual();
                glassVisual.Brush = effectBrush;

                if (Animate)
                {
                    // https://blogs.windows.com/buildingapps/2016/09/12/creating-beautiful-effects-for-uwp/

                    ColorKeyFrameAnimation colorAnimation = compositor.CreateColorKeyFrameAnimation();
                    colorAnimation.InsertKeyFrame(0.0f, blurColor);
                    colorAnimation.InsertKeyFrame(1.0f, Colors.Transparent);
                    colorAnimation.Duration = TimeSpan.FromSeconds(2);
                    effectBrush.StartAnimation("NewColor.Color", colorAnimation);
                }

                // Make sure size of glass host and glass visual always stay in sync
                var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size");
                bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual);

                glassVisual.StartAnimation("Size", bindSizeAnimation);

                // Add the blur as a child of the host in the visual tree
                ElementCompositionPreview.SetElementChildVisual(GlassHost, glassVisual);

                //glassVisual.Dispose();
                //glassVisual = null;
            }

            SetValue(IsGlassOnProperty, false);

            //ElementCompositionPreview.SetElementChildVisual(GlassHost, null); //turn off the glass
        }
Ejemplo n.º 33
0
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            // create the compositor
            _compositor = ElementCompositionPreview.GetElementVisual(this).Compositor;

            // create what captures the pointer position
            _hoverPositionPropertySet = ElementCompositionPreview.GetPointerPositionPropertySet(r2);

            // create the two visuals
            _vis      = _compositor.CreateSpriteVisual();
            _pulseVis = _compositor.CreateSpriteVisual();

            // create the main brush with warm colors
            _mainBrush = _compositor.CreateRadialGradientBrush();
            _mainBrush.EllipseCenter = new Vector2(.5f, .5f);
            _mainBrush.EllipseRadius = new Vector2(.5f, .5f);

            _MBGradientStop1        = _compositor.CreateColorGradientStop();
            _MBGradientStop1.Offset = 0;
            _MBGradientStop1.Color  = _warmColor1;

            _MBGradientStop2        = _compositor.CreateColorGradientStop();
            _MBGradientStop2.Offset = .1f;
            _MBGradientStop2.Color  = _warmColor2;

            _MBGradientStop3        = _compositor.CreateColorGradientStop();
            _MBGradientStop3.Offset = 1;
            _MBGradientStop3.Color  = _warmColor3;

            _mainBrush.ColorStops.Add(_MBGradientStop1);
            _mainBrush.ColorStops.Add(_MBGradientStop2);
            _mainBrush.ColorStops.Add(_MBGradientStop3);

            // create the brush for the pulse visual
            _pulseBrush = _compositor.CreateRadialGradientBrush();
            _pulseBrush.EllipseCenter = new Vector2(.5f, .5f);
            _pulseBrush.EllipseRadius = new Vector2(.5f, .5f);

            _PBGradientStop1        = _compositor.CreateColorGradientStop();
            _PBGradientStop1.Offset = 0;
            _PBGradientStop1.Color  = _innerPulseColor;

            _PBGradientStop2        = _compositor.CreateColorGradientStop();
            _PBGradientStop2.Offset = 1;
            _PBGradientStop2.Color  = _innerPulseColor;

            _pulseBrush.ColorStops.Add(_PBGradientStop1);
            _pulseBrush.ColorStops.Add(_PBGradientStop2);

            // finish setting properties of the first visual
            _vis.Size        = new Vector2(300, 300);
            _vis.Offset      = new Vector3(((float)r2.ActualWidth / 2), ((float)r2.ActualHeight / 2), 0);
            _vis.AnchorPoint = new Vector2(.5f, .5f);
            _vis.Brush       = _mainBrush;

            // finish setting properties of the pulsing visual
            _pulseVis.Size        = new Vector2(500, 500);
            _pulseVis.Offset      = new Vector3(((float)r1.ActualWidth / 2), ((float)r1.ActualHeight / 2), 0);
            _pulseVis.AnchorPoint = new Vector2(.5f, .5f);
            _pulseVis.Brush       = _pulseBrush;

            // create the clip that makes the visuals circular
            CompositionGeometricClip   gClip  = _compositor.CreateGeometricClip();
            CompositionEllipseGeometry circle = _compositor.CreateEllipseGeometry();

            circle.Radius  = new Vector2(_vis.Size.X / 2, _vis.Size.Y / 2);
            circle.Center  = new Vector2(_vis.Size.X / 2, _vis.Size.Y / 2);
            gClip.Geometry = circle;

            _vis.Clip = gClip;

            CompositionGeometricClip   gClip2  = _compositor.CreateGeometricClip();
            CompositionEllipseGeometry circle2 = _compositor.CreateEllipseGeometry();

            circle2.Radius  = new Vector2(_pulseVis.Size.X / 2, _pulseVis.Size.Y / 2);
            circle2.Center  = new Vector2(_pulseVis.Size.X / 2, _pulseVis.Size.Y / 2);
            gClip2.Geometry = circle2;

            _pulseVis.Clip = gClip2;

            // set the pointer
            my_pointer = new Vector3(((float)r1.ActualWidth / 2), ((float)r1.ActualHeight / 2), 0);

            // set the visuals in the tree
            ElementCompositionPreview.SetElementChildVisual(r2, _vis);
            ElementCompositionPreview.SetElementChildVisual(r1, _pulseVis);

            // ellipse center follows mouse
            _ellipseCenterAnim = _compositor.CreateExpressionAnimation("Vector2(p.Position.X / 500.0f, p.Position.Y / 500.0f)");
            _ellipseCenterAnim.SetReferenceParameter("p", _hoverPositionPropertySet);
            _mainBrush.StartAnimation("EllipseCenter", _ellipseCenterAnim);

            // second stop is animated for "pulsing" effect within the first visual that runs constantly
            _offsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _offsetAnim.InsertKeyFrame(0, 0);
            _offsetAnim.InsertKeyFrame(1f, 1f);
            _offsetAnim.Duration       = TimeSpan.FromSeconds(2);
            _offsetAnim.IterationCount = 50;

            _MBGradientStop2.StartAnimation("Offset", _offsetAnim);

            // set up the animation for the backing pulse visual
            // animate the color
            _pulseColorAnim = _compositor.CreateColorKeyFrameAnimation();
            _pulseColorAnim.InsertKeyFrame(0, _innerPulseColor);
            _pulseColorAnim.InsertKeyFrame(.99f, _outerPulseColor);
            _pulseColorAnim.InsertKeyFrame(1, _innerPulseColor);
            _pulseColorAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseColorAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _PBGradientStop1.StartAnimation("Color", _pulseColorAnim);

            // animate offset of first stop
            _pulseStop1OffsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _pulseStop1OffsetAnim.InsertKeyFrame(0, 0);
            _pulseStop1OffsetAnim.InsertKeyFrame(1f, 1f);
            _pulseStop1OffsetAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseStop1OffsetAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _PBGradientStop1.StartAnimation("Offset", _pulseStop1OffsetAnim);

            // animate offset of second stop
            _pulseStop2OffsetAnim = _compositor.CreateScalarKeyFrameAnimation();
            _pulseStop2OffsetAnim.InsertKeyFrame(0, 0);
            _pulseStop2OffsetAnim.InsertKeyFrame(1f, 1f);
            _pulseStop2OffsetAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseStop2OffsetAnim.IterationBehavior = AnimationIterationBehavior.Forever;
            _pulseStop2OffsetAnim.DelayTime         = TimeSpan.FromSeconds(.25f);

            _PBGradientStop2.StartAnimation("Offset", _pulseStop2OffsetAnim);

            _pulseScaleAnim = _compositor.CreateVector3KeyFrameAnimation();
            _pulseScaleAnim.InsertKeyFrame(0, Vector3.Zero);
            _pulseScaleAnim.InsertKeyFrame(1, Vector3.One);
            _pulseScaleAnim.Duration          = TimeSpan.FromSeconds(1);
            _pulseScaleAnim.IterationBehavior = AnimationIterationBehavior.Forever;

            _pulseVis.StartAnimation("Scale", _pulseScaleAnim);
        }
Ejemplo n.º 34
0
        private void InitializeFrostedGlass(UIElement glassHost)
        {
            //https://msdn.microsoft.com/en-us/windows/uwp/graphics/using-the-visual-layer-with-xaml

            CleanUp();

            // Create a glass effect, requires Win2D NuGet package
            glassEffect = new GaussianBlurEffect
            {
                Name       = "Blur",
                BlurAmount = 10.0f, //original value: 15.0f
                BorderMode = EffectBorderMode.Hard,
                Source     = new ArithmeticCompositeEffect
                {
                    MultiplyAmount = 0,
                    Source1Amount  = 0.5f,
                    Source2Amount  = 0.5f,
                    Source1        = new CompositionEffectSourceParameter("backdropBrush"),
                    Source2        = new ColorSourceEffect
                    {
                        Name  = "NewColor",
                        Color = blurColor
                    }
                }
            };

            //  Create an instance of the effect and set its source to a CompositionBackdropBrush
            effectFactory = compositor.CreateEffectFactory(glassEffect, new[] { "Blur.BlurAmount", "NewColor.Color" });
            effectBrush   = effectFactory.CreateBrush();

            effectBrush.SetSourceParameter("backdropBrush", backdropBrush);

            glassVisual.Brush = effectBrush;

            if (Animate)
            {
                // https://blogs.windows.com/buildingapps/2016/09/12/creating-beautiful-effects-for-uwp/

                ColorKeyFrameAnimation colorAnimation = compositor.CreateColorKeyFrameAnimation();
                colorAnimation.InsertKeyFrame(0.0f, lastBlurColor);
                colorAnimation.InsertKeyFrame(1.0f, blurColor);
                colorAnimation.Duration = TimeSpan.FromSeconds(2);
                effectBrush.StartAnimation("NewColor.Color", colorAnimation);

                //ScalarKeyFrameAnimation blurAnimation = compositor.CreateScalarKeyFrameAnimation();
                //blurAnimation.InsertKeyFrame(0.0f, 0.0f);
                //blurAnimation.InsertKeyFrame(0.5f, 100.0f);
                //blurAnimation.InsertKeyFrame(1.0f, 0.0f);
                //blurAnimation.Duration = TimeSpan.FromSeconds(4);
                //blurAnimation.StopBehavior = AnimationStopBehavior.SetToFinalValue;
                //effectBrush.StartAnimation("Blur.BlurAmount", blurAnimation);
            }

            // Add the blur as a child of the host in the visual tree
            ElementCompositionPreview.SetElementChildVisual(glassHost, glassVisual);

            // Make sure size of glass host and glass visual always stay in sync
            var bindSizeAnimation = compositor.CreateExpressionAnimation("hostVisual.Size");

            bindSizeAnimation.SetReferenceParameter("hostVisual", hostVisual);

            glassVisual.StartAnimation("Size", bindSizeAnimation);

            SetValue(IsGlassOnProperty, true);
        }
Ejemplo n.º 35
0
        private async void CreateImageAndLights(Vector2 sizeLightBounds)
        {
            //
            // Image and effect setup
            //

            // Create the effect graph.  We will combine the desaturated image with two diffuse lights
            IGraphicsEffect graphicsEffect = new CompositeEffect()
            {
                Mode    = Microsoft.Graphics.Canvas.CanvasComposite.Add,
                Sources =
                {
                    new SaturationEffect()
                    {
                        Saturation = 0,
                        Source     = new CompositionEffectSourceParameter("ImageSource")
                    },

                    new PointDiffuseEffect()
                    {
                        Name          = "Light1",
                        DiffuseAmount = 1f,
                        Source        = new CompositionEffectSourceParameter("NormalMap"),
                    },

                    new PointDiffuseEffect()
                    {
                        Name          = "Light2",
                        DiffuseAmount = 1f,
                        Source        = new CompositionEffectSourceParameter("NormalMap"),
                    },
                }
            };

            // Create the normal map.  For this sample, we want a flat surface with no bumps
            CompositionDrawingSurface normalMap = await SurfaceLoader.LoadFromUri(new Uri("ms-appx:///Samples/SDK Insider/NowPlaying/NormalMap.jpg"));

            CompositionSurfaceBrush surfaceBrush = _compositor.CreateSurfaceBrush(normalMap);

            surfaceBrush.Stretch = CompositionStretch.Fill;

            // Create the effect factory, we're going to animate the light positions and colors
            CompositionEffectFactory effectFactory = _compositor.CreateEffectFactory(graphicsEffect,
                                                                                     new[] { "Light1.LightPosition", "Light1.LightColor",
                                                                                             "Light2.LightPosition", "Light2.LightColor", });

            // Create the effect brush and bind the normal map
            CompositionEffectBrush brush = effectFactory.CreateBrush();

            brush.SetSourceParameter("NormalMap", surfaceBrush);

            // Update the CompositionImage to use the custom effect brush
            ArtistImage.Brush = brush;


            //
            //  Animation setup
            //

            // Setup the first light's position, top and to the left in general
            LinearEasingFunction     linear            = _compositor.CreateLinearEasingFunction();
            Vector3KeyFrameAnimation positionAnimation = _compositor.CreateVector3KeyFrameAnimation();

            positionAnimation.InsertKeyFrame(0f, new Vector3(0f, 0f, 300f), linear);
            positionAnimation.InsertKeyFrame(.33f, new Vector3(sizeLightBounds.X * .5f, sizeLightBounds.Y * .5f, 100f), linear);
            positionAnimation.InsertKeyFrame(.66f, new Vector3(sizeLightBounds.X * .25f, sizeLightBounds.Y * .95f, 100f), linear);
            positionAnimation.InsertKeyFrame(1f, new Vector3(0f, 0f, 300f), linear);
            positionAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            positionAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light1.LightPosition", positionAnimation);


            // Setup the first light's color animation, cycling through some brighter tones
            ColorKeyFrameAnimation colorAnimation = _compositor.CreateColorKeyFrameAnimation();

            colorAnimation.InsertKeyFrame(0f, Colors.Orange);
            colorAnimation.InsertKeyFrame(.2f, Colors.Orange);
            colorAnimation.InsertKeyFrame(.3f, Colors.Red);
            colorAnimation.InsertKeyFrame(.5f, Colors.Red);
            colorAnimation.InsertKeyFrame(.6f, Colors.Yellow);
            colorAnimation.InsertKeyFrame(.8f, Colors.Yellow);
            colorAnimation.InsertKeyFrame(.9f, Colors.Orange);
            colorAnimation.InsertKeyFrame(1f, Colors.Orange);
            colorAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            colorAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light1.LightColor", colorAnimation);


            // Setup the second light's position, down and to the right in general
            positionAnimation = _compositor.CreateVector3KeyFrameAnimation();
            positionAnimation.InsertKeyFrame(0f, new Vector3(sizeLightBounds.X, sizeLightBounds.Y, 200f), linear);
            positionAnimation.InsertKeyFrame(.33f, new Vector3(sizeLightBounds.X * .7f, sizeLightBounds.Y * .9f, 300f), linear);
            positionAnimation.InsertKeyFrame(.66f, new Vector3(sizeLightBounds.X * .95f, sizeLightBounds.Y * .95f, 100f), linear);
            positionAnimation.InsertKeyFrame(1f, new Vector3(sizeLightBounds.X, sizeLightBounds.Y, 200f), linear);
            positionAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            positionAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light2.LightPosition", positionAnimation);

            // Setup the second light's color animation, cycling through some darker tones
            colorAnimation = _compositor.CreateColorKeyFrameAnimation();
            colorAnimation.InsertKeyFrame(0f, Colors.Blue);
            colorAnimation.InsertKeyFrame(.2f, Colors.Blue);
            colorAnimation.InsertKeyFrame(.3f, Colors.DarkGreen);
            colorAnimation.InsertKeyFrame(.5f, Colors.DarkGreen);
            colorAnimation.InsertKeyFrame(.6f, Colors.DarkBlue);
            colorAnimation.InsertKeyFrame(.8f, Colors.DarkBlue);
            colorAnimation.InsertKeyFrame(.9f, Colors.Blue);
            colorAnimation.InsertKeyFrame(1f, Colors.Blue);
            colorAnimation.Duration          = TimeSpan.FromMilliseconds(20000);
            colorAnimation.IterationBehavior = AnimationIterationBehavior.Forever;
            brush.StartAnimation("Light2.LightColor", colorAnimation);
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Disposes the resources
        /// </summary>
        public void Dispose()
        {
            // Clean up resources
            _compositor = null;
            Source = null;
            DataContext = null;
            Foreground = null;
            Background = null;
            _scheduledObject = null;
            _currentObject = null;

            // Clean up Composition Objects
            _imageSurface?.Dispose();
            _imageSurface = null;
            _nextImageSurface?.Dispose();
            _nextImageSurface = null;
            _frameLayerMask?.Dispose();
            _frameLayerMask = null;
            _placeholderContentMask?.Dispose();
            _placeholderContentMask = null;
            _placeholderContentBrush?.Dispose();
            _placeholderContentBrush = null;
            _nextSurfaceBrush?.Dispose();
            _nextSurfaceBrush = null;
            _rootContainer?.Dispose();
            _rootContainer = null;
            _shadowVisual?.Dispose();
            _shadowVisual = null;
            _frameLayer?.Dispose();
            _frameLayer = null;
            _frameBackgroundVisual?.Dispose();
            _frameBackgroundVisual = null;
            _placeholderBackgroundVisual?.Dispose();
            _placeholderBackgroundVisual = null;
            _placeholderContentVisual?.Dispose();
            _placeholderContentVisual = null;
            _frameContentVisual?.Dispose();
            _frameContentVisual = null;
            _nextVisualContent?.Dispose();
            _nextVisualContent = null;
            _shadow?.Dispose();
            _shadow = null;
            _layerEffectBrush?.Dispose();
            _layerEffectBrush = null;
            _imageOptions = null;
            _zoomInAnimationGroup?.Dispose();
            _zoomInAnimationGroup = null;
            _fadeOutAnimation?.Dispose();
            _fadeOutAnimation = null;
            _fadeInAnimation?.Dispose();
            _fadeInAnimation = null;
            _colorAnimation?.Dispose();
            _colorAnimation = null;
            _alignXAnimation?.Dispose();
            _alignXAnimation = null;
            _alignYAnimation?.Dispose();
            _alignYAnimation = null;
            _offsetAnimation?.Dispose();
            _offsetAnimation = null;
            _scaleAnimation?.Dispose();
            _scaleAnimation = null;

            // Dispose the generator at the end to allow the 
            // dependant composition objects to unsubscribe from
            // generator events
            _generator?.Dispose();
            _generator = null;
        }