Skip to content

async_rotate()

Luke edited this page Apr 15, 2020 · 2 revisions

CanvasPlus.async_rotate()

In addition to CanvasPlus.rotate(), asynchronous rotation is also possible, using the asyncio library.

from numbers import Real
from typing import Union, Tuple 
async def async_rotate(self,
    tagOrId: Union[int, str], x: Real, y: Real, time: float, amount: Real,
    unit: str = "rad", warn: bool = True, fps: int = 24, update: bool = True
) -> Tuple[Union[float, int]]

Asynchronously rotate tagOrId on axis x, y by amount in degrees or radians clockwise (use negaitves for counter-clockwise) fps: frames per second, time: specify the amount of time the animation shall take to complete, update: call update() method within loop

Example:

from CanvasPlus import CanvasPlus
from tkinter import Tk
import math
import asyncio

#set up canvas
root = Tk()
canvas = CanvasPlus(root, width = 800, height = 800, background = "white")
canvas.pack()

rect1 = canvas.create_polygon(300, 300, 300, 500, 500, 500, 500, 300, fill = "blue")
rect2 = canvas.clone(rect1)
rect3 = canvas.clone(rect1)
rect4 = canvas.clone(rect1)

async def _test():
    await asyncio.gather(
        canvas.async_rotate(rect1, 300, 300, 1, 2*math.pi, unit = "r", fps = 24),
        canvas.async_rotate(rect2, 500, 500, 1, 2*math.pi, unit = "rad", fps = 24),
        canvas.async_rotate(rect3, 300, 500, 1, 360, unit = "d", fps = 24),
        canvas.async_rotate(rect4, 500, 300, 1, 360, unit = "deg", fps = 24)
    )
    await asyncio.gather(
        canvas.async_rotate(rect1, 200, 200, .25, .5*math.pi, unit = "r", fps = 24),
        canvas.async_rotate(rect2, 600, 600, .25, .5*math.pi, unit = "rad", fps = 24),
        canvas.async_rotate(rect3, 200, 600, .25, 90, unit = "d", fps = 24),
        canvas.async_rotate(rect4, 600, 200, .25, 90, unit = "deg", fps = 24)
    )
    await asyncio.gather(
        canvas.async_rotate(rect1, 200, 200, .25, -.5*math.pi, unit = "r", fps = 24),
        canvas.async_rotate(rect2, 600, 600, .25, -.5*math.pi, unit = "rad", fps = 24),
        canvas.async_rotate(rect3, 200, 600, .25, -90, unit = "d", fps = 24),
        canvas.async_rotate(rect4, 600, 200, .25, -90, unit = "deg", fps = 24)
    )

asyncio.run(_test())
canvas.mainloop()

async_rotate Note: The frame rates in this GIF do not accurately represent the actual frame rates, due to the nature of GIF images

Clone this wiki locally