Skip to content

Drawing API

mwerezak edited this page Mar 7, 2021 · 2 revisions

Modification

Although drawings can be made dynamic by clearing and recreating the entire draw list a more efficient method is offered.

To make a drawing dynamic we can use the DrawCommand object returned from the draw method. Simply modify the command's properties to dynamically change the drawing.

This example is a direct port of the Dear PyGui example, for easy comparison.

from dearpygui_obj import start_gui, set_render_callback
from dearpygui_obj.data import color_from_rgba8
from dearpygui_obj.window import Window
from dearpygui_obj.drawing import DrawingCanvas

with Window("Tutorial", width=800, height=800):
    canvas = DrawingCanvas(width=700, height=700)
    circle = canvas.draw_circle((0, 0), 5, (1.0, 1.0, 1.0))

counter = 0
modifier = 2

@set_render_callback
def on_render():
    global counter, modifier
    counter += 1
    if counter < 300:
        modifier += 1
    elif counter < 600:
        modifier -= 1
    else:
        counter = 0
        modifier = 2

    xpos = 15 + modifier*1.25
    ypos = 15 + modifier*1.25
    circle.center=(xpos, ypos)

    color1 = 255 - modifier*.8
    color3 = 255 - modifier*.3
    color2 = 255 - modifier*.8
    circle.color = color_from_rgba8(color1, color2, color3)

    circle.radius = 15 + modifier/2
    circle.segments = round(35-modifier/10)

start_gui()
Clone this wiki locally