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

Trigger onChange and onComplete with the right values #5813

Merged
merged 2 commits into from
Jul 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions src/util/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
return false;
}

function defaultEasing(t, b, c, d) {
return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;
}

/**
* Changes value from one to another within certain period of time, invoking callbacks as value is being changed.
* @memberOf fabric.util
Expand All @@ -28,7 +32,7 @@
onChange = options.onChange || noop,
abort = options.abort || noop,
onComplete = options.onComplete || noop,
easing = options.easing || function(t, b, c, d) {return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;},
easing = options.easing || defaultEasing,
startValue = 'startValue' in options ? options.startValue : 0,
endValue = 'endValue' in options ? options.endValue : 100,
byValue = options.byValue || endValue - startValue;
Expand All @@ -38,24 +42,26 @@
(function tick(ticktime) {
// TODO: move abort call after calculation
// and pass (current,valuePerc, timePerc) as arguments
if (abort()) {
onComplete(endValue, 1, 1);
return;
}
time = ticktime || +new Date();
var currentTime = time > finish ? duration : (time - start),
timePerc = currentTime / duration,
current = easing(currentTime, startValue, byValue, duration),
valuePerc = Math.abs((current - startValue) / byValue);
onChange(current, valuePerc, timePerc);
if (abort()) {
onComplete(endValue, 1, 1);
return;
}
if (time > finish) {
options.onComplete && options.onComplete();
onChange(endValue, 1, 1);
onComplete(endValue, 1, 1);
return;
}
requestAnimFrame(tick);
else {
onChange(current, valuePerc, timePerc);
requestAnimFrame(tick);
}
})(start);
});

}

var _requestAnimFrame = fabric.window.requestAnimationFrame ||
Expand Down