Created
June 26, 2017 15:47
-
-
Save JohnnyWestlake/b17210352857fd860313ce1d1c5b645b to your computer and use it in GitHub Desktop.
XAML Blur Brush
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Emilie.Framework.Media; | |
using Microsoft.Graphics.Canvas.Effects; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Windows.UI.Composition; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Media; | |
namespace Emilie.Framework.Media.Brushes | |
{ | |
public class BlurBrush : XamlCompositionBrushBase | |
{ | |
static string BLUR_PATH { get; } = $"Blur.{nameof(BlurAmount)}"; | |
static CompositionEffectFactory BLUR_FACTORY; | |
Compositor _compositor; | |
#region Dependency Properties | |
public double BlurAmount | |
{ | |
get { return (double)GetValue(BlurAmountProperty); } | |
set { SetValue(BlurAmountProperty, value); } | |
} | |
public static readonly DependencyProperty BlurAmountProperty = | |
DependencyProperty.Register(nameof(BlurAmount), typeof(double), typeof(BlurBrush), new PropertyMetadata(0d, (d, e) => | |
{ | |
((BlurBrush)d).BlurChanged(); | |
})); | |
public bool IsAutoAnimateEnabled | |
{ | |
get { return (bool)GetValue(IsAutoAnimateEnabledProperty); } | |
set { SetValue(IsAutoAnimateEnabledProperty, value); } | |
} | |
public static readonly DependencyProperty IsAutoAnimateEnabledProperty = | |
DependencyProperty.Register(nameof(IsAutoAnimateEnabled), typeof(bool), typeof(BlurBrush), new PropertyMetadata(false)); | |
public TimeSpan AutoAnimateDuration | |
{ | |
get { return (TimeSpan)GetValue(AutoAnimateDurationProperty); } | |
set { SetValue(AutoAnimateDurationProperty, value); } | |
} | |
public static readonly DependencyProperty AutoAnimateDurationProperty = | |
DependencyProperty.Register(nameof(AutoAnimateDuration), typeof(TimeSpan), typeof(BlurBrush), new PropertyMetadata(TimeSpan.FromSeconds(0.2))); | |
#endregion | |
protected override void OnConnected() | |
{ | |
base.OnConnected(); | |
_compositor = Window.Current.Compositor; | |
this.CompositionBrush = GetBlurBrush(); | |
} | |
protected override void OnDisconnected() | |
{ | |
base.OnDisconnected(); | |
CompositionBrush?.Dispose(); | |
CompositionBrush = null; | |
} | |
private void BlurChanged() | |
{ | |
if (!IsAutoAnimateEnabled) | |
{ | |
CompositionBrush?.Properties.InsertScalar(BLUR_PATH, (float)BlurAmount); | |
} | |
else | |
{ | |
CompositionBrush?.Properties.StartAnimation(BLUR_PATH, _compositor.CreateScalarKeyFrameAnimation() | |
.AddKeyFrame(1, (float)BlurAmount) | |
.SetDuration(AutoAnimateDuration)); | |
} | |
} | |
CompositionBrush GetBlurBrush() | |
{ | |
if (CompositionBrush != null) | |
return CompositionBrush; | |
if (BLUR_FACTORY == null) | |
{ | |
var graphicsEffect = new GaussianBlurEffect() | |
{ | |
Name = "Blur", | |
Source = new CompositionEffectSourceParameter("Backdrop"), | |
BlurAmount = 0f, | |
BorderMode = EffectBorderMode.Hard, | |
Optimization = EffectOptimization.Speed, | |
BufferPrecision = Microsoft.Graphics.Canvas.CanvasBufferPrecision.Precision16Float | |
}; | |
BLUR_FACTORY = _compositor.CreateEffectFactory(graphicsEffect, new[] { BLUR_PATH }); | |
} | |
var blurBrush = BLUR_FACTORY.CreateBrush(); | |
blurBrush.SetSourceParameter("Backdrop", _compositor.CreateBackdropBrush()); | |
return blurBrush; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment