33// See the LICENSE file in the project root for more information.
44
55using System ;
6+ using System . Numerics ;
67using System . Threading . Tasks ;
78using LottieViewer . ViewModel ;
9+ using Microsoft . Graphics . Canvas ;
10+ using Microsoft . Graphics . Canvas . Effects ;
11+ using Microsoft . Graphics . Canvas . UI . Composition ;
812using Microsoft . UI . Xaml . Controls ;
13+ using Windows . Graphics . DirectX ;
914using Windows . Storage ;
15+ using Windows . Storage . Streams ;
1016using Windows . UI ;
17+ using Windows . UI . Composition ;
1118using Windows . UI . Xaml ;
1219using Windows . UI . Xaml . Controls ;
20+ using Windows . UI . Xaml . Hosting ;
21+ using Windows . UI . Xaml . Media . Imaging ;
1322
1423namespace LottieViewer
1524{
@@ -31,7 +40,54 @@ public Stage()
3140 {
3241 this . InitializeComponent ( ) ;
3342
34- Reset ( ) ;
43+ // Do not show solid background by default.
44+ ShowSolidBackground = false ;
45+
46+ // Do not show checkerboard pattern until lottie file is loaded.
47+ _canvas . Visibility = Visibility . Collapsed ;
48+
49+ _feedbackLottie . PlayInitialStateAnimation ( ) ;
50+ }
51+
52+ // Draw repeating pattern texture on backgound canvas.
53+ public async Task SetupBackgroundPatternAsync ( )
54+ {
55+ var compositor = ElementCompositionPreview . GetElementVisual ( this ) . Compositor ;
56+ var canvasDevice = CanvasDevice . GetSharedDevice ( ) ;
57+ var graphicsDevice = CanvasComposition . CreateCompositionGraphicsDevice ( compositor , canvasDevice ) ;
58+
59+ var bitmap = await CanvasBitmap . LoadAsync ( canvasDevice , @"Assets\BackgroundPattern.png" ) ;
60+
61+ var drawingSurface = graphicsDevice . CreateDrawingSurface (
62+ bitmap . Size ,
63+ DirectXPixelFormat . B8G8R8A8UIntNormalized ,
64+ DirectXAlphaMode . Premultiplied ) ;
65+
66+ using ( var ds = CanvasComposition . CreateDrawingSession ( drawingSurface ) )
67+ {
68+ ds . Clear ( Colors . Transparent ) ;
69+ ds . DrawImage ( bitmap ) ;
70+ }
71+
72+ var surfaceBrush = compositor . CreateSurfaceBrush ( drawingSurface ) ;
73+ surfaceBrush . Stretch = CompositionStretch . None ;
74+
75+ var border = new BorderEffect
76+ {
77+ ExtendX = CanvasEdgeBehavior . Wrap ,
78+ ExtendY = CanvasEdgeBehavior . Wrap ,
79+ Source = new CompositionEffectSourceParameter ( "source" ) ,
80+ } ;
81+
82+ var fxFactory = compositor . CreateEffectFactory ( border ) ;
83+ var fxBrush = fxFactory . CreateBrush ( ) ;
84+ fxBrush . SetSourceParameter ( "source" , surfaceBrush ) ;
85+
86+ var sprite = compositor . CreateSpriteVisual ( ) ;
87+ sprite . Size = new Vector2 ( 4096 ) ;
88+ sprite . Brush = fxBrush ;
89+
90+ ElementCompositionPreview . SetElementChildVisual ( _canvas , sprite ) ;
3591 }
3692
3793 // The DiagnosticsViewModel contains information about the currently playing
@@ -41,6 +97,21 @@ public Stage()
4197
4298 internal AnimatedVisualPlayer Player => _player ;
4399
100+ internal Viewbox PlayerContainer => _playerContainer ;
101+
102+ private bool showSolidBackground = false ;
103+
104+ internal bool ShowSolidBackground
105+ {
106+ get => showSolidBackground ;
107+
108+ set
109+ {
110+ _backgroundColorBrush . Opacity = value ? 1.0 : 0.0 ;
111+ showSolidBackground = value ;
112+ }
113+ }
114+
44115 public Color ArtboardColor
45116 {
46117 get { return ( Color ) GetValue ( ArtboardColorProperty ) ; }
@@ -56,6 +127,10 @@ internal async Task<bool> TryLoadFileAsync(StorageFile file)
56127 {
57128 // Load the Lottie composition.
58129 await _playerSource . SetSourceAsync ( file ) ;
130+
131+ await SetupBackgroundPatternAsync ( ) ;
132+
133+ _canvas . Visibility = Visibility . Visible ;
59134 }
60135 catch ( Exception )
61136 {
@@ -89,10 +164,5 @@ internal void DoDragLeave()
89164 {
90165 _feedbackLottie . PlayDragLeaveAnimation ( ) ;
91166 }
92-
93- internal void Reset ( )
94- {
95- _feedbackLottie . PlayInitialStateAnimation ( ) ;
96- }
97167 }
98168}
0 commit comments