Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to supply own update method #3

Open
WickyNilliams opened this issue Jan 15, 2015 · 0 comments
Open

Allow users to supply own update method #3

WickyNilliams opened this issue Jan 15, 2015 · 0 comments

Comments

@WickyNilliams
Copy link

It would be nice if you could supply own update method. In my use case I am using the accelerometer to govern progress (e.g. shaking phone increases velocity, and thus progress along line. progress is not tied to time or frames, but to user's shake movement).

By allowing users to supply their own update function, the lib gets more flexible and there's an opportunity to reduce code duplication between animate_frame and animate_duration.

You can provide some higher-order functions to replace the internal animate_frame/duration functions:

metronome.timeBasedUpdate = function(duration) {
  var start = Date.now();

  return function() {
    return Math.min(1, (Date.now() - start) / duration);
  };
}

metronome.frameBasedUpdate = function(duration) {
  var frame = 0;

  return function() {
    return Math.min(1, (frame++ / duration));
  };
}

then you could use metronome like so:

metronome({
  update : metronome.frameBasedUpdate(200)
  render : function() {} // whatever
})

metronome({
  update : metronome.timeBasedUpdate(2000)
  render : function() {} // whatever 
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant