mgierlasinski / MagicGradients

Draw breathtaking backgrounds in your Xamarin.Forms application. It's a kind of magic.
MIT License
369 stars 21 forks source link

Gradient Masks #131

Closed mgierlasinski closed 3 years ago

mgierlasinski commented 3 years ago

API

<GradientView> 
    <GradientView.Mask>
         <TextMask Text="Hello" FontSize="20" />
    </GradientView.Mask>
</GradientView>

With markup extension

<GradientView Mask="{magic:TextMask 'Hello', FontSize=20" /> 

Sample implementations

public class RectangleMask : IMask
{
   public void DrawShape(GradientView view, RenderContext context)
   {
       context.Canvas.DrawRect(context.RenderRect, context.Paint);
   }
}
public class TextMask : IMask
{
   public string Text { get; set; }

   public void DrawShape(GradientView view, RenderContext context)
   {
       context.Canvas.DrawText(Text, ..., context.Paint);
   }
}
public class EllipseMask : IMask
{
   public void DrawShape(GradientView view, RenderContext context)
   {
       context.Canvas.DrawOval(....);
   }
}

Add multiple masks

<GradientView> 
    <GradientView.Mask>
          <MaskCollection>
                 <TextMask Text="Hello" FontSize="20" />
                 <OtherMask ... />
           </MaskCollection>
    </GradientView.Mask>
</GradientView>
public class MaskCollection : IMask
{
   public List<IMask> Masks { get; set; }

   public void DrawShape(GradientView view, RenderContext context)
   {
       foreach(var mask in Masks)
       {
            mask.DrawShape(view, context);
       }
   }
}