Created
February 16, 2013 15:14
-
-
Save benshimmin/4967331 to your computer and use it in GitHub Desktop.
Execute a series of named functions in a sequence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paper = Raphael el | |
rect = paper.rect 100, 100, 100, 100 | |
methods = | |
move : (x, y) -> | |
rect.attr | |
transform : "...T#{x},#{y}" | |
rotate : (ang) -> | |
rect.attr | |
transform : "...R#{ang}" | |
scale : (fac) -> | |
rect.attr | |
transform : "...S#{fac},#{fac}" | |
# `sequence` takes an array of objects and a delay between execution | |
# of each function | |
sequence = (fns, t) -> | |
call = (fn, args, d) -> | |
_.delay -> # could be `setTimeout`, if you prefer | |
methods[fn].apply @, args | |
, d | |
d = fns[0].delay ? t | |
call(f.fn, f.args, d += f.delay ? t) for f in fns | |
fns = [ | |
# function to call | |
fn : "move" | |
# arguments to pass to function | |
args : [100, 100] | |
# optional delay before execution | |
delay : 1000 | |
, | |
fn : "rotate" | |
args : [45] | |
, | |
fn : "move" | |
args : [50, 50] | |
, | |
fn : "scale" | |
args : [2, 2] | |
, | |
fn : "move" | |
args : [200, 200] | |
delay : 1000 | |
, | |
fn : "rotate" | |
args : [45] | |
, | |
fn : "scale" | |
args : [0.5, 0.5] | |
, | |
fn : "move" | |
args : [-350, -350] | |
] | |
# call the sequence with a delay of 100ms between each call, | |
# unless otherwise specified | |
sequence fns, 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment