-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathanimate.fsx
39 lines (32 loc) · 1.29 KB
/
animate.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#r "nuget:DIKU.Canvas, 2.0"
open Canvas
open Color
type state = float // a rotation degree
let w,h = 256,256 // the size of the canvas
let N = 360.0 // Number of degrees to sample
/// Update state function
let next d = (d % N) + 1.0
// A non-trivial tree
let lst1 = [(10.0,10.0); (60.0, 80.0); (10.0, 80.0); (10.0, 10.0)]
let lst2 = [(0.0,0.0); (40.0, 30.0); (0.0, 30.0); (0.0, 0.0)]
let tri1 = piecewiseAffine green 1.0 lst1
let tri2 = filledPolygon purple lst2
let ell = ellipse blue 4.0 20.0 25.0
let fig = translate 50.0 50.0 (alignV (alignH tri1 Center tri2) Center ell)
/// Prepare a Picture by the present state whenever needed
let draw (i:state): Picture =
let fw,fh = getSize <| getBoundingBox fig
let cx,cy = fw/2.0, fh/2.0
let rad = i/(N-1.0)*2.0*System.Math.PI
let figi = rotate cx cy rad fig
printfn "%s" (toString figi) // Print to screen,e.g., for debugging
make figi
/// React to whenever an event happens
let react (s:state) (ev:Event) : state option =
match ev with
| Event.TimerTick -> Some (next s)
| _ -> None // all other events are ignored
// Start interaction session
let initialState = 0.0 // First state drawn by draw
let delayTime = (Some 100) // microseconds (as an option type)
interact "animate" w h delayTime draw react initialState