I've a SwiftUI-based app that draws into a Canvas
for a complicated, dynamic rendering. Since that rendering is based on a map of the world, I transform the provided context
to (±180°×±90°) longitude / latitude coordinates before stroking paths etc. Note that the necessary scaling flips the Y-axis because latitude increases up the screen.
All is well until I add words to the picture. Because of the inversion of the Y-axis, the text is rendered inverted.
mercatorContext.draw(Text(satellite.commonName)
.font(Font(.init(.userFixedPitch, size: 4.0)))
.foregroundColor(.white), at: satPoint)
My solution was to draw the text via a another (un-inverted) context
which corrects the words, but requires the satPoint
to be flipped to place the words at the right place on the (inverted) map ..
With that preamble, someone suggested I apply scaleEffect(y: -1)
to the Text
and avoid messing with more than one GraphicsContext
. This seemed an excellent solution but it doesn't work ..
context.draw(Text(..
draws a Text
view but applying scaleEffect
turns it into a View
which context.draw
can't accept. Once it's a View
, I suppose I could convert it to an Image
and context.draw(Image(..
instead which seems messy.
I wondered about the scaleEffect
function .. is it the case that it would ever actually return a view type that was different from the type it was given?
Leaving my curiosity aside, what would a better way than using a second context
to keep my text upright?