0
我創建了一個簡單的工作動畫方法Canvas
library我正在創建,但我無法完全弄清楚如何使動畫循環 - 從方法本身給出選項。我的意思是我知道如何在應用程序本身中循環它,但不僅僅是如果我通過方法loop
參數,然後它爲我做。這裏的動畫片段:循環動畫
Canvas.objects.Base.prototype.animate = function (options) {
options.easing = Canvas.animation.easing[options.easing] || options.easing;
options.duration = Canvas.animation.durations[options.duration] || options.duration * 1000;
var start = Date.now(),
total = start + options.duration,
old = {},
id,
self = this;
for (var i in options.properties) {
if (options.properties.hasOwnProperty(i)) {
old[i] = this[i];
}
}
(function update() {
var now = Date.now(),
progress = Math.min((options.duration - (total - now))/options.duration, 1);
for (var i in options.properties) {
if (options.properties.hasOwnProperty(i)) {
self[i] = options.easing(now - start, old[i], options.properties[i] - old[i], options.duration);
}
}
if (progress < 1) {
id = requestAnimationFrame(update);
} else {
id = cancelAnimationFrame(id);
if (options.loop) {
var animate = function() {
for (var j in options.properties) {
self[j] = old[j];
}
if (!options.callback) {
self.animate({
properties: options.properties,
easing: options.easing,
duration: options.duration,
callback: animate
});
} else {
self.animate({
properties: options.properties,
easing: options.easing,
duration: options.duration,
callback: function() {
options.callback();
animate();
}
});
}
};
options.callback = animate;
}
if (options.callback) {
options.callback();
}
}
}());
};
現在,這裏的code我不得不做出的循環工作方法本身之外:
var animate = function() {
circle.x = test.canvas.width/2;
circle.y = test.canvas.height/2;
circle.animate({
properties: {
x: test.canvas.width
},
easing: 'easeInOutExpo',
duration: 1,
callback: function() {
circle.animate({
properties: {
y: test.canvas.height
},
easing: 'easeInOutExpo',
duration: 1,
callback: animate
});
}
});
};
// 'animate` is called later on a click
隨着我的方法的代碼現在,一旦它執行最初的動畫,它會在動畫之前恢復到它的所有屬性,但是直到再次單擊該窗口爲止。
編輯:哦,這裏的code我現在不工作。