Skip to content

Commit 08620b7

Browse files
vanceingallsclaude
andcommitted
feat(registry): make caption-pill-karaoke data-driven via caption-data runtime
Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent c67e9dc commit 08620b7

1 file changed

Lines changed: 289 additions & 70 deletions

File tree

‎registry/components/caption-pill-karaoke/caption-pill-karaoke.html‎

Lines changed: 289 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -131,56 +131,199 @@
131131
</div>
132132

133133
<script>
134-
var DURATION = 8;
135-
var SAFE_ZONE_WIDTH = 1400;
136-
var PILL_MAX_WIDTH = 1400;
134+
var HF_COMPOSITION_ID = "caption-pill-karaoke";
135+
var HF_ROOT_ID = "pill-karaoke";
136+
var HF_GROUP_SELECTOR = ".caption-group";
137+
138+
var HF_DEMO_DATA = {
139+
version: 1,
140+
segments: [
141+
{
142+
start: 0,
143+
text: "Every great video starts with a single frame. HyperFrames lets you write HTML and render professional video. No timeline. No drag and drop. Just code that becomes cinema.",
144+
words: [
145+
{ text: "Every", start: 0.0, end: 0.3 },
146+
{ text: "great", start: 0.3, end: 0.55 },
147+
{ text: "video", start: 0.55, end: 0.85 },
148+
{ text: "starts", start: 0.85, end: 1.1 },
149+
{ text: "with", start: 1.1, end: 1.25 },
150+
{ text: "a", start: 1.25, end: 1.35 },
151+
{ text: "single", start: 1.35, end: 1.65 },
152+
{ text: "frame.", start: 1.65, end: 2.0 },
153+
{ text: "HyperFrames", start: 2.1, end: 2.65 },
154+
{ text: "lets", start: 2.65, end: 2.85 },
155+
{ text: "you", start: 2.85, end: 2.95 },
156+
{ text: "write", start: 2.95, end: 3.15 },
157+
{ text: "HTML", start: 3.15, end: 3.55 },
158+
{ text: "and", start: 3.55, end: 3.65 },
159+
{ text: "render", start: 3.65, end: 3.95 },
160+
{ text: "professional", start: 3.95, end: 4.45 },
161+
{ text: "video.", start: 4.45, end: 4.8 },
162+
{ text: "No", start: 4.9, end: 5.05 },
163+
{ text: "timeline.", start: 5.05, end: 5.45 },
164+
{ text: "No", start: 5.5, end: 5.65 },
165+
{ text: "drag", start: 5.65, end: 5.85 },
166+
{ text: "and", start: 5.85, end: 5.95 },
167+
{ text: "drop.", start: 5.95, end: 6.25 },
168+
{ text: "Just", start: 6.35, end: 6.55 },
169+
{ text: "code", start: 6.55, end: 6.8 },
170+
{ text: "that", start: 6.8, end: 6.95 },
171+
{ text: "becomes", start: 6.95, end: 7.3 },
172+
{ text: "cinema.", start: 7.3, end: 7.7 },
173+
],
174+
},
175+
],
176+
};
177+
178+
/* ── HyperFrames caption-data runtime (shared across caption templates) ── */
179+
var HF_CONTRACT_VERSION = 1;
180+
var HF_DEFAULT_RESOLUTION = { width: 1920, height: 1080 };
181+
182+
function hfFlattenSegments(segments) {
183+
var out = [];
184+
(segments || []).forEach(function (seg) {
185+
((seg && seg.words) || []).forEach(function (w) {
186+
var start = Math.max(0, Number(w.start) || 0);
187+
out.push({
188+
text: String(w.word || w.text || "").trim(),
189+
start: start,
190+
end: Math.max(start, Number(w.end) || 0),
191+
});
192+
});
193+
});
194+
out.sort(function (a, b) {
195+
return a.start - b.start;
196+
});
197+
return out.filter(function (w) {
198+
return w.text.length > 0;
199+
});
200+
}
201+
202+
function hfDeriveDuration(words) {
203+
var end = 0;
204+
words.forEach(function (w) {
205+
if (w.end > end) end = w.end;
206+
});
207+
return end;
208+
}
209+
210+
function hfValidate(data) {
211+
if (!data || typeof data !== "object") return { ok: false, reason: "not-an-object" };
212+
if (data.version != null && Math.floor(Number(data.version)) > HF_CONTRACT_VERSION) {
213+
return { ok: false, reason: "unsupported-version" };
214+
}
215+
if (!Array.isArray(data.segments) || data.segments.length === 0) {
216+
return { ok: false, reason: "no-segments" };
217+
}
218+
if (data.displayMode != null && data.displayMode !== "full") {
219+
return { ok: false, reason: "unsupported-displayMode" };
220+
}
221+
var words = hfFlattenSegments(data.segments);
222+
if (words.length === 0) return { ok: false, reason: "no-words" };
223+
return { ok: true, words: words, durationS: hfDeriveDuration(words) };
224+
}
225+
226+
function hfApplyStageConfig(data, durationS) {
227+
var res = (data && data.resolution) || HF_DEFAULT_RESOLUTION;
228+
var W = Math.max(1, Math.round(Number(res.width) || HF_DEFAULT_RESOLUTION.width));
229+
var H = Math.max(1, Math.round(Number(res.height) || HF_DEFAULT_RESOLUTION.height));
230+
var root = document.getElementById(HF_ROOT_ID);
231+
[document.documentElement, document.body, root].forEach(function (el) {
232+
el.style.width = W + "px";
233+
el.style.height = H + "px";
234+
});
235+
root.setAttribute("data-width", String(W));
236+
root.setAttribute("data-height", String(H));
237+
root.setAttribute("data-duration", String(durationS));
238+
var brand = (data && data.brand) || {};
239+
if (typeof brand.primaryColor === "string" && brand.primaryColor) {
240+
root.style.setProperty("--hf-caption-primary", brand.primaryColor);
241+
}
242+
if (typeof brand.accentColor === "string" && brand.accentColor) {
243+
root.style.setProperty("--hf-caption-accent", brand.accentColor);
244+
}
245+
return { W: W, H: H, scaleX: W / 1920, scaleY: H / 1080, fontScale: Math.min(W, H) / 1080 };
246+
}
247+
248+
function hfPublishStatus(status) {
249+
window.__HF_CAPTION_STATUS = status;
250+
document
251+
.getElementById(HF_ROOT_ID)
252+
.setAttribute("data-caption-status", status.ok ? "ok" : "failed");
253+
}
254+
255+
function hfRunGate(words, durationS, failReason) {
256+
if (failReason) {
257+
hfPublishStatus({
258+
ok: false,
259+
present: false,
260+
contained: false,
261+
reason: failReason,
262+
version: HF_CONTRACT_VERSION,
263+
durationS: durationS || 0,
264+
});
265+
return;
266+
}
267+
var root = document.getElementById(HF_ROOT_ID);
268+
var rootRect = root.getBoundingClientRect();
269+
var groups = root.querySelectorAll(HF_GROUP_SELECTOR);
270+
var present = false;
271+
var contained = true;
272+
var tokens = [];
273+
words.forEach(function (w) {
274+
var t = w.text.toLowerCase().replace(/[^a-z0-9]/g, "");
275+
if (t) tokens.push(t);
276+
});
277+
groups.forEach(function (el) {
278+
var prevOpacity = el.style.opacity;
279+
var prevVisibility = el.style.visibility;
280+
el.style.opacity = "1";
281+
el.style.visibility = "visible";
282+
var r = el.getBoundingClientRect();
283+
if (r.width > 0 && r.height > 0) {
284+
var text = (el.textContent || "").toLowerCase();
285+
for (var i = 0; i < tokens.length; i++) {
286+
if (text.indexOf(tokens[i]) !== -1) {
287+
present = true;
288+
break;
289+
}
290+
}
291+
if (
292+
r.left < rootRect.left - 1 ||
293+
r.right > rootRect.right + 1 ||
294+
r.top < rootRect.top - 1 ||
295+
r.bottom > rootRect.bottom + 1
296+
) {
297+
contained = false;
298+
}
299+
}
300+
el.style.opacity = prevOpacity;
301+
el.style.visibility = prevVisibility;
302+
});
303+
hfPublishStatus({
304+
ok: present && contained,
305+
present: present,
306+
contained: contained,
307+
version: HF_CONTRACT_VERSION,
308+
durationS: durationS,
309+
});
310+
}
311+
312+
var DURATION, SAFE_ZONE_WIDTH, PILL_MAX_WIDTH, BASE_FONT_SIZE, MIN_FONT_SIZE, WORD_SPACING;
137313
var PILL_MIN_PADDING_X = 30;
138314
var PILL_MAX_PADDING_X = 60;
139315
var FONT_WIDTH_SAFETY = 1.18;
140316
var FONT_FAMILY = "Poppins";
141317
var FONT_WEIGHT = "700";
142-
var BASE_FONT_SIZE = 72;
143-
var MIN_FONT_SIZE = 52;
144318
var MAX_WORDS_PER_GROUP = 4;
145-
var WORD_SPACING = 16;
146319
var PAUSE_THRESHOLD = 0.1;
147320
var GROUP_END_BUFFER = 0.3;
148321
var COLOR_INACTIVE = "#A6A6A6";
149322
var COLOR_ACTIVE = "#1C1E1D";
323+
var hfColorActive = COLOR_ACTIVE;
150324
var COLOR_FADE_DURATION = 0.1;
151325
var WORD_FADE_LEAD = 0.05;
152326

153-
var TRANSCRIPT = [
154-
{ text: "Every", start: 0.0, end: 0.3 },
155-
{ text: "great", start: 0.3, end: 0.55 },
156-
{ text: "video", start: 0.55, end: 0.85 },
157-
{ text: "starts", start: 0.85, end: 1.1 },
158-
{ text: "with", start: 1.1, end: 1.25 },
159-
{ text: "a", start: 1.25, end: 1.35 },
160-
{ text: "single", start: 1.35, end: 1.65 },
161-
{ text: "frame.", start: 1.65, end: 2.0 },
162-
{ text: "HyperFrames", start: 2.1, end: 2.65 },
163-
{ text: "lets", start: 2.65, end: 2.85 },
164-
{ text: "you", start: 2.85, end: 2.95 },
165-
{ text: "write", start: 2.95, end: 3.15 },
166-
{ text: "HTML", start: 3.15, end: 3.55 },
167-
{ text: "and", start: 3.55, end: 3.65 },
168-
{ text: "render", start: 3.65, end: 3.95 },
169-
{ text: "professional", start: 3.95, end: 4.45 },
170-
{ text: "video.", start: 4.45, end: 4.8 },
171-
{ text: "No", start: 4.9, end: 5.05 },
172-
{ text: "timeline.", start: 5.05, end: 5.45 },
173-
{ text: "No", start: 5.5, end: 5.65 },
174-
{ text: "drag", start: 5.65, end: 5.85 },
175-
{ text: "and", start: 5.85, end: 5.95 },
176-
{ text: "drop.", start: 5.95, end: 6.25 },
177-
{ text: "Just", start: 6.35, end: 6.55 },
178-
{ text: "code", start: 6.55, end: 6.8 },
179-
{ text: "that", start: 6.8, end: 6.95 },
180-
{ text: "becomes", start: 6.95, end: 7.3 },
181-
{ text: "cinema.", start: 7.3, end: 7.7 },
182-
];
183-
184327
function normalizeWords(rawWords) {
185328
return rawWords
186329
.map(function (item) {
@@ -322,6 +465,7 @@
322465
});
323466
var layout = planPillLayout(indexedWords, fontSize);
324467
pillEl.style.width = layout.pillWidth + "px";
468+
pillEl.style.maxWidth = PILL_MAX_WIDTH + "px";
325469
pillEl.style.paddingLeft = layout.padX + "px";
326470
pillEl.style.paddingRight = layout.padX + "px";
327471
layout.lines.slice(0, 2).forEach(function (lineWords) {
@@ -342,41 +486,116 @@
342486
});
343487
}
344488

345-
var WORDS = normalizeWords(TRANSCRIPT);
346-
var GROUPS = makeGroups(WORDS);
347-
buildCaptions(GROUPS);
348-
349-
window.__timelines = window.__timelines || {};
350-
var tl = gsap.timeline({ paused: true });
351-
352-
GROUPS.forEach(function (group, groupIndex) {
353-
var groupEl = document.getElementById("caption-group-" + groupIndex);
354-
var nextGroup = GROUPS[groupIndex + 1];
355-
var isLastGroup = groupIndex === GROUPS.length - 1;
356-
var effectiveEnd = isLastGroup
357-
? DURATION
358-
: Math.min(nextGroup.start, group.end + GROUP_END_BUFFER);
359-
var visibleStart = Math.max(0, group.start);
360-
var visibleEnd = Math.max(visibleStart + 0.01, effectiveEnd);
361-
362-
tl.set(groupEl, { opacity: 1 }, visibleStart);
363-
tl.set(groupEl, { opacity: 0 }, visibleEnd);
364-
365-
group.words.forEach(function (word, wordIndex) {
366-
var wordEl = document.getElementById("caption-word-" + groupIndex + "-" + wordIndex);
367-
var isFirstWord = wordIndex === 0;
368-
var wordStart = Math.max(visibleStart, word.start - WORD_FADE_LEAD);
369-
tl.set(wordEl, { color: isFirstWord ? COLOR_ACTIVE : COLOR_INACTIVE }, visibleStart);
370-
if (isFirstWord) return;
371-
tl.to(
372-
wordEl,
373-
{ color: COLOR_ACTIVE, duration: COLOR_FADE_DURATION, ease: "none" },
374-
wordStart,
375-
);
489+
function hfTeardown() {
490+
document.getElementById("caption-stage").innerHTML = "";
491+
}
492+
493+
function hfBuild(words, layout, durationS) {
494+
DURATION = durationS;
495+
BASE_FONT_SIZE = Math.round(72 * layout.fontScale);
496+
MIN_FONT_SIZE = Math.round(52 * layout.fontScale);
497+
SAFE_ZONE_WIDTH = Math.round(1600 * layout.scaleX);
498+
PILL_MAX_WIDTH = SAFE_ZONE_WIDTH;
499+
WORD_SPACING = Math.round(16 * layout.fontScale);
500+
501+
var stage = document.getElementById("caption-stage");
502+
var stageHeight = Math.round(220 * layout.fontScale);
503+
stage.style.width = SAFE_ZONE_WIDTH + "px";
504+
stage.style.height = stageHeight + "px";
505+
stage.style.bottom = Math.round(90 * layout.scaleY) + "px";
506+
507+
var rootEl = document.getElementById(HF_ROOT_ID);
508+
var accent = getComputedStyle(rootEl).getPropertyValue("--hf-caption-accent").trim();
509+
hfColorActive = accent || COLOR_ACTIVE;
510+
511+
var WORDS = normalizeWords(words);
512+
var GROUPS = makeGroups(WORDS);
513+
buildCaptions(GROUPS);
514+
GROUPS.forEach(function (_, i) {
515+
var el = document.getElementById("caption-group-" + i);
516+
el.style.width = SAFE_ZONE_WIDTH + "px";
517+
el.style.height = stageHeight + "px";
376518
});
377-
});
378519

379-
window.__timelines["caption-pill-karaoke"] = tl;
520+
var tl = gsap.timeline({ paused: true });
521+
522+
GROUPS.forEach(function (group, groupIndex) {
523+
var groupEl = document.getElementById("caption-group-" + groupIndex);
524+
var nextGroup = GROUPS[groupIndex + 1];
525+
var isLastGroup = groupIndex === GROUPS.length - 1;
526+
var effectiveEnd = isLastGroup
527+
? DURATION
528+
: Math.min(nextGroup.start, group.end + GROUP_END_BUFFER);
529+
var visibleStart = Math.max(0, group.start);
530+
var visibleEnd = Math.max(visibleStart + 0.01, effectiveEnd);
531+
532+
tl.set(groupEl, { opacity: 1 }, visibleStart);
533+
tl.set(groupEl, { opacity: 0 }, visibleEnd);
534+
535+
group.words.forEach(function (word, wordIndex) {
536+
var wordEl = document.getElementById("caption-word-" + groupIndex + "-" + wordIndex);
537+
var isFirstWord = wordIndex === 0;
538+
var wordStart = Math.max(visibleStart, word.start - WORD_FADE_LEAD);
539+
tl.set(wordEl, { color: isFirstWord ? hfColorActive : COLOR_INACTIVE }, visibleStart);
540+
if (isFirstWord) return;
541+
tl.to(
542+
wordEl,
543+
{ color: hfColorActive, duration: COLOR_FADE_DURATION, ease: "none" },
544+
wordStart,
545+
);
546+
});
547+
});
548+
549+
return tl;
550+
}
551+
552+
var hfCurrentTimeline = null;
553+
554+
function hfAttach(data) {
555+
var v = hfValidate(data);
556+
if (!v.ok) {
557+
hfRunGate([], 0, v.reason);
558+
return;
559+
}
560+
if (hfCurrentTimeline) {
561+
hfCurrentTimeline.kill();
562+
hfCurrentTimeline = null;
563+
}
564+
hfTeardown();
565+
var layout = hfApplyStageConfig(data, v.durationS);
566+
var tl = hfBuild(v.words, layout, v.durationS);
567+
tl.set({}, {}, v.durationS); // pin timeline length to the data-derived duration
568+
tl.seek(0);
569+
hfCurrentTimeline = tl;
570+
window.__timelines = window.__timelines || {};
571+
window.__timelines[HF_COMPOSITION_ID] = tl;
572+
hfRunGate(v.words, v.durationS, null);
573+
}
574+
window.__HF_CAPTION_ATTACH__ = hfAttach;
575+
576+
(function hfBoot() {
577+
function start(data) {
578+
var ready =
579+
document.fonts && document.fonts.ready ? document.fonts.ready : Promise.resolve();
580+
ready.then(function () {
581+
hfAttach(data);
582+
});
583+
}
584+
if (window.__HF_CAPTION__) {
585+
start(window.__HF_CAPTION__);
586+
return;
587+
}
588+
fetch("./caption-data.json")
589+
.then(function (r) {
590+
return r.ok ? r.json() : null;
591+
})
592+
.catch(function () {
593+
return null;
594+
})
595+
.then(function (json) {
596+
start(json || HF_DEMO_DATA);
597+
});
598+
})();
380599
</script>
381600
</body>
382601
</html>

0 commit comments

Comments
 (0)