Skip to content

Instantly share code, notes, and snippets.

@villares
Last active September 29, 2024 18:48
Show Gist options
  • Save villares/968c41e594db6b83172700dcf856a09b to your computer and use it in GitHub Desktop.
Save villares/968c41e594db6b83172700dcf856a09b to your computer and use it in GitHub Desktop.
fading screensaver-like sketch show
"""
You'll need py5 and a use the run_sketch tool or Thonny + thonny-py5mode
to run this py5 "imported mode" style sketch. Learn more at py5coding.org
I will run on full screen. Click on screen and press ESC to exit.
Adjust speed by changing the time_per_image variable in milliseconds.
"""
time_per_image = 3000 # milliseconds
fade_time = 500 # actually fade in + fade out will be double this
last_image_time = 0
seed = 0
def setup():
full_screen() # this is an option instead of size()
# size(400, 400) # test
def draw():
global seed
global last_image_time
background(0)
### Your work goes here
random_seed(seed)
grid(width / 2, height / 2, height * 0.8)
### Transitions
time_past = millis() - last_image_time
# fade out
fade_out = time_per_image - time_past
if fade_out < fade_time:
fill(0, 255 - fade_out / fade_time * 255)
rect(0, 0, width, height)
# fade in
if time_past < fade_time:
fill(0, 255 - time_past / fade_time * 255)
rect(0, 0, width, height)
if time_past > time_per_image:
seed = seed + 1
last_image_time = millis()
def grid(gx, gy, gw):
color_mode(HSB)
no_stroke()
n = 4
cw = gw / n
for i in range(n):
cx = i * cw + cw / 2 + gx - gw / 2
for j in range(n):
cy = j * cw + cw / 2 + gy - gw / 2
if random_int(1, 3) == 1 and cw > 50:
grid(cx, cy, cw)
else:
# diâmeto baseado na soma da linha coluna
d = random(10, cw * 2)
# matiz baseado no produto da linha e coluna
h = 10 + random_choice(((i * j), (i + j))) * 20
c = color(h, 255, 200, 200)
fill(c)
# desenho do elemento em x, y
circle(cx, cy, d)
@villares
Copy link
Author

villares commented Jul 29, 2024

fade_peek

@villares
Copy link
Author

A "module mode" template

"""
This is a py5 "module mode" style sketch. Learn more at py5coding.org

I will run on full screen. Click on screen and press ESC to exit.
Adjust speed by changing the time_per_image variable in milliseconds. 
"""
import py5

time_per_image = 3000 # milliseconds
fade_time = 500  # actually fade in + fade out will be double this
last_image_time = 0
seed = 0

def setup():
    py5.full_screen() # this is an option instead of size()
    # size(400, 400) # test
    
def draw():
    global seed
    global last_image_time
    py5.background(0)
    ### Your work goes here
    py5.random_seed(seed)    
    something(py5.width / 2, py5.height / 2, py5.height * 0.8)
    
    
    ### Transitions
    py5.rect_mode(py5.CORNER)
    time_past = py5.millis() - last_image_time
    # fade out
    fade_out = time_per_image - time_past
    if fade_out < fade_time:
        py5.fill(0, 255 - fade_out / fade_time * 255)
        py5.rect(0, 0, py5.width, py5.height)
    # fade in
    if time_past < fade_time:
        py5.fill(0, 255 - time_past / fade_time * 255)
        py5.rect(0, 0, py5.width, py5.height)

    if time_past > time_per_image:
        seed = seed + 1
        last_image_time = py5.millis()

def something(x, y, s):
    py5.rect_mode(py5.CENTER)
    py5.color_mode(py5.HSB)
    py5.no_stroke()    
    w = py5.random(s / 2, s)
    h = py5.random(s / 2, s)
    c = py5.color((w * h) % 255, 255, 200, 200)
    py5.fill(c)
    drawing_func = py5.random_choice((py5.rect, py5.ellipse))
    drawing_func(x, y, w, h)

py5.run_sketch()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment