p5py / p5

p5 is a Python package based on the core ideas of Processing.
https://p5.readthedocs.io
GNU General Public License v3.0
724 stars 120 forks source link

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

Open jb273678 opened 6 years ago

jb273678 commented 6 years ago

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

abhikpal commented 6 years ago

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

gotham13 commented 5 years ago

@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 commented 5 years ago

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

vaishnavsm commented 5 years ago

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.

Awerito commented 4 years ago

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

CodeCox commented 3 years ago

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

Skerminkel commented 2 years ago

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.