Forum Archive

canvas module equivalent of NSView's isFlipped?

roosterboy197

I know there's no direct equivalent to the isFlipped method of NSView (though it would be nice for the future), but how would one simulate the same effect in Pythonista's canvas module? I have some drawing code I'm trying to port over and the difference in the origins of the two coordinate systems is causing a problem.

omz

You mean that you want to flip the coordinate system? You can do that with the scale and translate functions (scale by 1,-1, then translate by the height of the canvas).

ccc

What am I doing wrong?

import canvas  # flipDisplay.py

screenHeight = 512
circleHeight = 128

colorBlue = (0, 0, 1)
colorRed  = (1, 0, 0)

canvas.clear()
canvas.set_size(screenHeight, screenHeight)

canvas.set_fill_color(*colorRed)   # Red circle appears at bottom.
canvas.fill_ellipse(10, 10, circleHeight, circleHeight)

canvas.scale(1, -1)
canvas.translate(0, -screenHeight)  # Fixed.  Thanks jose3f.

canvas.set_fill_color(*colorBlue)  # Blue circle appears at top.
canvas.fill_ellipse(10, 10, circleHeight, circleHeight)
jose3f23

canvas.translate(0, -screenHeight)

and do not forget that now the origin is top left

ccc

https://gist.github.com/cclauss/6313658 for the syntax:

with flippedDisplay():  # Set topLeft as graphics origin (0,0)
    canvas.draw_xxx()