p5
p5 copied to clipboard
background() doesn't work as expected when called from setup().
Test script:
from p5 import *
def setup(): background(255, 0, 0) size(600, 600) no_loop()
def draw(): circle((300, 300), 50, "CENTER")
run()
Expected behavior: Set background color to red.
Actual behavior: background color set to black -no matter what arguments are used. (background() works as expected when called from draw()).
Errors produced (if any): No errors.
p5 version: v0.4.0a1.dev2
Python version: 3.7.0 Operating System: Windows 10
Thanks for reporting this! I can reproduce this on my machine.. so I'll take a look : )
@abhikpal I found the source of this problem. This line is resetting the background color. https://github.com/p5py/p5/blob/7269080cb3566260f9086760e9062b87c45ac1d7/p5/core/structure.py#L72
commenting this out keeps the background color but I dont know the use of this line, Its probably important
The function background()
does not, in fact, set a background color or image. It instead draws the background or image once and resets all the parameters, as the push_style
context manager is used.
This is (apparently) required for images, and has been maintained for colors, probably for consistency in working.
This is probably not the intended behavior from the code, though, as is clear from the docstring and the renderer.background_color = background_color.normalized
line.
Sending a fix.
commenting this out keeps the background color but I dont know the use of this line, Its probably important
I comment that line and the background is fix but everything else draw in setup slill being erased when I draw something in the draw loop
still an issue in v0.7.1
What's the status on this? (A PR was merged a while back but the the issue is still open?)
Still hasn't been sorted out, but I found a workaround for anyone searching for this problem.
Instead of setting your background in setup, do it once in draw (I just use an if statement). I don't know if this will mess with images, but it works if you just want to change your background at least.