Hello, I'm using canvas to draw some 2D scene and ...
# compose
d
Hello, I'm using canvas to draw some 2D scene and it works fine, but I want to be able to zoom the scene. I added some buttons that increase/decrease a scale and uses that with
DrawScope.scale(scale)
, but it scales the thickness of the lines. How can I scale the lines without making them thicker?
m
You can apply the inverse scale to the line thickness parameter.
k
.scale
scales everything. You’ll need to either: a) apply your scaling factor manually on all the coordinates you pass to different calls or b) apply “inverse” scaling on the stroke width
d
I think the first option is good, but I don't know how it impacts the position before/after the zoom. Is the second option good for float calculations? I don't know if the stroke calculated will be too small or something and they won't be showing in some zoom levels
k
The second option might or might not break pixel snapping, depending on how Skia treats these combined scales. You know how floating operations can be off a few digits after the decimal point. Like, 3.0 x (1.0 / 3.0) might not get you back to 1.0
m
To my opinion inverse scaling works nicely. I use it all the time for the route display here: https://pbs.twimg.com/media/FW4_9sOWQAIT5Sy?format=png
👍🏻 1
👍 1
For some performance optimisation you may want to change the value only when it has changed by more than a certain percentage, e.g. 20% or so.