2

I'm writing basic graphic editor in WPF using InkCanvas. I've made some custom shapes (inherited from Stroke). When I draw line wen on InkCanvas, I take first and last point and make a line. That works fine, but now I don't like the default "pen stroke" so i decided to rewrite DynamicRenderer to render line in real time.

Problem is, that DynamicRenderer draws line from origin point to every point of stroke and i obviously don't want that, becouse it makes "fan" insted of line.

There is my custom code and I'm looking for solution to draw line from origin point to last point only, if it is possible.

class LineRenderer : DynamicRenderer
{
    private Point firstPoint;
    private Pen pen = new Pen(new SolidColorBrush(Colors.Gray),1);

    public LineRenderer()
    {
        firstPoint = new Point(double.PositiveInfinity, double.PositiveInfinity);
    }

    protected override void OnStylusDown(RawStylusInput rawStylusInput)
    {
        firstPoint = new Point(rawStylusInput.GetStylusPoints().First().ToPoint().X, rawStylusInput.GetStylusPoints().First().ToPoint().Y);
        base.OnStylusDown(rawStylusInput);
    }

    protected override void OnDraw(DrawingContext drawingContext,
                                   StylusPointCollection stylusPoints,
                                   Geometry geometry, Brush fillBrush)
    {
        drawingContext.DrawLine(pen, firstPoint, stylusPoints.First().ToPoint());
    }

    protected override void OnStylusUp(RawStylusInput rawStylusInput)
    {
        firstPoint = new Point(double.PositiveInfinity, double.PositiveInfinity);
        base.OnStylusUp(rawStylusInput);
    }
}
Errol Piper
  • 21
  • 1
  • 2

2 Answers2

2

This is very much late. In order to avoid the "fan" while the stroke is being drawn, try this:

protected override void OnDraw(DrawingContext drawingContext,
                               StylusPointCollection stylusPoints,
                               Geometry geometry, Brush fillBrush)
        {
            if (!_isManipulating)
            {
                _isManipulating = true;
                StylusDevice currentStylus = Stylus.CurrentStylusDevice;
                this.Reset(currentStylus, stylusPoints);
            }
            _isManipulating = false;

            var pen = new Pen(brush, 2);
            drawingContext.DrawLine(pen, startPoint,stylusPoints.First().ToPoint());
       }


protected override void OnStylusDown(RawStylusInput rawStylusInput)
        {
            StylusPointCollection y = rawStylusInput.GetStylusPoints();
            startPoint = (Point)y.First();

            // Allocate memory to store the previous point to draw from.
            prevPoint = new Point(double.NegativeInfinity, double.NegativeInfinity);
            base.OnStylusDown(rawStylusInput);
        }

The trick here is to use DynamicRenderer.Reset, which:

Clears rendering on the current stroke and redraws it.

The redraw will reenter the OnDraw method, so _isManipulating provides a simple flag to stop looping.

Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
-1

what i think in your ondraw function your first point is fixed so it always take it as origin so you can try this for continous drawing.

protected override void OnDraw(DrawingContext drawingContext,
                               StylusPointCollection stylusPoints,
                               Geometry geometry, Brush fillBrush)
{
    drawingContext.DrawLine(pen, firstPoint, stylusPoints.First().ToPoint());
firstPoint = stylusPoints.First().ToPoint();
}

it will update your firstpoint if you get same point exception put a check on it ( or you can define anew point name as previous point and initially make it equal to first point and keep it updating in your ondraw method as i did with first point..

and if you want direct line from first point to last point you can called you on draw method after getting last point from onstylusup method ..hope this might help you..

loop
  • 9,002
  • 10
  • 40
  • 76
  • When i move my mouse, i want to draw line vrom first point (this is why it is fixed) to point where the cursor is. This works, but it draws line for every single point on path of movement. I want only one line: from the frst point (where i clicked/the fixed one) to current position of cursor when I move it. – Errol Piper Jul 16 '13 at 12:47
  • look what is happening your ondraw is being called every point on line right so it draw line to from your firstpoint to each point you get on canvas..so you have to just update your first point to your current point till that line is got drawn..the code i have given just check it..it will work..you can check this by putting break point on ondraw method.. – loop Jul 16 '13 at 12:51
  • Looks like we don't understand each other, so I draw little diagram [link](http://errolek.cz/img/schema.png). I click (red circle) and move mouse (red line). Red rectangles are colected point. I want to draw blue line (one at the time) as i move mouse. Your code just connects red rectangles and make line along the red one. – Errol Piper Jul 16 '13 at 13:40
  • look what i think.. you want only one line from your first point to last point(the point when you leave the canvas.ok).. – loop Jul 17 '13 at 05:12