Hi!
I am struggling to think of a way to endlessly move a rect() along X axis and "rewind" back to 0 once it moves past page bounds – essentially creating a smooth movement.
Here‘s my code: it kind of works, but it starts to break as soon the cycle runs repeatedly.
I know there must be a mistake sowewhere and my terrible math skills only got me so far:
Variable([
dict(name="progress", ui="Slider",
args=dict(
value=0,
minValue=0,
maxValue=1,
)),
], globals())
# Use page dimensions as frame for debugging
pageW, pageH = (500, 500)
# Arbitrary backgrounds to tell them apart
backgrounds = [(0.5, 1, 0.5), (0.5, 1, 1), (0.5, 0, 0), (1, 1, 0), (0.5, 0, 1), (0.5, 0.5, 0.5), (0, 1, 0)]
newPage(900, 900)
itemCount = 6
itemW = 100
itemH = pageH
translate(200, 200)
offsetX = (progress * pageW) * 4 # Speed up 4x
for item in range(itemCount):
# Position on init
startingX = item * itemW
# Move in time
currentX = startingX + offsetX
# How many times whole cycle elapsed
fullProgressCount = offsetX // pageW
# How many times this item elapsed
elapsedCount = (currentX) // pageW
# Rewind to beginning
rewind = (elapsedCount * pageW) + itemW if elapsedCount else 0
# Real X position
realX = currentX - rewind
# Fill and draw
fill(*backgrounds[item])
rect(realX, 0, itemW, itemH)
# Debug item
fill(0)
text(f'{str(item)}\nReal: {round(realX)}\ncX: {round(currentX)}\nEl: {elapsedCount}\n{rewind}', (currentX - rewind, 560))
# Debug status
fill(0)
text(f'{str(progress)}\n{offsetX}\n{fullProgressCount}', (-100, -100))
# Draw debugging page frame
with savedState():
fill(None)
stroke(0)
rect(0, 0, pageW, pageH)
Thanks so much for any pointers.