Skip to content

Instantly share code, notes, and snippets.

@dela3499
Last active August 29, 2015 14:27
Show Gist options
  • Save dela3499/2cff87bc3a63e1f429dd to your computer and use it in GitHub Desktop.
Save dela3499/2cff87bc3a63e1f429dd to your computer and use it in GitHub Desktop.
A hacked-up canvas drawing that takes advantage of easing, circles, lines, and colors. http://share-elm.com/gists/2cff87bc3a63e1f429dd
import Color exposing (..)
import Graphics.Collage exposing (..)
import Graphics.Element exposing (..)
import Mouse
import Signal
import Window
import Time
import List
type alias Pos = (Int, Int)
config =
{ rs = [ 11.71630375, 15.73713928, 6.78684501, 4.82127608,
9.48062476, 7.36505383, 19.50820433, 9.44403149,
19.64276505, 14.73209169, 11.78007254, 16.77807227,
2.26111131, 3.34936254, 6.45023204, 18.16470948,
18.75500152, 7.92744326, 8.10679818, 13.98237027,
13.71772771, 15.07030045, 17.56863092, 13.63286304,
7.31712858, 19.25017072, 0.28716668, 2.08691612,
5.5219993 , 3.2400437 , 4.47150208, 19.98123505,
13.97395796, 5.72912029, 13.21701992, 17.18222282,
11.3359453 , 3.63364374, 2.1638158 , 19.54247912,
1.31945086, 12.12743979, 1.11673472, 19.24686421,
12.62153802, 2.472378 , 10.204799 , 15.43618744,
17.05220695, 15.04300971, 3.21028444, 3.88317009,
7.35784094, 16.92835753, 2.79879311, 1.82192534,
5.03123544, 0.21017111, 10.01255613, 2.13252375,
9.29466902, 9.1772429 , 10.323122 , 8.28639052,
16.35262695, 0.676837 , 4.28795543, 15.29497507,
8.78407421, 16.66309437, 4.78445746, 10.95356908,
6.55059975, 19.09912371, 17.80361979, 17.93965859,
3.37251098, 6.63053816, 10.9488653 , 17.557282 ,
16.22624179, 0.55578485, 12.62652863, 13.40353954,
12.34223886, 19.66972411, 8.24830533, 10.06067751,
9.47023011, 5.50738928, 10.77336101, 15.25381075,
15.47006843, 2.48226456, 10.64176169, 2.66997435,
11.29600454, 8.50547213, 5.04069848, 6.32750494 ]
, lengths = [ 389.25726777, 178.03462619, 292.74252795, 102.6879681 ,
9.7584858 , 250.48449675, 125.4356764 , 262.09019128,
347.58546085, 204.9484853 , 113.19494337, 202.83304944,
89.08668454, 259.33126291, 39.91340147, 476.77731601,
359.05275879, 261.41399326, 57.80634132, 128.34770268,
490.27105407, 232.00391262, 137.94786577, 410.56633042,
207.12213718, 76.27684753, 35.0871706 , 63.01389875,
301.49179656, 66.70927097, 372.62071707, 15.61208018,
162.01536808, 144.27860609, 46.25476759, 347.94985526,
19.0783253 , 154.36752007, 342.62525243, 28.77488102,
464.67356094, 265.48900386, 442.48068276, 492.68295253,
444.06313374, 305.66091104, 183.89164696, 266.43955688,
350.87971271, 473.34206348, 148.93382987, 205.16059864,
159.51378517, 138.13075854, 466.93463235, 238.93941601,
435.1347872 , 424.09509225, 100.91484033, 284.33938379,
14.21769591, 11.34197815, 389.97139441, 437.35638596,
279.38049378, 18.29158066, 279.94483327, 144.33310569,
302.21892342, 254.42022356, 245.83843756, 101.03934195,
380.51432103, 430.35091087, 429.37405683, 188.70724387,
487.14883319, 208.74527822, 421.0868943 , 210.00844739,
225.34379298, 146.24211329, 155.90255385, 389.17441375,
437.80917555, 342.05799388, 216.62069216, 14.27594237,
327.86053779, 474.67048137, 172.85873202, 115.35728399,
388.21860173, 371.85431048, 322.99646054, 160.13981588,
492.43075421, 124.99622861, 128.86944554, 363.62004092]
, colors = [ 0.16115834, 0.50294619, 0.54132462, 0.08990062, 0.06901996,
0.7710781 , 0.08895044, 0.26814078, 0.84747646, 0.22547006,
0.51965424, 0.4889109 , 0.46162889, 0.82725018, 0.79109398,
0.46853299, 0.02034533, 0.91054961, 0.63059006, 0.03067754,
0.63546573, 0.56741336, 0.82079575, 0.86314617, 0.08231803,
0.64830054, 0.47531006, 0.20849731, 0.57013223, 0.57193829,
0.46079821, 0.14406123, 0.48780105, 0.19354031, 0.11685322,
0.13070985, 0.44384247, 0.04473897, 0.38693902, 0.04373465,
0.60841848, 0.69326551, 0.1135386 , 0.15837321, 0.96918778,
0.82554393, 0.57629402, 0.66379332, 0.5127199 , 0.40315205,
0.70471442, 0.77430921, 0.69586255, 0.84856193, 0.43431455,
0.43537224, 0.57450321, 0.54586911, 0.58364746, 0.97568555,
0.82567532, 0.65162331, 0.10000282, 0.96188943, 0.42967312,
0.82418293, 0.45615335, 0.4232124 , 0.12156035, 0.55316518,
0.93812439, 0.28563757, 0.52969189, 0.91592772, 0.91160698,
0.72112622, 0.12639023, 0.5965989 , 0.88239143, 0.10146802,
0.15116888, 0.10260329, 0.08871854, 0.65070484, 0.63058392,
0.67351089, 0.78009483, 0.01279271, 0.87175884, 0.93794123,
0.40832141, 0.50525635, 0.2363822 , 0.44398153, 0.99504244,
0.48630859, 0.32783508, 0.00144573, 0.84554617, 0.30213732]
}
view: (Int, Int) -> Pos -> Float -> Float -> Element
view (width, height) (x, y) radius t =
let canvasX = x - (width // 2)
canvasY = (height - y) - (height // 2)
rs = List.map (\x -> radius + (x * 3)) config.rs
lengths = config.lengths
colors = List.map (\c -> c * 20 / radius) config.colors
lines = group (createLines rs lengths colors)
angle = (floor t % 360000) |> toFloat |> (\x -> 200 * (logBase 2 (x * 10/ (radius)))) |> degrees
in
layers
[ collage (width) (height)
[ rect (toFloat width) (toFloat height) |> filled (rgb 20 40 (20 + (floor (0.3 * radius))))
, rotate angle (move (toFloat canvasX, toFloat canvasY) lines)
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 2)) |> filled (rgba 30 60 (20 + (floor (0.3 * radius))) 0.5))
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 1.75)) |> filled (rgba 30 60 (20 + (floor (0.3 * radius))) 0.5))
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 1.5)) |> filled (rgba 30 60 (20 + (floor (0.3 * radius))) 0.5))
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 0.4)) |> filled (rgba 121 255 57 (30 / radius)))
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 0.3)) |> filled (rgba 121 255 57 (30 / radius)))
, move (toFloat canvasX, toFloat canvasY) ((circle (radius * 0.2)) |> filled (rgba 121 255 57 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) + radius) ((circle (radius * 0.2)) |> filled (rgba 100 0 0 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) + radius) ((circle (radius * 0.15)) |> filled (rgba 255 255 255 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) + radius) ((circle (radius * 0.10)) |> filled (rgba 100 0 0 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) - radius) ((circle (radius * 0.2)) |> filled (rgba 100 0 0 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) - radius) ((circle (radius * 0.15)) |> filled (rgba 255 255 255 (30 / radius)))
, move (toFloat canvasX, (toFloat canvasY) - radius) ((circle (radius * 0.10)) |> filled (rgba 100 0 0 (30 / radius)))
]
, flow down
[ show ("Window.dimensions", width, height)
, show ("Mouse.position", x, y)
, show ("Canvas position", canvasX, canvasY)
]
]
createLine: Float -> Float -> Float -> Float -> Form
createLine r theta len col =
[(r, theta), (r + len, theta)] -- Polar Points
|> List.map fromPolar
|> path
|> traced (solid (hsla 1 1 1 col))
createLines: List Float -> List Float -> List Float -> List Form
createLines rs lengths colors =
let thetas = List.map turns (linspace 0 1 (List.length lengths))
in List.map4 createLine rs thetas lengths colors
-- Return list of integers from start to end (not including end)
range: Int -> Int -> List Int
range start end =
if start < end
then start :: (range (start + 1) end)
else []
linspace: Float -> Float -> Int -> List Float
linspace start end n =
let interval = (end - start) / (toFloat n)
in List.map (\i -> interval * (toFloat i)) (range 0 n)
myCircle radius =
(circle radius) |> filled lightBlue
--main = Signal.map2 view Window.dimensions target
currentTime = (Time.every Time.millisecond)
timeOfClick = Signal.sampleOn Mouse.clicks currentTime
timeSinceClick = Signal.map2 (\t t0 -> t - t0) currentTime timeOfClick
bounce duration t =
let amplitude = 2.0
frequency = 10 / duration
decay_period = 0.3 * duration --0.15 * duration
in amplitude * (sin (frequency * t)) * e ^ (t * (-1.0 / decay_period))
--main = Signal.map (\t -> show (bounce 4000 t)) timeSinceClick
circleRadius =
Signal.map (\t -> (bounce 2000 t) * 70 + 50) timeSinceClick
target: Signal Pos
target = Signal.sampleOn Mouse.clicks Mouse.position
main = Signal.map4 view Window.dimensions target circleRadius currentTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment