Redraw shape only if size changed

I have a task where you can change the length of a horizontal line by pressing two keys, so you increase the length by pressing ‘z’ or reduce the length by pressing ‘m’. I’m currently checking for key presses and updating the size of the shape with a custom python code (auto-translated to JS) that is repeated every frame. Something like this:

pressedKey = event.getKeys(keyList=['z','m'])
if len(pressedKey) > 0:
    if pressedKey[0] == 'z':
        averageLength      -= 2
    else:
        averageLength      += 2

line.size =[(averageLength * 2), 0]

Right now if you don’t press anything the line is still redrawn on every frame. In the console there is this repeating message:

>>>>>>>>> CREATING PIXI POLYGON!!!! 189 visual-2020.1.js:493:11

I’m worried this continuous redrawing is going to slow down participants computers.

Is it possible to redraw only if the size of the line is changed? Or, is it likely that this continuous redrawing will slowdown participants computers?

I also tried the code below but then the line disappears when no key is pressed and appears fleetingly when pressing a key.

line.setAutoDraw(False)
pressedKey = event.getKeys(keyList=['z','m'])
if len(pressedKey) > 0:
    if pressedKey[0] == 'z':
        length      -= 2
        line.setAutoDraw(True)
    else:
        length      += 2
        line.setAutoDraw(True)

line.size =[(length), 0]
pressedKey = event.getKeys(keyList=['z','m'])
if len(pressedKey) > 0:
    if pressedKey[0] == 'z':
        averageLength      -= 2
        line.setSize((averageLength * 2), 0)
    else:
        averageLength      += 2
        line.setSize((averageLength * 2), 0)

One line shorter

pressedKey = event.getKeys(keyList=['z','m'])
if len(pressedKey) > 0:
    if pressedKey[0] == 'z':
        averageLength      -= 2
    else:
        averageLength      += 2
    line.setSize((averageLength * 2), 0)
1 Like