Add flip method to examples

pull/2589/head
Jaspreet Singh 7 years ago
parent 1ba42435ac
commit 0d420b4782

@ -1,6 +1,7 @@
<script> <script>
import { quintOut } from 'svelte/easing'; import { quintOut } from 'svelte/easing';
import crossfade from './crossfade.js'; // TODO put this in svelte/transition! import crossfade from './crossfade.js'; // TODO put this in svelte/transition!
import { flip } from './flip.js';
const { send, receive } = crossfade({ const { send, receive } = crossfade({
fallback(node, params) { fallback(node, params) {
@ -13,9 +14,9 @@
css: t => ` css: t => `
transform: ${transform} scale(${t}); transform: ${transform} scale(${t});
opacity: ${t} opacity: ${t}
` `,
}; };
} },
}); });
let todos = [ let todos = [
@ -33,7 +34,7 @@
const todo = { const todo = {
id: uid++, id: uid++,
done: false, done: false,
description: input.value description: input.value,
}; };
todos = [todo, ...todos]; todos = [todo, ...todos];
@ -57,7 +58,8 @@
margin: 0 auto; margin: 0 auto;
} }
.left, .right { .left,
.right {
float: left; float: left;
width: 50%; width: 50%;
padding: 0 1em 0 0; padding: 0 1em 0 0;
@ -83,7 +85,9 @@
user-select: none; user-select: none;
} }
input { margin: 0 } input {
margin: 0;
}
.right label { .right label {
background-color: rgb(180, 240, 100); background-color: rgb(180, 240, 100);
@ -116,6 +120,7 @@
<label <label
in:receive="{{key: todo.id}}" in:receive="{{key: todo.id}}"
out:send="{{key: todo.id}}" out:send="{{key: todo.id}}"
animate:flip
> >
<input type=checkbox bind:checked={todo.done}> <input type=checkbox bind:checked={todo.done}>
{todo.description} {todo.description}
@ -130,6 +135,7 @@
<label <label
in:receive="{{key: todo.id}}" in:receive="{{key: todo.id}}"
out:send="{{key: todo.id}}" out:send="{{key: todo.id}}"
animate:flip
> >
<input type=checkbox bind:checked={todo.done}> <input type=checkbox bind:checked={todo.done}>
{todo.description} {todo.description}

@ -0,0 +1,24 @@
import { cubicOut } from 'svelte/easing';
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: typeof duration === 'function' ? duration(d) : duration,
easing,
css: (t, u) => `transform: ${transform} translate(${u * dx}px, ${u * dy}px);`
};
}

@ -1,6 +1,7 @@
<script> <script>
import { quintOut } from 'svelte/easing'; import { quintOut } from 'svelte/easing';
import crossfade from './crossfade.js'; // TODO put this in svelte/transition! import crossfade from './crossfade.js'; // TODO put this in svelte/transition!
import { flip } from './flip.js';
const { send, receive } = crossfade({ const { send, receive } = crossfade({
fallback(node, params) { fallback(node, params) {
@ -13,9 +14,9 @@
css: t => ` css: t => `
transform: ${transform} scale(${t}); transform: ${transform} scale(${t});
opacity: ${t} opacity: ${t}
` `,
}; };
} },
}); });
let todos = [ let todos = [
@ -33,7 +34,7 @@
const todo = { const todo = {
id: uid++, id: uid++,
done: false, done: false,
description: input.value description: input.value,
}; };
todos = [todo, ...todos]; todos = [todo, ...todos];
@ -63,7 +64,8 @@
margin: 0 auto; margin: 0 auto;
} }
.left, .right { .left,
.right {
float: left; float: left;
width: 50%; width: 50%;
padding: 0 1em 0 0; padding: 0 1em 0 0;
@ -89,7 +91,9 @@
user-select: none; user-select: none;
} }
input { margin: 0 } input {
margin: 0;
}
.right label { .right label {
background-color: rgb(180, 240, 100); background-color: rgb(180, 240, 100);
@ -122,6 +126,7 @@
<label <label
in:receive="{{key: todo.id}}" in:receive="{{key: todo.id}}"
out:send="{{key: todo.id}}" out:send="{{key: todo.id}}"
animate:flip
> >
<input type=checkbox bind:checked={todo.done}> <input type=checkbox bind:checked={todo.done}>
{todo.description} {todo.description}
@ -136,6 +141,7 @@
<label <label
in:receive="{{key: todo.id}}" in:receive="{{key: todo.id}}"
out:send="{{key: todo.id}}" out:send="{{key: todo.id}}"
animate:flip
> >
<input type=checkbox bind:checked={todo.done}> <input type=checkbox bind:checked={todo.done}>
{todo.description} {todo.description}

@ -0,0 +1,24 @@
import { cubicOut } from 'svelte/easing';
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: typeof duration === 'function' ? duration(d) : duration,
easing,
css: (t, u) => `transform: ${transform} translate(${u * dx}px, ${u * dy}px);`
};
}
Loading…
Cancel
Save