Open in a new tab

Slide a panel with an eased frame animation, and offer a fullscreen toggle that is disabled with a reason when the browser has no Fullscreen API.

Uses @awacloud/fw (Apache-2.0). This example's code is Apache-2.0.

When to use it

Small motion without a CSS transition (a value you compute per frame, a sequence, a cancellable tween), and an element that should go fullscreen where the browser allows it. Chrome, Edge, Firefox and Safari run the fullscreen path from a user gesture; elsewhere the button is disabled with the reason.

Copy-paste steps

  1. npm install @awacloud/fw
  2. Serve node_modules/@awacloud/fw/src/ at /vendor/fw/ (node serve.js from the examples root does this; any static server works).
  3. Copy index.html, main.js and style.css, then open index.html through that server. The ../../_style/examples.css link is optional: copy that file along, or delete the link.
  4. Pick an easing name from fw's easing module (animate brings it along).

What it shows

  • animate(duration, easingName, { init, end }, onFrame, onComplete): a requestAnimationFrame loop handing you the eased value per frame. The button is disabled while a slide runs, so two animations never fight over the same panel.
  • fullscreen.isSupported() first; then toggle(element) and listen.add(name, fn) for enter / exit notifications. toggle returns a promise the browser may reject (a policy, a missing user gesture): the page shows the reason.
  • Feature detection as UI: the button is disabled and says why.

Code

main.js

javascriptApache-2.067 lines

// SPDX-License-Identifier: Apache-2.0
import fw from '@awacloud/fw';
import { animate } from '@awacloud/fw/dom/display/animate.js';
import { fullscreen } from '@awacloud/fw/dom/display/fullscreen.js';

fw.runtime.registerAllDeep([animate, fullscreen]);
const ANIMATE = fw.runtime.resolve('animate');
const FULLSCREEN = fw.runtime.resolve('fullscreen');

export const DISTANCE = 200;
export const DURATION = 300;

const panel = /** @type {HTMLElement} */ (document.getElementById('panel'));
const status = /** @type {HTMLElement} */ (document.getElementById('status'));
const slideButton = /** @type {HTMLButtonElement} */ (document.getElementById('slide'));
let offset = 0;
/** The slide in progress: a second request while it runs gets the same promise. @type {Promise<number> | null} */
let sliding = null;

/**
 * Slide the panel to the other side over `DURATION` ms with an ease-out curve.
 * @returns {Promise<number>} the final offset in px
 */
export function slide() {
    if (sliding) return sliding;
    const from = offset;
    const to = offset === 0 ? DISTANCE : 0;
    status.textContent = 'sliding';
    slideButton.disabled = true;
    sliding = new Promise((resolve) => {
        ANIMATE(DURATION, 'easeOutCubic', { init: from, end: to }, (/** @type {number} */ value) => {
            offset = value;
            panel.style.transform = `translateX(${Math.round(value)}px)`;
        }, () => {
            offset = to;
            sliding = null;
            slideButton.disabled = false;
            status.textContent = `at ${to}px`;
            resolve(to);
        });
    });
    return sliding;
}

/** Feature-detected: a disabled button with a reason beats a broken one. */
export function setupFullscreen() {
    const button = /** @type {HTMLButtonElement} */ (document.getElementById('fullscreen'));
    const reason = /** @type {HTMLElement} */ (document.getElementById('reason'));
    if (!FULLSCREEN.isSupported()) {
        button.disabled = true;
        reason.textContent = 'Fullscreen API not supported in this browser';
        return false;
    }
    button.disabled = false;
    reason.textContent = '';
    // The browser may still refuse (a policy, a missing gesture): say so.
    button.onclick = () => FULLSCREEN.toggle(panel).catch((/** @type {Error} */ err) => {
        reason.textContent = `Fullscreen refused: ${err.message}`;
    });
    FULLSCREEN.listen.add('status', (/** @type {boolean} */ active) => {
        status.textContent = active ? 'fullscreen' : 'windowed';
    });
    return true;
}

slideButton.addEventListener('click', () => slide());
export const fullscreenAvailable = setupFullscreen();

index.html

htmlApache-2.028 lines

<!doctype html>
<!-- SPDX-License-Identifier: Apache-2.0 -->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>fw · Animate a panel and toggle fullscreen</title>
  <link rel="stylesheet" href="../../_style/examples.css">
  <link rel="stylesheet" href="./style.css">
  <script type="importmap">
  { "imports": {
    "@awacloud/fw": "/vendor/fw/main.js", "@awacloud/fw/": "/vendor/fw/" } }
  </script>
  <script type="module" src="./main.js"></script>
</head>
<body>
  <main>
    <h1>Animate a panel and toggle fullscreen</h1>
    <p>
      <button id="slide" type="button">Slide the panel</button>
      <button id="fullscreen" type="button">Toggle fullscreen</button>
      <span id="reason"></span>
    </p>
    <p id="status" aria-live="polite">idle</p>
    <div id="panel" class="panel">Panel</div>
  </main>
</body>
</html>

style.css

cssApache-2.010 lines

/* SPDX-License-Identifier: Apache-2.0 */
.panel {
    inline-size: 8rem;
    padding: 1rem;
    background: #dbeafe;
    color: #0f1720;
    border-radius: 8px;
    will-change: transform;
}
.panel:fullscreen { display: grid; place-items: center; font-size: 2rem; }