Transforming path without transforming stroke width

24 views
Skip to first unread message

Sherlock Doyle

unread,
Nov 11, 2021, 3:20:50 AMNov 11
to skia-discuss
I'm drawing (stroking) path on a transformed canvas. I want the line to be drawn with the necessary transformation, but don't want the stroke width to change. This is a sample pseudo-code to show what I mean.

path = Path.Circle(150, 150, 100)
canvas.scale(0.5, 1)
canvas.drawPath(path, stroke_paint)


Output: (Original refers to the original path drawn without any transformations)
path815.png

As you see, the stroke width gets scaled too, which I don't want. How should I go about getting what I want?

paul....@gmail.com

unread,
Nov 12, 2021, 2:44:53 AMNov 12
to skia-discuss
Instead of scaling the canvas, you can scale the path instead using  SkPath.transform().

paul....@gmail.com

unread,
Nov 12, 2021, 2:56:12 AMNov 12
to skia-discuss
And if it's not feasible to remove the canvas scale, then apply the inverse transform to the path before you draw it.

Ie.  path.transform(inverseTransform)

Where inverseTransform can be retrieved by calling invert() on the current Canvas matrix, if you don't already know it.

Sherlock Doyle

unread,
Nov 13, 2021, 7:56:51 AMNov 13
to skia-discuss
Transforming the path isn't an option for me since I need the canvas' transformations. This is because I need the canvas' transformation applied to the paint; for instance, if the paint has a gradient.
However, based on the ideas above, I've found a way (rather cumbersome). Any ideas for simplification are welcome.

transformation = canvas.getTotalMatrix() // get the canvas' transformation
transformed_path = Path()
original_path.transform(transformation, transformed_path) // transform the path forward (this changes position, etc)

fill_path = Path()
stroke_paint.getFillPath(transformed_path, fill_path) // create a filled version of the path

inverse_transformation = Matrix()
transformation.invert(inverse_transformation) // get the inverse transformation
fill_path.transform(inverse_transformation) // transform the path back (this changes position, including the stroke width in reverse since we've a filled path now)

stroke_paint.setStyle(Paint.kFill_Style) // change the paint style to fill
canvas.drawPath(fill_path, stroke_paint) // draw the path with the canvas' transformation (this reverses the stroke width change)


Reply all
Reply to author
Forward
0 new messages