Skip to content

Instantly share code, notes, and snippets.

Last active Dec 21, 2015
What would you like to do?
Pythonista's canvas module's graphics origin (0, 0) is at the bottomLeft. "with flippedDisplay": will temporarily switch the graphics origin to be at the topLeft.
import canvas, scene
from contextlib import contextmanager
def privateGstate():
"""Save the canvas.gstate and then restore it when leaving the 'with' clause."""
try: yield None
finally: canvas.restore_gstate()
def privateMatrix():
"""Save the scene.matrix and then restore it when leaving the 'with' clause."""
try: yield None
finally: scene.pop_matrix()
def flippedDisplay():
"""Flip the display so that the origin is the topLeft instead of the bottomLeft.
Restore the origin to bottomLeft when leaving the 'with' clause."""
canvas.scale(1, -1)
canvas.translate(0, -screenHeight)
try: yield None
finally: canvas.restore_gstate()
screenHeight = 512
circleHeight = 128
colorBlue = (0, 0, 1)
colorGreen = (0, 1, 0)
colorRed = (1, 0, 0)
def coloredCircle(inColor, inX, inY):
canvas.fill_ellipse(inX, inY, circleHeight, circleHeight)
canvas.set_size(screenHeight * 1.42, screenHeight)
with privateGstate(): # Save and then restore the canvas.gstate
canvas.rotate(89) # Text on an angle.
canvas.draw_text('Green on top -->', 154, 0, 'Helvetica', 36)
canvas.draw_text('<-- Red and Blue on bottom', 282, 52, 'Helvetica', 36)
x = 20 + circleHeight
coloredCircle(colorRed, 10, 10) # Red circle appears at bottom.
with flippedDisplay(): # Change origin to topLeft instead of bottomLeft.
coloredCircle(colorGreen, x, 10) # Green circle appears at top.
coloredCircle(colorBlue, x, 10) # Blue circle appears at bottom.
Copy link

cclauss commented Aug 26, 2013

Added coloredCircle() to increase readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment