Search code examples
canvasskiasharpskia

How to transform path instead of transforming Canvas in Skiasharp?


In Skiasharp, I have a transformation matrix which I apply on the canvas to move a path and follow the displacement of the mouse. Presently, I compute a transformMatrix from the mouse coordinates and evreything works fine :

    canvas.Save();                  // Save the actuel transform matrix
    SKMatrix ZoomMatrix = canvas.TotalMatrix.PostConcat(transformMatrix);
    canvas.SetMatrix(ZoomMatrix);   // Apply the matrix for actual Transform
    canvas.DrawPath(Path, paint);
    canvas.Restore();               // And restore the initial transform matrix

But actually I need to transform the Path itself rather than "moving" the canvas. If I use :

    Path.Transform(transformMatrix);
    canvas.DrawPath(Path, paint);

the path is not at the same position than when moving the canvas ?!? Any Idea to computes the correct transform Matrix to apply to the path to get the same result ?

Just to be sure (I don't know if it's important to mention it) but the matrix of the canvas is no more the identity matrix. Of course it doesn't change during the tests !


Solution

  • Ok, I was completely wrong with the approach ... !

    When a path is moved, instead of applying a matrix transform to the canvas (which is the correct approach when the canvas itself is moved or rescaled), the solution is in this case to directly transform the path itself :

        SelectedPath.Transform(SKMatrix.CreateTranslation(dx, dy));
    

    Just so easy ...