start simplifying animations

pull/1937/head
Richard Harris 7 years ago
parent 4f5f187cbe
commit 35aee272d6

@ -13,8 +13,8 @@ export function wrapAnimation(node, from, fn, params) {
const duration = 'duration' in info ? info.duration : 300;
const delay = 'delay' in info ? info.delay : 0;
const ease = info.easing || linear;
const start = window.performance.now() + delay;
const end = start + duration;
const start_time = window.performance.now() + delay;
const end = start_time + duration;
const program = {
a: 0,
@ -22,66 +22,59 @@ export function wrapAnimation(node, from, fn, params) {
b: 1,
delta: 1,
duration,
start,
start: start_time,
end
};
const cssText = node.style.cssText;
const animation = {
pending: delay ? program : null,
program: delay ? null : program,
running: true,
function start() {
if (info.css) {
if (delay) node.style.cssText = cssText;
start() {
if (info.css) {
if (delay) node.style.cssText = cssText;
program.name = create_rule(program, ease, info.css);
program.name = create_rule(program, ease, info.css);
node.style.animation = (node.style.animation || '')
.split(', ')
.filter(anim => anim && (program.delta < 0 || !/__svelte/.test(anim)))
.concat(`${program.name} ${program.duration}ms linear 1 forwards`)
.join(', ');
}
node.style.animation = (node.style.animation || '')
.split(', ')
.filter(anim => anim && (program.delta < 0 || !/__svelte/.test(anim)))
.concat(`${program.name} ${program.duration}ms linear 1 forwards`)
.join(', ');
}
animation.program = program;
animation.pending = null;
},
running_program = program;
pending_program = null;
}
update: now => {
const p = now - program.start;
const t = program.a + program.delta * ease(p / program.duration);
if (info.tick) info.tick(t, 1 - t);
},
let running = true;
let pending_program = delay ? program : null;
let running_program = delay ? null : program;
done() {
if (info.tick) info.tick(1, 0);
animation.stop();
},
function stop() {
if (info.css) delete_rule(node, program.name);
running = false;
}
stop() {
if (info.css) delete_rule(node, program.name);
animation.running = false;
const { abort, promise } = loop(now => {
if (pending_program && now >= pending_program.start) {
start();
}
};
const { abort, promise } = loop(() => {
const now = window.performance.now();
if (animation.program && now >= animation.program.end) {
animation.done();
if (running_program && now >= running_program.end) {
if (info.tick) info.tick(1, 0);
stop();
}
if (animation.pending && now >= animation.pending.start) {
animation.start(animation.pending);
if (!running) {
return false;
}
if (animation.running) {
animation.update(now);
return true;
if (running_program) {
const p = now - program.start;
const t = program.a + program.delta * ease(p / program.duration);
if (info.tick) info.tick(t, 1 - t);
}
return true;
});
if (info.tick) info.tick(0, 1);
@ -89,10 +82,11 @@ export function wrapAnimation(node, from, fn, params) {
if (delay) {
if (info.css) node.style.cssText += info.css(0, 1);
} else {
animation.start();
start();
}
return animation;
// TODO just return the function
return { stop };
}
export function fixPosition(node) {

@ -3,7 +3,7 @@ let running = false;
function run_tasks() {
tasks.forEach(task => {
if (!task[0]()) {
if (!task[0](window.performance.now())) {
tasks.delete(task);
task[1]();
}

@ -61,9 +61,7 @@ export function wrapTransition(component, node, fn, params, intro) {
if (!this.running) {
this.running = true;
const { abort, promise } = loop(() => {
const now = window.performance.now();
const { abort, promise } = loop(now => {
if (this.program && now >= this.program.end) {
this.done();
}

Loading…
Cancel
Save