I'm working on an app to draw a lot of strokes in an InkCanvas surface.
My big problem is while user draws a stroke(mouseDown and MouseMove), it doesn't look smooth:
Only after user had finished to draw this stroke(MouseUp) it gets smooth because I set InkCanvas to FitToCurve. But there is a kind of flick.
Can I use CustomDynamicRender to allow smooth processing while the user is drawing?
As a result, we want the result while drawing and the final reflected result (Stroke) to be the same.
With FitToCurve settings applied
/// Updated
Here is my DynamicRenderer's code.
Doing this source will add lines over and over again.
Is there a way to erase and redraw the existing line to keep the stroke neatly one line?
And this method is obviously very poorly performing.
Is there a better way?
class CustomDynamicRenderer : DynamicRenderer
{
//[ThreadStatic]
//static private Brush brush = null;
//
//[ThreadStatic]
//static private Pen pen = null;
private Point prevPoint;
StylusPointCollection stylusPointsColl;
DrawingAttributes inkDA = new DrawingAttributes();
Stroke stroke;
public CustomDynamicRenderer() : base()
{
inkDA.IgnorePressure = true;
inkDA.Width = 1;
inkDA.Height = 1;
inkDA.Color = Colors.Red;
inkDA.FitToCurve = true;
Matrix matrix = new Matrix(1, 0, 0, 1, 0, 0);
matrix.Rotate(45);
inkDA.StylusTipTransform = matrix;
}
protected override void OnStylusUp(RawStylusInput rawStylusInput)
{
base.OnStylusUp(rawStylusInput);
}
protected override void OnStylusDown(RawStylusInput rawStylusInput)
{
// Allocate memory to store the previous point to draw from.
prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
stylusPointsColl ??= new StylusPointCollection();
stylusPointsColl.Clear();
base.OnStylusDown(rawStylusInput);
}
InkPresenter ip = new InkPresenter();
protected override void OnDraw(DrawingContext drawingContext,
StylusPointCollection stylusPoints,
Geometry geometry, Brush fillBrush)
{
foreach(var stylusPoint in stylusPoints)
{
Point pt = (Point)stylusPoint;
Vector v = Point.Subtract(prevPoint, pt);
if(v.Length > 25)
{
drawingContext.Dispatcher.Invoke(new Action(() =>
{
stylusPointsColl.Add(new StylusPoint(stylusPoint.X, stylusPoint.Y, 0.5f));
stroke ??= new Stroke(stylusPointsColl, inkDA);
stroke.StylusPoints = stylusPointsColl;
stroke.StylusPoints = stroke.GetBezierStylusPoints();
stroke.Draw(drawingContext, inkDA);
prevPoint = pt;
}));
}
}
}
}
]