spriograph.st
· 1.6 KiB · Smalltalk
Raw
Playground
NB[ Spirograph program, done for comparison between StackTalk, Carousel/Lua and Lisp
Carousel version by akkartik
StackTalk version by yumaikas
]
me. >Lobby
#game: Canvas .query-ctx-2d do[
%w[ blue grey red ] each[ dup curry[ color= ] fn ]
] >ctx
RAF: fn[ Lobby frame. Window .requestAnimationFrame ]
once: fn[ ts. >oldTs once: drop once: fn[ ] ]
frame: fn[ once
ts. oldTs> - >dt
ts> >oldTs
draw
dt: drop RAF
] RAF
make-spiro: fn[
obj[ >color >r >r2 >rotSpeed
200 >x 200 >y 0 >T Canvas .new-path-2d >spiro
move-rT: fn[ >d >r >y >x
y> d. sin r. * + x> d> cos r> * + ]
update: fn[ 1000 div T> + >T ]
draw: fn[ >ctx
x y r ctx .pen[ grey circle ]
x y r T move-rT >x2 >y2
x2 y2 5 ctx .brush[ red circle ]
x2 y2 r2 ctx .pen[ grey circle ]
x2 y2 r2 T rotSpeed * move-rT >x3 >y3
x3 y3 x2> y2> ctx .pen[ grey moveTo lineTo ]
x3 y3 5 ctx .brush[ blue circle ]
color ctx .color=
spiro ask[ your[ x3> y3> ] line-to your[ ctx> ] to-canvas ]
]
]
]
[
5 50 100 red: make-spiro
5.2 35 125 green: make-spiro
10.4 10 40 yellow: make-spiro
20 10 110 gold: make-spiro
5 50 105 red: make-spiro
5.2 35 130 green: make-spiro
10.4 10 35 yellow: make-spiro
20 10 140 gold: make-spiro
5.2 55 105 red: make-spiro
5.2 40 130 green: make-spiro
10.3 15 35 yellow: make-spiro
30 13 140 gold: make-spiro
] Array .of >gears
draw: fn[
#111: ctx .clear
gears each[ ask[
your[ dt. ] update
your[ ctx. ] draw
] ]
]
| 1 | NB[ Spirograph program, done for comparison between StackTalk, Carousel/Lua and Lisp |
| 2 | Carousel version by akkartik |
| 3 | StackTalk version by yumaikas |
| 4 | ] |
| 5 | me. >Lobby |
| 6 | #game: Canvas .query-ctx-2d do[ |
| 7 | %w[ blue grey red ] each[ dup curry[ color= ] fn ] |
| 8 | ] >ctx |
| 9 | RAF: fn[ Lobby frame. Window .requestAnimationFrame ] |
| 10 | once: fn[ ts. >oldTs once: drop once: fn[ ] ] |
| 11 | frame: fn[ once |
| 12 | ts. oldTs> - >dt |
| 13 | ts> >oldTs |
| 14 | draw |
| 15 | dt: drop RAF |
| 16 | ] RAF |
| 17 | |
| 18 | make-spiro: fn[ |
| 19 | obj[ >color >r >r2 >rotSpeed |
| 20 | 200 >x 200 >y 0 >T Canvas .new-path-2d >spiro |
| 21 | move-rT: fn[ >d >r >y >x |
| 22 | y> d. sin r. * + x> d> cos r> * + ] |
| 23 | update: fn[ 1000 div T> + >T ] |
| 24 | draw: fn[ >ctx |
| 25 | x y r ctx .pen[ grey circle ] |
| 26 | x y r T move-rT >x2 >y2 |
| 27 | x2 y2 5 ctx .brush[ red circle ] |
| 28 | x2 y2 r2 ctx .pen[ grey circle ] |
| 29 | x2 y2 r2 T rotSpeed * move-rT >x3 >y3 |
| 30 | x3 y3 x2> y2> ctx .pen[ grey moveTo lineTo ] |
| 31 | x3 y3 5 ctx .brush[ blue circle ] |
| 32 | color ctx .color= |
| 33 | spiro ask[ your[ x3> y3> ] line-to your[ ctx> ] to-canvas ] |
| 34 | ] |
| 35 | ] |
| 36 | ] |
| 37 | |
| 38 | [ |
| 39 | 5 50 100 red: make-spiro |
| 40 | 5.2 35 125 green: make-spiro |
| 41 | 10.4 10 40 yellow: make-spiro |
| 42 | 20 10 110 gold: make-spiro |
| 43 | |
| 44 | 5 50 105 red: make-spiro |
| 45 | 5.2 35 130 green: make-spiro |
| 46 | 10.4 10 35 yellow: make-spiro |
| 47 | 20 10 140 gold: make-spiro |
| 48 | |
| 49 | 5.2 55 105 red: make-spiro |
| 50 | 5.2 40 130 green: make-spiro |
| 51 | 10.3 15 35 yellow: make-spiro |
| 52 | 30 13 140 gold: make-spiro |
| 53 | |
| 54 | ] Array .of >gears |
| 55 | |
| 56 | draw: fn[ |
| 57 | #111: ctx .clear |
| 58 | gears each[ ask[ |
| 59 | your[ dt. ] update |
| 60 | your[ ctx. ] draw |
| 61 | ] ] |
| 62 | ] |
| 63 |