Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="box"></div>

<div id="debug"></div>

<div class="debug" id="before"></div>
<div class="debug" id="after"></div>
              
            
!

CSS

              
                @import "https://unpkg.com/open-props" layer(design.system);
@import "https://unpkg.com/open-props/normalize.min.css" layer(demo.support);

@layer demo {
  ::view-transition {
    pointer-events: none;
  }
  ::view-transition-group(*) {
    animation-duration: 1s;
  }
  ::view-transition-new(*),
  ::view-transition-old(*) {
    width: 100%;
    height: 100%;
    object-fit: fill;
  }
  .box {
    view-transition-name: box;
    z-index: 2;
    overflow: clip;
    user-select: none;
  }
  
  #debug {
    position: fixed;
    opacity: 0.5;
    font-family: monospace;
    padding: 0.2rem;
    white-space: pre-wrap;
    z-index: 2;
  }
  
  .debug {
    position: fixed;
    outline: 1px solid hotpink;
    
    &#before {
      background: #eee;
    }
  }
}

@layer demo.support {
  .box {
    aspect-ratio: 1;
    inline-size: 20vmin;
    background: var(--link);
  }
  
  body {
    display: grid;
    place-content: center;
    padding: var(--size-5);
    gap: var(--size-5);
  }
  
  html {
    view-transition-name: none;
  }
}
              
            
!

JS

              
                const flipIt = true;
const positions = ['start', 'end', 'center'];
const sentences = [
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam in facilisis eros. Donec eget faucibus lectus, sed molestie mi.',
  'Vivamus dignissim nisi sed diam scelerisque tempor et et risus. Aenean tincidunt tempor augue, id varius enim commodo congue.',
  'Sed iaculis placerat luctus. Vivamus sed consectetur dui. Suspendisse lacinia sit amet diam sed gravida. Ut eget dui congue, venenatis erat et, faucibus nisl. Donec a auctor leo.',
  'Duis nisi diam, varius a ipsum faucibus, iaculis bibendum nunc.',
  'Donec quis hendrerit nunc, bibendum feugiat sem. Sed lobortis sem augue, facilisis gravida purus molestie eu. Sed ac ipsum maximus, feugiat mauris non, pretium nisi.',
  'Phasellus elementum scelerisque pulvinar. Ut sagittis egestas enim. Curabitur at condimentum tellus.'
];

function getRandomInt(max) {
  return Math.floor(Math.random() * max)
}

function setRandomAlignments() {
  const curAlignContent = document.body.style.alignContent,
        curJustifyContent = document.body.style.justifyContent;
  let newAlignContent, newJustifyContent;
  do {
    newAlignContent = positions[getRandomInt(3)];
    newJustifyContent = positions[getRandomInt(3)];  
  } while ((newAlignContent === curAlignContent) && (newJustifyContent === curJustifyContent))
  document.body.style.alignContent = newAlignContent;
  document.body.style.justifyContent = newJustifyContent;
}

function randomBetween(min, max) { // min and max included 
  return Math.floor(Math.random() * (max - min + 1) + min);
}

function manipulateBox() {
  const $box= document.querySelector('.box');
  
  $box.style.width = `${randomBetween(5, 30)}vmin`;
  $box.style.height = `${randomBetween(5, 30)}vmin`;
  $box.innerText = sentences[randomBetween(0, sentences.length-1)];
}

document.body.addEventListener('click', async (e) => {
  if (!document.startViewTransition) {
    setRandomAlignments();
    setRandomSize();
  } else {
    const t = document.startViewTransition(() => {
      setRandomAlignments();
      manipulateBox();
    });
    
    if (!flipIt) return;
    
    await t.ready;
    
    // Get boxGroupAnimation
    const vtAnimations = document.getAnimations().filter((anim) => {
			return anim.effect.target === document.documentElement &&
        anim.effect.pseudoElement?.startsWith("::view-transition")
		});
    const boxGroupAnimation = vtAnimations.find((anim) => {
      return anim.effect.pseudoElement == '::view-transition-group(box)';
    });
    
    // Get old position + size by extracting the data from the keyframes
    const boxGroupKeyframes = boxGroupAnimation.effect.getKeyframes();
    const oldMatrix = new DOMMatrix(boxGroupKeyframes[0].transform);

    const rectBefore = {
      width: boxGroupKeyframes[0].width.split('px')[0],
      height: boxGroupKeyframes[0].height.split('px')[0],
      left: oldMatrix.e,
      top: oldMatrix.f,
    };
    
    // Get new position + size by either extacting the data from the keyframes
    // or by forwarding the animation to the end and extracting the values from the getComputedStyle()
    // The latter happens in Chrome because of https://crbug.com/387030974
    let rectAfter;
    if (boxGroupKeyframes[1].transform === 'none') {
      boxGroupAnimation.currentTime = boxGroupAnimation.effect.getTiming().duration;
      const newStyles = window.getComputedStyle(document.documentElement, '::view-transition-group(box)');
      const newMatrix = new DOMMatrix(newStyles.transform);

      rectAfter = {
        width: newStyles.width.split('px')[0],
        height: newStyles.height.split('px')[0],
        left: newMatrix.e,
        top: newMatrix.f,
      };
      boxGroupAnimation.currentTime = 0;
    } else {
      const newMatrix = new DOMMatrix(boxGroupKeyframes[0].transform);
      rectAfter = {
        width: boxGroupKeyframes[1].width.split('px')[0],
        height: boxGroupKeyframes[1].height.split('px')[0],
        left: newMatrix.e,
        top: newMatrix.f,
      };
    }
    
    // debugBox(document.getElementById('before'), rectBefore);
    // debugBox(document.getElementById('after'), rectAfter);
    
    const flip = [
      `translate(${rectBefore.left}px,${rectBefore.top}px) scaleX(${rectBefore.width / rectAfter.width}) scaleY(${rectBefore.height / rectAfter.height})`,
      `translate(${rectAfter.left}px,${rectAfter.top}px) scaleX(1) scaleY(1)`,
    ];
    
    const flipKeyframes = {
      transform: flip,
      transformOrigin: ['0% 0%', '0% 0%'],
      easing: "ease",
    };
    
    
    boxGroupAnimation.effect.setKeyframes(flipKeyframes);
    
    document.getElementById('debug').innerText = JSON.stringify({ flipKeyframes, rectBefore, rectAfter }, null, 4);
  }
});

const debugBox = ($elem, domRect) => {
  $elem.style.left = `${domRect.left}px`;
  $elem.style.top = `${domRect.top}px`;
  $elem.style.width = `${domRect.width}px`;
  $elem.style.height = `${domRect.height}px`;
}
              
            
!
999px

Console