Skip to content

Instantly share code, notes, and snippets.

@eduairet
Created February 3, 2021 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduairet/f3eeb144c058a76c7c2099f46c40e968 to your computer and use it in GitHub Desktop.
Save eduairet/f3eeb144c058a76c7c2099f46c40e968 to your computer and use it in GitHub Desktop.
DrawBot.app script to generate a waterfall of items
# Functions
def hex2rgb(hexColor): # Transforms a Hex color to its DrawBot RGB value (0 to 1 basis)
offset = 1 if hexColor.startswith('#') else 0
r = int(hexColor[offset:offset+2], 16)
g = int(hexColor[offset+2:offset+4], 16)
b = int(hexColor[offset+4:], 16)
return r/255, g/255, b/255
def lerp(minVal, maxVal, factor): # Linear interpolation
return minVal + (maxVal - minVal) * factor
def canvas(x, y, hexColor): # Create a canvas with background
color = hex2rgb(hexColor)
newPage(x, y)
with savedState():
fill(*color)
rect(0, 0, x, y)
def resizeIm(x, y, path, size, xtranslate, ytranslate, angle): # Place and resize any image
w, h = imageSize(path)
s = size/w
with savedState():
translate(xtranslate, ytranslate)
scale(s)
rotate(angle, (w/2*s, h/2*s))
image(path, (0, 0))
def shapeMap(x, y, shapes=100): # Make random coordinates and starting angles for your images
arr = [ # Your images to display
'./donut1.png',
'./donut2.png'
]
shapesData = []
for shape in range(shapes):
shapesData.append([
randint(0, x), randint(0, y), # Random x and y coordinates
randint(0, 360), # Random starting angle
choice(arr) # Random choice of our images
])
return shapesData
def designBot(x=1920, y=1080, frames=8): # Put all together
shapes = shapeMap(x, y)
speed = y/frames
for i in range(frames):
canvas(x, y, '#34e5eb')
for shape in shapes:
an = lerp(shape[2], 360 + shape[2], i/frames) # Angle interpolation
ypos = lerp(shape[1], y + shape[1], i/frames) # Y coordinate interpolation
if ypos > y: # If shape finishes the canvas it restarts
ypos = lerp(shape[1] - y, shape[1], i/frames)
resizeIm(x, y, shape[3], randint(50, 70), shape[0], ypos, an)
# Design
designBot()
saveImage('~/Desktop/randomDonuts.gif')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment