p5 icon indicating copy to clipboard operation
p5 copied to clipboard

background() doesn't work as expected when called from setup().

Open jb273678 opened this issue 6 years ago • 7 comments

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

jb273678 avatar Aug 01 '18 18:08 jb273678

Thanks for reporting this! I can reproduce this on my machine.. so I'll take a look : )

abhikpal avatar Aug 09 '18 20:08 abhikpal

@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

gotham13 avatar Oct 14 '18 09:10 gotham13

commenting this out keeps the background color but I dont know the use of this line, Its probably important

gotham13 avatar Oct 14 '18 09:10 gotham13

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.

vaishnavsm avatar Mar 04 '19 08:03 vaishnavsm

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

Awerito avatar Feb 06 '20 07:02 Awerito

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?)

CodeCox avatar Oct 02 '20 15:10 CodeCox

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.

Skerminkel avatar Mar 25 '22 13:03 Skerminkel