Skip to content

Commit

Permalink
add crossfade and flip functions (sveltejs#2241)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Mar 17, 2019
1 parent c2413ae commit 394e3e9
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules
/easing.js
/motion.*
/transition.js
/animate.js
/scratch/
/coverage/
/coverage.lcov/
Expand Down
25 changes: 25 additions & 0 deletions animate.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { cubicOut } from './easing';
import { is_function } from './internal';

export function flip(node, animation, params) {
const style = getComputedStyle(node);
const transform = style.transform === 'none' ? '' : style.transform;

const dx = animation.from.left - animation.to.left;
const dy = animation.from.top - animation.to.top;

const d = Math.sqrt(dx * dx + dy * dy);

const {
delay = 0,
duration = d => Math.sqrt(d) * 120,
easing = cubicOut
} = params;

return {
delay,
duration: is_function(duration) ? duration(d) : duration,
easing,
css: (t, u) => `transform: ${transform} translate(${u * dx}px, ${u * dy}px);`
};
}
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export default [
},

// everything else
...['index', 'store', 'easing', 'transition'].map(name => ({
...['index', 'store', 'easing', 'transition', 'animate'].map(name => ({
input: `${name}.mjs`,
output: {
file: `${name}.js`,
Expand Down
59 changes: 59 additions & 0 deletions transition.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cubicOut, cubicInOut } from './easing';
import { assign, is_function } from './internal';

export function fade(node, {
delay = 0,
Expand Down Expand Up @@ -79,4 +80,62 @@ export function draw(node, {
easing,
css: (t, u) => `stroke-dasharray: ${t * len} ${u * len}`
};
}

export function crossfade({ fallback, ...defaults }) {
let to_receive = new Map();
let to_send = new Map();

function crossfade(from, node, params) {
const {
delay = 0,
duration = d => Math.sqrt(d) * 30,
easing = cubicOut
} = assign(assign({}, defaults), params);

const to = node.getBoundingClientRect();
const dx = from.left - to.left;
const dy = from.top - to.top;

const style = getComputedStyle(node);
const transform = style.transform === 'none' ? '' : style.transform;

return {
delay,
duration: is_function(duration) ? duration(d) : duration,
easing,
css: (t, u) => `
opacity: ${t};
transform: ${transform} translate(${u * dx}px,${u * dy}px);
`
};
}

function transition(items, counterparts, intro) {
return (node, params) => {
items.set(params.key, {
rect: node.getBoundingClientRect()
});

return () => {
if (counterparts.has(params.key)) {
const { rect } = counterparts.get(params.key);
counterparts.delete(params.key);

return crossfade(rect, node, params);
}

// if the node is disappearing altogether
// (i.e. wasn't claimed by the other list)
// then we need to supply an outro
items.delete(params.key);
return fallback(node, params, intro);
};
};
}

return [
transition(to_send, to_receive, false),
transition(to_receive, to_send, true)
];
}

0 comments on commit 394e3e9

Please sign in to comment.