@hyperframes/studio 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,198 @@
1
+ var T=Object.defineProperty;var N=(p,e,t)=>e in p?T(p,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):p[e]=t;var h=(p,e,t)=>N(p,typeof e!="symbol"?e+"":e,t);var R=`
2
+ :host {
3
+ display: block;
4
+ position: relative;
5
+ overflow: hidden;
6
+ background: #000;
7
+ contain: layout style;
8
+ }
9
+
10
+ .hfp-container {
11
+ position: absolute;
12
+ inset: 0;
13
+ overflow: hidden;
14
+ pointer-events: none;
15
+ }
16
+
17
+
18
+ .hfp-iframe {
19
+ position: absolute;
20
+ top: 50%;
21
+ left: 50%;
22
+ border: none;
23
+ pointer-events: none;
24
+ }
25
+
26
+ .hfp-poster {
27
+ position: absolute;
28
+ inset: 0;
29
+ object-fit: contain;
30
+ z-index: 1;
31
+ pointer-events: none;
32
+ }
33
+
34
+ /* ── Theming via CSS custom properties ──
35
+ *
36
+ * Override from outside the shadow DOM:
37
+ * hyperframes-player {
38
+ * --hfp-controls-bg: linear-gradient(transparent, rgba(0,0,0,0.9));
39
+ * --hfp-accent: #ff6b6b;
40
+ * --hfp-font: "Inter", sans-serif;
41
+ * }
42
+ */
43
+
44
+ .hfp-controls {
45
+ position: absolute;
46
+ bottom: 0;
47
+ left: 0;
48
+ right: 0;
49
+ display: flex;
50
+ align-items: center;
51
+ gap: var(--hfp-controls-gap, 12px);
52
+ padding: var(--hfp-controls-padding, 8px 16px);
53
+ background: var(--hfp-controls-bg, linear-gradient(transparent, rgba(0, 0, 0, 0.7)));
54
+ color: var(--hfp-color, #fff);
55
+ font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
56
+ font-size: var(--hfp-font-size, 13px);
57
+ z-index: 10;
58
+ pointer-events: auto;
59
+ opacity: 1;
60
+ transition: opacity 0.3s ease;
61
+ user-select: none;
62
+ }
63
+
64
+ .hfp-controls.hfp-hidden {
65
+ opacity: 0;
66
+ pointer-events: none;
67
+ }
68
+
69
+ .hfp-play-btn {
70
+ background: none;
71
+ border: none;
72
+ color: var(--hfp-color, #fff);
73
+ cursor: pointer;
74
+ padding: 8px;
75
+ display: flex;
76
+ align-items: center;
77
+ justify-content: center;
78
+ width: 40px;
79
+ height: 40px;
80
+ flex-shrink: 0;
81
+ z-index: 10;
82
+ }
83
+
84
+ .hfp-play-btn:hover {
85
+ opacity: 0.8;
86
+ }
87
+
88
+ .hfp-play-btn svg,
89
+ .hfp-play-btn svg * {
90
+ pointer-events: none;
91
+ }
92
+
93
+ .hfp-scrubber {
94
+ flex: 1;
95
+ height: var(--hfp-scrubber-height, 4px);
96
+ background: var(--hfp-scrubber-bg, rgba(255, 255, 255, 0.3));
97
+ border-radius: var(--hfp-scrubber-radius, 2px);
98
+ cursor: pointer;
99
+ position: relative;
100
+ }
101
+
102
+ .hfp-scrubber:hover {
103
+ height: var(--hfp-scrubber-height-hover, 6px);
104
+ }
105
+
106
+ .hfp-progress {
107
+ position: absolute;
108
+ top: 0;
109
+ left: 0;
110
+ height: 100%;
111
+ background: var(--hfp-accent, #fff);
112
+ border-radius: var(--hfp-scrubber-radius, 2px);
113
+ pointer-events: none;
114
+ }
115
+
116
+ .hfp-time {
117
+ flex-shrink: 0;
118
+ font-variant-numeric: tabular-nums;
119
+ opacity: 0.9;
120
+ }
121
+
122
+ .hfp-speed-wrap {
123
+ position: relative;
124
+ flex-shrink: 0;
125
+ }
126
+
127
+ .hfp-speed-btn {
128
+ background: var(--hfp-speed-btn-bg, rgba(255, 255, 255, 0.15));
129
+ border: none;
130
+ border-radius: var(--hfp-speed-btn-radius, 4px);
131
+ color: var(--hfp-color, #fff);
132
+ cursor: pointer;
133
+ font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
134
+ font-size: 12px;
135
+ font-variant-numeric: tabular-nums;
136
+ font-weight: 600;
137
+ padding: 4px 8px;
138
+ min-width: 40px;
139
+ text-align: center;
140
+ transition: background 0.15s ease;
141
+ }
142
+
143
+ .hfp-speed-btn:hover {
144
+ background: var(--hfp-speed-btn-bg-hover, rgba(255, 255, 255, 0.3));
145
+ }
146
+
147
+ .hfp-speed-menu {
148
+ position: absolute;
149
+ bottom: calc(100% + 8px);
150
+ right: 0;
151
+ background: var(--hfp-menu-bg, rgba(20, 20, 20, 0.95));
152
+ backdrop-filter: blur(12px);
153
+ -webkit-backdrop-filter: blur(12px);
154
+ border: 1px solid var(--hfp-menu-border, rgba(255, 255, 255, 0.1));
155
+ border-radius: var(--hfp-menu-radius, 8px);
156
+ padding: 4px;
157
+ display: flex;
158
+ flex-direction: column;
159
+ gap: 2px;
160
+ min-width: 80px;
161
+ opacity: 0;
162
+ visibility: hidden;
163
+ transform: translateY(4px);
164
+ transition: opacity 0.15s ease, transform 0.15s ease, visibility 0.15s;
165
+ box-shadow: var(--hfp-menu-shadow, 0 8px 24px rgba(0, 0, 0, 0.4));
166
+ }
167
+
168
+ .hfp-speed-menu.hfp-open {
169
+ opacity: 1;
170
+ visibility: visible;
171
+ transform: translateY(0);
172
+ }
173
+
174
+ .hfp-speed-option {
175
+ background: none;
176
+ border: none;
177
+ border-radius: 4px;
178
+ color: var(--hfp-menu-color, rgba(255, 255, 255, 0.7));
179
+ cursor: pointer;
180
+ font-family: var(--hfp-font, system-ui, -apple-system, sans-serif);
181
+ font-size: 13px;
182
+ font-variant-numeric: tabular-nums;
183
+ padding: 6px 12px;
184
+ text-align: left;
185
+ transition: background 0.1s ease, color 0.1s ease;
186
+ white-space: nowrap;
187
+ }
188
+
189
+ .hfp-speed-option:hover {
190
+ background: var(--hfp-menu-hover-bg, rgba(255, 255, 255, 0.1));
191
+ color: var(--hfp-color, #fff);
192
+ }
193
+
194
+ .hfp-speed-option.hfp-active {
195
+ color: var(--hfp-accent, #fff);
196
+ font-weight: 600;
197
+ }
198
+ `,I='<svg width="24" height="24" viewBox="0 0 18 18" fill="currentColor"><polygon points="4,2 16,9 4,16"/></svg>',F='<svg width="24" height="24" viewBox="0 0 18 18" fill="currentColor"><rect x="3" y="2" width="4" height="14"/><rect x="11" y="2" width="4" height="14"/></svg>',O=[.25,.5,1,1.5,2,4];function y(p){return Number.isInteger(p)?`${p}x`:`${p}x`}function S(p){if(!Number.isFinite(p)||p<0)return"0:00";let e=Math.floor(p),t=Math.floor(e/60),s=e%60;return`${t}:${s.toString().padStart(2,"0")}`}function j(p,e,t={}){let s=t.speedPresets??O,i=document.createElement("div");i.className="hfp-controls",i.addEventListener("click",r=>{r.stopPropagation()});let a=document.createElement("button");a.className="hfp-play-btn",a.type="button",a.innerHTML=I,a.setAttribute("aria-label","Play");let o=document.createElement("div");o.className="hfp-scrubber";let d=document.createElement("div");d.className="hfp-progress",d.style.width="0%",o.appendChild(d);let u=document.createElement("span");u.className="hfp-time",u.textContent="0:00 / 0:00";let m=document.createElement("div");m.className="hfp-speed-wrap";let l=document.createElement("button");l.className="hfp-speed-btn",l.type="button",l.textContent="1x",l.setAttribute("aria-label","Playback speed");let c=document.createElement("div");c.className="hfp-speed-menu",c.setAttribute("role","menu");for(let r of s){let n=document.createElement("button");n.className="hfp-speed-option",n.type="button",n.setAttribute("role","menuitem"),n.dataset.speed=String(r),n.textContent=y(r),r===1&&n.classList.add("hfp-active"),c.appendChild(n)}m.appendChild(c),m.appendChild(l),i.appendChild(a),i.appendChild(o),i.appendChild(u),i.appendChild(m),p.appendChild(i);let v=!1,b=null;s.indexOf(1),a.addEventListener("click",r=>{r.stopPropagation(),v?e.onPause():e.onPlay()});let x=r=>{for(let n of c.querySelectorAll(".hfp-speed-option"))n.classList.toggle("hfp-active",n.dataset.speed===String(r))};l.addEventListener("click",r=>{r.stopPropagation();let n=c.classList.toggle("hfp-open");l.setAttribute("aria-expanded",String(n))}),c.addEventListener("click",r=>{r.stopPropagation();let n=r.target.closest(".hfp-speed-option");if(!n)return;let f=parseFloat(n.dataset.speed);s.indexOf(f),l.textContent=y(f),x(f),c.classList.remove("hfp-open"),l.setAttribute("aria-expanded","false"),e.onSpeedChange(f)});let w=()=>{c.classList.remove("hfp-open"),l.setAttribute("aria-expanded","false")};document.addEventListener("click",w);let _=r=>{let n=o.getBoundingClientRect(),f=Math.max(0,Math.min(1,(r-n.left)/n.width));e.onSeek(f)},g=!1;o.addEventListener("mousedown",r=>{r.stopPropagation(),g=!0,_(r.clientX)});let k=r=>{g&&_(r.clientX)},A=()=>{g=!1};document.addEventListener("mousemove",k),document.addEventListener("mouseup",A),o.addEventListener("touchstart",r=>{g=!0;let n=r.touches[0];n&&_(n.clientX)},{passive:!0});let C=r=>{if(g){let n=r.touches[0];n&&_(n.clientX)}},P=()=>{g=!1};document.addEventListener("touchmove",C,{passive:!0}),document.addEventListener("touchend",P);let M=()=>{b&&clearTimeout(b),b=setTimeout(()=>{v&&i.classList.add("hfp-hidden")},3e3)},L=p instanceof ShadowRoot?p.host:p;return L.addEventListener("mousemove",()=>{i.classList.remove("hfp-hidden"),M()}),L.addEventListener("mouseleave",()=>{v&&i.classList.add("hfp-hidden")}),{updateTime(r,n){let f=n>0?r/n*100:0;d.style.width=`${f}%`,u.textContent=`${S(r)} / ${S(n)}`},updatePlaying(r){v=r,a.innerHTML=r?F:I,a.setAttribute("aria-label",r?"Pause":"Play"),r?M():i.classList.remove("hfp-hidden")},updateSpeed(r){s.indexOf(r),l.textContent=y(r),x(r)},show(){i.style.display=""},hide(){i.style.display="none"},destroy(){document.removeEventListener("mousemove",k),document.removeEventListener("mouseup",A),document.removeEventListener("touchmove",C),document.removeEventListener("touchend",P),document.removeEventListener("click",w),b&&clearTimeout(b)}}}var E=30,z="https://cdn.jsdelivr.net/npm/@hyperframes/core/dist/hyperframe.runtime.iife.js",D=class extends HTMLElement{constructor(){super();h(this,"shadow");h(this,"container");h(this,"iframe");h(this,"posterEl",null);h(this,"controlsApi",null);h(this,"resizeObserver");h(this,"_ready",!1);h(this,"_duration",0);h(this,"_currentTime",0);h(this,"_paused",!0);h(this,"_compositionWidth",1920);h(this,"_compositionHeight",1080);h(this,"_probeInterval",null);h(this,"_lastUpdateMs",0);h(this,"_parentMedia",[]);h(this,"_runtimeInjected",!1);this.shadow=this.attachShadow({mode:"open"});let e=document.createElement("style");e.textContent=R,this.shadow.appendChild(e),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),this.addEventListener("click",t=>{this._isControlsClick(t)||(this._paused?this.play():this.pause())}),this.resizeObserver=new ResizeObserver(()=>this._updateScale()),this._onMessage=this._onMessage.bind(this),this._onIframeLoad=this._onIframeLoad.bind(this)}static get observedAttributes(){return["src","width","height","controls","muted","poster","playback-rate","audio-src"]}connectedCallback(){this.resizeObserver.observe(this),window.addEventListener("message",this._onMessage),this.iframe.addEventListener("load",this._onIframeLoad),this.hasAttribute("controls")&&this._setupControls(),this.hasAttribute("poster")&&this._setupPoster(),this.hasAttribute("audio-src")&&this._setupParentAudioFromUrl(this.getAttribute("audio-src")),this.hasAttribute("src")&&(this.iframe.src=this.getAttribute("src"))}disconnectedCallback(){var e;this.resizeObserver.disconnect(),window.removeEventListener("message",this._onMessage),this.iframe.removeEventListener("load",this._onIframeLoad),this._probeInterval&&clearInterval(this._probeInterval),(e=this.controlsApi)==null||e.destroy();for(let t of this._parentMedia)t.el.pause(),t.el.src="";this._parentMedia=[]}attributeChangedCallback(e,t,s){var i,a;switch(e){case"src":s&&(this._ready=!1,this.iframe.src=s);break;case"width":this._compositionWidth=parseInt(s||"1920",10),this._updateScale();break;case"height":this._compositionHeight=parseInt(s||"1080",10),this._updateScale();break;case"controls":s!==null?this._setupControls():((i=this.controlsApi)==null||i.destroy(),this.controlsApi=null);break;case"poster":this._setupPoster();break;case"playback-rate":{let o=parseFloat(s||"1");for(let d of this._parentMedia)d.el.playbackRate=o;this._sendControl("set-playback-rate",{playbackRate:o}),(a=this.controlsApi)==null||a.updateSpeed(o),this.dispatchEvent(new Event("ratechange"));break}case"muted":for(let o of this._parentMedia)o.el.muted=s!==null;this._sendControl("set-muted",{muted:s!==null});break;case"audio-src":s&&this._setupParentAudioFromUrl(s);break}}get iframeElement(){return this.iframe}play(){var e;this._hidePoster(),this._playParentMedia(),this._sendControl("play"),this._paused=!1,(e=this.controlsApi)==null||e.updatePlaying(!0),this.dispatchEvent(new Event("play"))}pause(){var e;this._pauseParentMedia(),this._sendControl("pause"),this._paused=!0,(e=this.controlsApi)==null||e.updatePlaying(!1),this.dispatchEvent(new Event("pause"))}seek(e){var s,i;let t=Math.round(e*E);this._sendControl("seek",{frame:t}),this._currentTime=e;for(let a of this._parentMedia){let o=e-a.start;o>=0&&o<a.duration&&(a.el.currentTime=o)}this._paused=!0,(s=this.controlsApi)==null||s.updatePlaying(!1),(i=this.controlsApi)==null||i.updateTime(this._currentTime,this._duration)}get currentTime(){return this._currentTime}set currentTime(e){this.seek(e)}get duration(){return this._duration}get paused(){return this._paused}get ready(){return this._ready}get playbackRate(){return parseFloat(this.getAttribute("playback-rate")||"1")}set playbackRate(e){this.setAttribute("playback-rate",String(e))}get muted(){return this.hasAttribute("muted")}set muted(e){e?this.setAttribute("muted",""):this.removeAttribute("muted")}get loop(){return this.hasAttribute("loop")}set loop(e){e?this.setAttribute("loop",""):this.removeAttribute("loop")}_sendControl(e,t={}){var s;try{(s=this.iframe.contentWindow)==null||s.postMessage({source:"hf-parent",type:"control",action:e,...t},"*")}catch{}}_isControlsClick(e){return e.composedPath().some(t=>t instanceof HTMLElement&&t.classList.contains("hfp-controls"))}_onMessage(e){var s,i,a,o;if(e.source!==this.iframe.contentWindow)return;let t=e.data;if(!(!t||t.source!=="hf-preview")){if(t.type==="state"){this._currentTime=(t.frame??0)/E;let d=!this._paused;this._paused=!t.isPlaying,d&&this._paused?this._pauseParentMedia():!d&&!this._paused&&this._playParentMedia();let u=performance.now();(u-this._lastUpdateMs>100||this._paused!==d)&&(this._lastUpdateMs=u,(s=this.controlsApi)==null||s.updateTime(this._currentTime,this._duration),(i=this.controlsApi)==null||i.updatePlaying(!this._paused),this.dispatchEvent(new CustomEvent("timeupdate",{detail:{currentTime:this._currentTime}}))),this._currentTime>=this._duration&&!this._paused&&(this._pauseParentMedia(),this.loop?(this.seek(0),this.play()):(this._paused=!0,(a=this.controlsApi)==null||a.updatePlaying(!1),this.dispatchEvent(new Event("ended"))))}t.type==="timeline"&&t.durationInFrames>0&&Number.isFinite(t.durationInFrames)&&(this._duration=t.durationInFrames/E,(o=this.controlsApi)==null||o.updateTime(this._currentTime,this._duration)),t.type==="stage-size"&&t.width>0&&t.height>0&&(this._compositionWidth=t.width,this._compositionHeight=t.height,this._updateScale())}}_onIframeLoad(){let e=0;this._runtimeInjected=!1,this._probeInterval&&clearInterval(this._probeInterval),this._probeInterval=setInterval(()=>{var t,s;e++;try{let i=this.iframe.contentWindow;if(!i)return;let a=!!(i.__hf||i.__player),o=!!(i.__timelines&&Object.keys(i.__timelines).length>0);if(!a&&o&&!this._runtimeInjected&&e>=5){this._injectRuntime();return}if(this._runtimeInjected&&!a)return;let d=(()=>{var u,m;if(i.__player&&typeof i.__player.getDuration=="function")return i.__player;if(i.__timelines){let l=Object.keys(i.__timelines);if(l.length>0){let c=(m=(u=this.iframe.contentDocument)==null?void 0:u.querySelector("[data-composition-id]"))==null?void 0:m.getAttribute("data-composition-id"),v=c&&c in i.__timelines?c:l[l.length-1],b=i.__timelines[v];return{getDuration:()=>b.duration()}}}return null})();if(d&&d.getDuration()>0){clearInterval(this._probeInterval),this._duration=d.getDuration(),this._ready=!0,(t=this.controlsApi)==null||t.updateTime(0,this._duration),this.dispatchEvent(new CustomEvent("ready",{detail:{duration:this._duration}}));let u=(s=this.iframe.contentDocument)==null?void 0:s.querySelector("[data-composition-id]");if(u){let m=parseInt(u.getAttribute("data-width")||"0",10),l=parseInt(u.getAttribute("data-height")||"0",10);m>0&&l>0&&(this._compositionWidth=m,this._compositionHeight=l,this._updateScale())}this._setupParentMedia(),this.hasAttribute("autoplay")&&this.play();return}}catch{}e>=40&&(clearInterval(this._probeInterval),this.dispatchEvent(new CustomEvent("error",{detail:{message:"Composition timeline not found after 8s"}})))},200)}_injectRuntime(){this._runtimeInjected=!0;try{let e=this.iframe.contentDocument;if(!e)return;let t=e.createElement("script");t.src=z,t.onload=()=>{},t.onerror=()=>{},(e.head||e.documentElement).appendChild(t)}catch{}}_updateScale(){let e=this.getBoundingClientRect();if(e.width===0||e.height===0)return;let t=Math.min(e.width/this._compositionWidth,e.height/this._compositionHeight);this.iframe.style.width=`${this._compositionWidth}px`,this.iframe.style.height=`${this._compositionHeight}px`,this.iframe.style.transform=`translate(-50%, -50%) scale(${t})`}_setupControls(){if(this.controlsApi)return;let e={onPlay:()=>this.play(),onPause:()=>this.pause(),onSeek:i=>this.seek(i*this._duration),onSpeedChange:i=>{this.playbackRate=i}},t=this.getAttribute("speed-presets"),s=t?t.split(",").map(Number).filter(i=>!isNaN(i)&&i>0):void 0;this.controlsApi=j(this.shadow,e,{speedPresets:s})}_setupPoster(){var t;let e=this.getAttribute("poster");if(!e){(t=this.posterEl)==null||t.remove(),this.posterEl=null;return}this.posterEl||(this.posterEl=document.createElement("img"),this.posterEl.className="hfp-poster",this.shadow.appendChild(this.posterEl)),this.posterEl.src=e}_playParentMedia(){for(let e of this._parentMedia)e.el.src&&e.el.play().then(()=>{this._muteIframeMedia()}).catch(()=>{})}_muteIframeMedia(){try{let e=this.iframe.contentDocument;if(!e)return;let t=e.querySelectorAll("audio[data-start], video[data-start]");for(let s of t)s.volume=0}catch{}}_pauseParentMedia(){for(let e of this._parentMedia)e.el.pause()}_createParentMedia(e,t,s,i){if(this._parentMedia.some(o=>o.el.src===e))return;let a=t==="video"?document.createElement("video"):new Audio;a.preload="auto",a.src=e,a.load(),a.muted=this.muted,this.playbackRate!==1&&(a.playbackRate=this.playbackRate),this._parentMedia.push({el:a,start:s,duration:i})}_setupParentAudioFromUrl(e){this._createParentMedia(e,"audio",0,1/0)}_setupParentMedia(){var e;try{let t=this.iframe.contentDocument;if(!t)return;let s=t.querySelectorAll("audio[data-start], video[data-start]");for(let i of s){let a=i.getAttribute("src")||((e=i.querySelector("source"))==null?void 0:e.src);if(!a)continue;let o=parseFloat(i.getAttribute("data-start")||"0"),d=parseFloat(i.getAttribute("data-duration")||"Infinity"),u=i.tagName==="VIDEO"?"video":"audio";this._createParentMedia(a,u,o,d)}}catch{}}_hidePoster(){var e;(e=this.posterEl)==null||e.remove(),this.posterEl=null}};customElements.get("hyperframes-player")||customElements.define("hyperframes-player",D);export{D as HyperframesPlayer,O as SPEED_PRESETS,y as formatSpeed,S as formatTime};