import { createControls, SPEED_PRESETS, type ControlsCallbacks } from "./controls.js"; import { PLAYER_STYLES } from "./styles.js"; const DEFAULT_FPS = 30; const RUNTIME_CDN_URL = "https://cdn.jsdelivr.net/npm/@hyperframes/core/dist/hyperframe.runtime.iife.js"; class HyperframesPlayer extends HTMLElement { static get observedAttributes() { return ["src", "width", "height", "controls", "muted", "poster", "playback-rate"]; } private shadow: ShadowRoot; private container: HTMLDivElement; private iframe: HTMLIFrameElement; private posterEl: HTMLImageElement | null = null; private controlsApi: ReturnType | null = null; private resizeObserver: ResizeObserver; private _ready = false; private _duration = 0; private _currentTime = 0; private _paused = true; private _compositionWidth = 1920; private _compositionHeight = 1080; private _probeInterval: ReturnType | null = null; private _lastUpdateMs = 0; constructor() { super(); this.shadow = this.attachShadow({ mode: "open" }); const style = document.createElement("style"); style.textContent = PLAYER_STYLES; this.shadow.appendChild(style); this.container = document.createElement("div"); this.container.className = "hfp-container"; this.iframe = document.createElement("iframe"); this.iframe.className = "hfp-iframe"; this.iframe.sandbox.add("allow-scripts", "allow-same-origin"); this.iframe.allow = "autoplay; fullscreen"; this.iframe.referrerPolicy = "no-referrer"; this.iframe.title = "HyperFrames Composition"; this.container.appendChild(this.iframe); this.shadow.appendChild(this.container); // Clicking the bare player surface toggles play/pause. // Ignore shadow-DOM control interactions so overlay clicks don't double-handle. this.addEventListener("click", (event) => { if (this._isControlsClick(event)) return; if (this._paused) this.play(); else this.pause(); }); this.resizeObserver = new ResizeObserver(() => this._updateScale()); this._onMessage = this._onMessage.bind(this); this._onIframeLoad = this._onIframeLoad.bind(this); } connectedCallback() { this.resizeObserver.observe(this); window.addEventListener("message", this._onMessage); this.iframe.addEventListener("load", this._onIframeLoad); if (this.hasAttribute("controls")) this._setupControls(); if (this.hasAttribute("poster")) this._setupPoster(); if (this.hasAttribute("src")) this.iframe.src = this.getAttribute("src")!; } disconnectedCallback() { this.resizeObserver.disconnect(); window.removeEventListener("message", this._onMessage); this.iframe.removeEventListener("load", this._onIframeLoad); if (this._probeInterval) clearInterval(this._probeInterval); this.controlsApi?.destroy(); } attributeChangedCallback(name: string, _old: string | null, val: string | null) { switch (name) { case "src": if (val) { this._ready = false; this.iframe.src = val; } break; case "width": this._compositionWidth = parseInt(val || "1920", 10); this._updateScale(); break; case "height": this._compositionHeight = parseInt(val || "1080", 10); this._updateScale(); break; case "controls": if (val !== null) this._setupControls(); else { this.controlsApi?.destroy(); this.controlsApi = null; } break; case "poster": this._setupPoster(); break; case "playback-rate": { const rate = parseFloat(val || "1"); this._sendControl("set-playback-rate", { playbackRate: rate }); this.controlsApi?.updateSpeed(rate); this.dispatchEvent(new Event("ratechange")); break; } case "muted": this._sendControl("set-muted", { muted: val !== null }); break; } } // ── Public API ── /** * Access the inner `