Skip to content

Commit 1609304

Browse files
committed
Morph: get duration and easing as arugments
1 parent 0ed5c5a commit 1609304

2 files changed

Lines changed: 10 additions & 23 deletions

File tree

demo/morph/scripts.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,18 @@ $link.addEventListener('click', async (e) => {
1010
$card.classList.toggle('big');
1111
});
1212

13-
await t.ready;
14-
1513
const css = morph(t, $card, [
1614
"border-radius",
1715
"border-width",
1816
"border-color",
1917
"background-color",
20-
// "aspect-ratio",
21-
// "font-size"
22-
]);
23-
24-
console.log(css);
18+
"aspect-ratio",
19+
"font-size"
20+
], { duration: 2000, easing: 'ease' });
2521

2622
if (css) {
2723
const style = document.createElement("style");
28-
style.textContent = `#card { font-family: monospace; } ${css}`;
24+
style.textContent = css;
2925
document.head.appendChild(style);
3026

3127
try {

src/morph.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,22 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { getAnimations, ViewTransitionPart } from "./animations.js";
6+
77

88
/**
99
* Returns CSS needed to morph an element during a View Transition,
1010
* following the technique described in https://www.bram.us/2025/05/15/view-transitions-border-radius-revisited/
1111
*
1212
* @param vt The active ViewTransition
1313
* @param element The element to apply the morph to
14-
* @param properties A list of CSS properties to transition
14+
* @param options Object containing duration (ms) and easing string
1515
* @returns A string containing the required CSS, or an empty string if it couldn't be generated
1616
*/
1717
export function morph(
1818
vt: ViewTransition,
1919
element: HTMLElement,
20-
properties: string[]
20+
properties: string[],
21+
options: { duration: number; easing: string }
2122
): string {
2223
// 1. Get view-transition-name
2324
const styles = window.getComputedStyle(element);
@@ -34,18 +35,8 @@ export function morph(
3435
console.warn("morph: Element already has a CSS transition applied. It will be overwritten by morph.");
3536
}
3637

37-
// 3. Get the duration and easing from the original ::view-transition-group
38-
const groupAnimations = getAnimations(vt, vtName, ViewTransitionPart.Group);
39-
if (groupAnimations.length === 0) {
40-
console.warn(`morph: Could not find ::view-transition-group animation for ${vtName}. Skipping.`);
41-
return "";
42-
}
43-
44-
const anim = groupAnimations[0];
45-
const effect = anim.effect as KeyframeEffect;
46-
const timing = effect.getComputedTiming();
47-
const durationMs = timing.duration;
48-
const easing = effect.getTiming().easing;
38+
const durationMs = options.duration;
39+
const easing = options.easing;
4940

5041
// 4. Generate the CSS string
5142
if (!element.id) {

0 commit comments

Comments
 (0)