Skip to content

Instantly share code, notes, and snippets.

@MachinesAreUs
Last active January 4, 2016 04:39
Show Gist options
  • Save MachinesAreUs/8569909 to your computer and use it in GitHub Desktop.
Save MachinesAreUs/8569909 to your computer and use it in GitHub Desktop.
Randomly placed colorful circles.
import Window
import Graphics.Collage
import Random
import Text
import Signal
radius = 30
lineWidth = 3
bubbleAlpha = 0.4
-- Util
(!!) list idx = drop idx list |> head
text =
toForm . Text.text . (Text.color white) . toText . show
-- Program
scatter : (Int,Int) -> Int -> [Int] -> Form -> Form
scatter (w,h) idx rands f =
let (wf,hf) = (toFloat w, toFloat h)
randsf = map toFloat rands
[a,b] = drop (idx * 2) randsf |> take 2
pos = (a * (wf - 2 * radius) / 200, b * (hf - 2 * radius) / 200)
in move pos f
bubble r color =
let baseOutline = solid color
outline = { baseOutline | width <- lineWidth}
in group [
circle (r+lineWidth) |> outlined outline,
circle r |> filled color |> alpha bubbleAlpha
]
scene {ttime, delta, rands, dim} =
let (w,h) = dim
toBubble = bubble radius
colors = [red, orange, yellow, green, blue, purple]
unpositionedBubbles = map toBubble colors
indexedBubbles = zip unpositionedBubbles [0..10]
bubbles = map (\(f,idx) -> scatter (w,h) idx rands f) indexedBubbles
in collage w h bubbles |> color black
type Input = { ttime: Time, delta: Time, rands: [Int], dim: (Int,Int) }
input =
let source = fps 1
ttime = foldp (+) 0 source
rands = combine <| map (\_ -> Random.range -100 100 source) [1..20]
in sampleOn ttime <| Input <~ ttime
~ (inSeconds <~ source)
~ rands
~ Window.dimensions
main = lift scene input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment