From 80c920977d52248c712df69b27f377db0776f9c2 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 4 Jun 2026 20:06:12 +0100 Subject: [PATCH] Constrain VN graph chrome and continue affordance --- dist/sai.css | 41 +++++++++++++++++-------- dist/sai.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++---- src/sai.css | 41 +++++++++++++++++-------- src/sai.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 214 insertions(+), 38 deletions(-) diff --git a/dist/sai.css b/dist/sai.css index cbe97cc..f341dc4 100644 --- a/dist/sai.css +++ b/dist/sai.css @@ -251,6 +251,10 @@ height: clamp(620px, 78vh, 860px); } +.sai-speaker-stage.can-continue { + cursor: pointer; +} + .sai-speaker-background, .sai-speaker-scrim { position: absolute; @@ -304,9 +308,22 @@ .sai-graph-layer { position: absolute; - inset: 3.2rem 1rem 9rem; + top: 3.2rem; + left: 1rem; z-index: 4; - pointer-events: none; + width: min(24rem, calc(100% - 2rem)); + height: min(15rem, 34vh); + overflow: hidden; + border: 1px solid rgba(89, 183, 255, 0.18); + border-radius: 12px; + background: + linear-gradient(180deg, rgba(5, 9, 15, 0.72), rgba(3, 6, 11, 0.52)), + radial-gradient(circle at 16% 10%, rgba(89, 183, 255, 0.12), transparent 36%); + box-shadow: + 0 18px 42px rgba(0, 0, 0, 0.34), + inset 0 1px 0 rgba(255, 255, 255, 0.05); + pointer-events: auto; + backdrop-filter: blur(8px); } .sai-graph-layer[hidden] { @@ -315,9 +332,9 @@ .sai-graph-layer svg { display: block; - width: min(48rem, 46vw); - height: min(28rem, 52vh); - overflow: visible; + width: 100%; + height: 100%; + overflow: hidden; pointer-events: auto; } @@ -472,7 +489,7 @@ } .sai-speaker-stage.has-adventure-graph.has-dom-cards .sai-dom-layer { - inset: 3.6rem 1rem 9rem calc(min(48rem, 46vw) + 2rem); + inset: 3.6rem 1rem 9rem calc(min(24rem, calc(100% - 2rem)) + 2rem); } .sai-speaker-stage.has-sprites.has-dom-cards .sai-dom-card { @@ -741,12 +758,10 @@ } .sai-graph-layer { - inset: 3.2rem 0.65rem 13.5rem; - } - - .sai-graph-layer svg { - width: min(100%, 34rem); - height: min(44vh, 22rem); + top: 3.2rem; + left: 0.65rem; + width: min(18rem, calc(100% - 1.3rem)); + height: min(11rem, 28vh); } .sai-speaker-stage.has-sprites.has-dom-cards .sai-dom-layer { @@ -754,7 +769,7 @@ } .sai-speaker-stage.has-adventure-graph.has-dom-cards .sai-dom-layer { - inset: calc(3.2rem + min(44vh, 22rem) + 0.75rem) 0.65rem 13.5rem; + inset: calc(3.2rem + min(11rem, 28vh) + 0.75rem) 0.65rem 13.5rem; } .sai-sprite-layer { diff --git a/dist/sai.js b/dist/sai.js index c6731e8..f60672f 100644 --- a/dist/sai.js +++ b/dist/sai.js @@ -917,6 +917,7 @@ } function applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -926,6 +927,7 @@ cursorInfluence = 1, ) { const focus = blendGraphFocus(activePoint, cursorPoint, cursorInfluence); + applyGraphCamera(cameraGroup, viewport, graph, focus, activePoint, cursorInfluence); const radius = Math.min(viewport.width, viewport.height); for (const item of nodeElements) { const distorted = distortGraphPoint( @@ -964,6 +966,39 @@ } } + function graphZoomFrame(graph, key, fallback) { + const value = Number(graph?.[key] ?? graph?.[snakeToCamel(key)] ?? fallback); + return clamp(Number.isFinite(value) ? value : fallback, 0.18, 1); + } + + function snakeToCamel(value) { + return String(value).replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); + } + + function applyGraphCamera( + cameraGroup, + viewport, + graph, + focus, + activePoint, + cursorInfluence, + ) { + if (!cameraGroup) return; + const activeFrame = activePoint + ? graphZoomFrame(graph, "active_zoom_frame", 0.5) + : 1; + const cursorFrame = lerp( + 1, + graphZoomFrame(graph, "cursor_zoom_frame", 0.34), + cursorInfluence, + ); + const frame = Math.min(activeFrame, cursorFrame); + const zoom = 1 / frame; + const x = viewport.width / 2 - focus.x * zoom; + const y = viewport.height / 2 - focus.y * zoom; + cameraGroup.setAttribute("transform", `translate(${x} ${y}) scale(${zoom})`); + } + function graphPointFromClient(svg, viewport, clientX, clientY) { const bounds = svg.getBoundingClientRect(); return { @@ -1052,9 +1087,13 @@ svg.setAttribute("aria-label", graph.label || "Ink knot graph"); svg.setAttribute("role", "img"); + const cameraGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); + cameraGroup.setAttribute("class", "sai-graph-camera"); + svg.append(cameraGroup); + const edgeGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); edgeGroup.setAttribute("class", "sai-graph-edges"); - svg.append(edgeGroup); + cameraGroup.append(edgeGroup); const nodeGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); nodeGroup.setAttribute("class", "sai-graph-nodes"); @@ -1096,10 +1135,17 @@ }); const activate = () => { + if (story.canContinue) { + render(); + return; + } if (!chooseStoryPath(story, target)) return; render(); }; - group.addEventListener("click", activate); + group.addEventListener("click", (event) => { + event.stopPropagation(); + activate(); + }); group.addEventListener("keydown", (event) => { if (event.key !== "Enter" && event.key !== " ") return; event.preventDefault(); @@ -1107,7 +1153,7 @@ }); nodeGroup.append(group); } - svg.append(nodeGroup); + cameraGroup.append(nodeGroup); const nodeElementById = new Map(nodeElements.map((item) => [item.node.id, item])); for (const edge of graph.edges || []) { @@ -1128,6 +1174,7 @@ const applyDefaultFocus = () => { applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -1152,6 +1199,7 @@ event.clientY, ); applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -1163,10 +1211,12 @@ }; applyDefaultFocus(); - window.addEventListener("pointermove", applyPointerFocus, { passive: true }); + svg.addEventListener("pointermove", applyPointerFocus, { passive: true }); + svg.addEventListener("pointerleave", applyDefaultFocus); window.addEventListener("resize", applyDefaultFocus); stageState.graphAttentionCleanup = () => { - window.removeEventListener("pointermove", applyPointerFocus); + svg.removeEventListener("pointermove", applyPointerFocus); + svg.removeEventListener("pointerleave", applyDefaultFocus); window.removeEventListener("resize", applyDefaultFocus); }; @@ -1250,6 +1300,21 @@ }); } + function setSpeakerCanContinue(stageState, canContinue) { + if (!stageState?.stage) return; + stageState.stage.classList.toggle("can-continue", canContinue); + stageState.stage.dataset.saiCanContinue = canContinue ? "true" : "false"; + } + + function isSpeakerInteractiveTarget(target) { + if (!(target instanceof Element)) return false; + return Boolean( + target.closest( + "a, button, input, textarea, select, summary, [role='button'], [contenteditable='true']", + ), + ); + } + function renderCinematicStep(story, state) { const { choices, variables, stageState, manifest, continueButton } = state; @@ -1315,6 +1380,7 @@ choices.replaceChildren(); continueButton.hidden = true; + setSpeakerCanContinue(stageState, false); let text = ""; let metadata = {}; @@ -1346,6 +1412,7 @@ if (story.canContinue) { continueButton.hidden = false; continueButton.textContent = "Continue"; + setSpeakerCanContinue(stageState, true); return; } } @@ -1530,7 +1597,8 @@ stageState.avatar.hidden = true; stageState.name.textContent = ""; stageState.line.textContent = ""; - continueButton.onclick = () => { + const advanceSpeaker = () => { + if (continueButton.hidden) return; stageState.card.classList.add("is-exiting"); window.setTimeout(() => { stageState.card.classList.remove("is-exiting"); @@ -1544,6 +1612,11 @@ }); }, 140); }; + continueButton.onclick = advanceSpeaker; + stageState.stage.onclick = (event) => { + if (event.defaultPrevented || isSpeakerInteractiveTarget(event.target)) return; + advanceSpeaker(); + }; renderSpeakerStep(story, { choices, variables, diff --git a/src/sai.css b/src/sai.css index cbe97cc..f341dc4 100644 --- a/src/sai.css +++ b/src/sai.css @@ -251,6 +251,10 @@ height: clamp(620px, 78vh, 860px); } +.sai-speaker-stage.can-continue { + cursor: pointer; +} + .sai-speaker-background, .sai-speaker-scrim { position: absolute; @@ -304,9 +308,22 @@ .sai-graph-layer { position: absolute; - inset: 3.2rem 1rem 9rem; + top: 3.2rem; + left: 1rem; z-index: 4; - pointer-events: none; + width: min(24rem, calc(100% - 2rem)); + height: min(15rem, 34vh); + overflow: hidden; + border: 1px solid rgba(89, 183, 255, 0.18); + border-radius: 12px; + background: + linear-gradient(180deg, rgba(5, 9, 15, 0.72), rgba(3, 6, 11, 0.52)), + radial-gradient(circle at 16% 10%, rgba(89, 183, 255, 0.12), transparent 36%); + box-shadow: + 0 18px 42px rgba(0, 0, 0, 0.34), + inset 0 1px 0 rgba(255, 255, 255, 0.05); + pointer-events: auto; + backdrop-filter: blur(8px); } .sai-graph-layer[hidden] { @@ -315,9 +332,9 @@ .sai-graph-layer svg { display: block; - width: min(48rem, 46vw); - height: min(28rem, 52vh); - overflow: visible; + width: 100%; + height: 100%; + overflow: hidden; pointer-events: auto; } @@ -472,7 +489,7 @@ } .sai-speaker-stage.has-adventure-graph.has-dom-cards .sai-dom-layer { - inset: 3.6rem 1rem 9rem calc(min(48rem, 46vw) + 2rem); + inset: 3.6rem 1rem 9rem calc(min(24rem, calc(100% - 2rem)) + 2rem); } .sai-speaker-stage.has-sprites.has-dom-cards .sai-dom-card { @@ -741,12 +758,10 @@ } .sai-graph-layer { - inset: 3.2rem 0.65rem 13.5rem; - } - - .sai-graph-layer svg { - width: min(100%, 34rem); - height: min(44vh, 22rem); + top: 3.2rem; + left: 0.65rem; + width: min(18rem, calc(100% - 1.3rem)); + height: min(11rem, 28vh); } .sai-speaker-stage.has-sprites.has-dom-cards .sai-dom-layer { @@ -754,7 +769,7 @@ } .sai-speaker-stage.has-adventure-graph.has-dom-cards .sai-dom-layer { - inset: calc(3.2rem + min(44vh, 22rem) + 0.75rem) 0.65rem 13.5rem; + inset: calc(3.2rem + min(11rem, 28vh) + 0.75rem) 0.65rem 13.5rem; } .sai-sprite-layer { diff --git a/src/sai.js b/src/sai.js index c6731e8..f60672f 100644 --- a/src/sai.js +++ b/src/sai.js @@ -917,6 +917,7 @@ } function applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -926,6 +927,7 @@ cursorInfluence = 1, ) { const focus = blendGraphFocus(activePoint, cursorPoint, cursorInfluence); + applyGraphCamera(cameraGroup, viewport, graph, focus, activePoint, cursorInfluence); const radius = Math.min(viewport.width, viewport.height); for (const item of nodeElements) { const distorted = distortGraphPoint( @@ -964,6 +966,39 @@ } } + function graphZoomFrame(graph, key, fallback) { + const value = Number(graph?.[key] ?? graph?.[snakeToCamel(key)] ?? fallback); + return clamp(Number.isFinite(value) ? value : fallback, 0.18, 1); + } + + function snakeToCamel(value) { + return String(value).replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()); + } + + function applyGraphCamera( + cameraGroup, + viewport, + graph, + focus, + activePoint, + cursorInfluence, + ) { + if (!cameraGroup) return; + const activeFrame = activePoint + ? graphZoomFrame(graph, "active_zoom_frame", 0.5) + : 1; + const cursorFrame = lerp( + 1, + graphZoomFrame(graph, "cursor_zoom_frame", 0.34), + cursorInfluence, + ); + const frame = Math.min(activeFrame, cursorFrame); + const zoom = 1 / frame; + const x = viewport.width / 2 - focus.x * zoom; + const y = viewport.height / 2 - focus.y * zoom; + cameraGroup.setAttribute("transform", `translate(${x} ${y}) scale(${zoom})`); + } + function graphPointFromClient(svg, viewport, clientX, clientY) { const bounds = svg.getBoundingClientRect(); return { @@ -1052,9 +1087,13 @@ svg.setAttribute("aria-label", graph.label || "Ink knot graph"); svg.setAttribute("role", "img"); + const cameraGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); + cameraGroup.setAttribute("class", "sai-graph-camera"); + svg.append(cameraGroup); + const edgeGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); edgeGroup.setAttribute("class", "sai-graph-edges"); - svg.append(edgeGroup); + cameraGroup.append(edgeGroup); const nodeGroup = document.createElementNS("http://www.w3.org/2000/svg", "g"); nodeGroup.setAttribute("class", "sai-graph-nodes"); @@ -1096,10 +1135,17 @@ }); const activate = () => { + if (story.canContinue) { + render(); + return; + } if (!chooseStoryPath(story, target)) return; render(); }; - group.addEventListener("click", activate); + group.addEventListener("click", (event) => { + event.stopPropagation(); + activate(); + }); group.addEventListener("keydown", (event) => { if (event.key !== "Enter" && event.key !== " ") return; event.preventDefault(); @@ -1107,7 +1153,7 @@ }); nodeGroup.append(group); } - svg.append(nodeGroup); + cameraGroup.append(nodeGroup); const nodeElementById = new Map(nodeElements.map((item) => [item.node.id, item])); for (const edge of graph.edges || []) { @@ -1128,6 +1174,7 @@ const applyDefaultFocus = () => { applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -1152,6 +1199,7 @@ event.clientY, ); applyGraphAttention( + cameraGroup, nodeElements, edgeElements, viewport, @@ -1163,10 +1211,12 @@ }; applyDefaultFocus(); - window.addEventListener("pointermove", applyPointerFocus, { passive: true }); + svg.addEventListener("pointermove", applyPointerFocus, { passive: true }); + svg.addEventListener("pointerleave", applyDefaultFocus); window.addEventListener("resize", applyDefaultFocus); stageState.graphAttentionCleanup = () => { - window.removeEventListener("pointermove", applyPointerFocus); + svg.removeEventListener("pointermove", applyPointerFocus); + svg.removeEventListener("pointerleave", applyDefaultFocus); window.removeEventListener("resize", applyDefaultFocus); }; @@ -1250,6 +1300,21 @@ }); } + function setSpeakerCanContinue(stageState, canContinue) { + if (!stageState?.stage) return; + stageState.stage.classList.toggle("can-continue", canContinue); + stageState.stage.dataset.saiCanContinue = canContinue ? "true" : "false"; + } + + function isSpeakerInteractiveTarget(target) { + if (!(target instanceof Element)) return false; + return Boolean( + target.closest( + "a, button, input, textarea, select, summary, [role='button'], [contenteditable='true']", + ), + ); + } + function renderCinematicStep(story, state) { const { choices, variables, stageState, manifest, continueButton } = state; @@ -1315,6 +1380,7 @@ choices.replaceChildren(); continueButton.hidden = true; + setSpeakerCanContinue(stageState, false); let text = ""; let metadata = {}; @@ -1346,6 +1412,7 @@ if (story.canContinue) { continueButton.hidden = false; continueButton.textContent = "Continue"; + setSpeakerCanContinue(stageState, true); return; } } @@ -1530,7 +1597,8 @@ stageState.avatar.hidden = true; stageState.name.textContent = ""; stageState.line.textContent = ""; - continueButton.onclick = () => { + const advanceSpeaker = () => { + if (continueButton.hidden) return; stageState.card.classList.add("is-exiting"); window.setTimeout(() => { stageState.card.classList.remove("is-exiting"); @@ -1544,6 +1612,11 @@ }); }, 140); }; + continueButton.onclick = advanceSpeaker; + stageState.stage.onclick = (event) => { + if (event.defaultPrevented || isSpeakerInteractiveTarget(event.target)) return; + advanceSpeaker(); + }; renderSpeakerStep(story, { choices, variables,