forked from linkong/planet
1068 lines
45 KiB
JavaScript
1068 lines
45 KiB
JavaScript
import * as THREE from "three";
|
|
import {CalloutConnector} from "./callout-connector.js";
|
|
import {createCalloutSettleGate, layoutObstaclesSignature, updateCalloutFloat} from "./callout-rest.js";
|
|
import {centralReadingExclusion, legalDockRect} from "./callout-dock-layout.js";
|
|
import {CONFIG} from "./constants.js";
|
|
import {projectedSphereRadius, smoothHighlightAnchor} from "./highlight-anchor.js";
|
|
import {bindHUDDrag} from "./hud-drag.js";
|
|
import {advanceCalloutReveal} from "./callout-reveal.js";
|
|
import {createRegionConnectorPath} from "./callout-region-path.js";
|
|
import {getCalloutLimits} from "./callout-limits.js";
|
|
|
|
const STORAGE_KEY = "planet.compute.card-positions.v1";
|
|
const EDGE = 12;
|
|
const clamp = (value,min,max) => Math.max(min,Math.min(Math.max(min,max),value));
|
|
|
|
export function renderSpatialCalloutConnector(state, { animate = false, globe = null } = {}) {
|
|
const point = state?.sourcePoint || state?.point;
|
|
const rect = state && { x: state.displayX ?? state.x + (state.floatX || 0), y: state.displayY ?? state.y + (state.floatY || 0), width: state.width, height: state.height };
|
|
if (!point || !state.connector) return false;
|
|
|
|
// Strict leash: only automatically stretching peripheral capsules have an auto-stretching cutoff.
|
|
// When a card is manually dragged/placed by the user (state.userPlaced || state.dragging),
|
|
// or is a spotlight stage card, NEVER cut or hide the connector!
|
|
const isManual = Boolean(state.userPlaced || state.dragging);
|
|
if (!isManual && !state.isSpotlight) {
|
|
const cardCenterX = rect.x + rect.width / 2;
|
|
const cardCenterY = rect.y + rect.height / 2;
|
|
const distToAnchor = Math.hypot(point.x - cardCenterX, point.y - cardCenterY);
|
|
const maxAllowedDist = (state.width || 220) + 140;
|
|
if (distToAnchor > maxAllowedDist) {
|
|
state.connector.hide();
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (state.connector.isAnimating?.()) return true;
|
|
const rearChanged = state.lastConnectorFront !== point.front;
|
|
const sameRect = state.lastConnectorRect && ["x", "y", "width", "height"].every(
|
|
key => Math.abs(rect[key] - state.lastConnectorRect[key]) < 0.5,
|
|
);
|
|
const positionSettled = Math.hypot(state.x - state.targetX, state.y - state.targetY) < 0.5;
|
|
if (state.connector.isVisible?.() === true && state.lastConnector &&
|
|
!rearChanged && sameRect && !state.dragging && positionSettled &&
|
|
Math.hypot(point.x - state.lastConnector.x, point.y - state.lastConnector.y) < 1) {
|
|
return true;
|
|
}
|
|
|
|
const path = createRegionConnectorPath(point, rect, { globe });
|
|
const rendered = path ? state.connector.render(path, { animate }) : false;
|
|
if (!rendered) state.connector.hide();
|
|
|
|
const connectorElement = state.connector.connectorEl;
|
|
if (connectorElement) {
|
|
const rear = !point.front;
|
|
connectorElement.classList.toggle("is-rear", rear);
|
|
connectorElement.classList.toggle("is-spotlight-connector", Boolean(state.isSpotlight));
|
|
connectorElement.dataset.anchorDepth = rear ? "rear" : "front";
|
|
connectorElement.dataset.calloutKey = state.key;
|
|
}
|
|
state.lastConnector = { x: point.x, y: point.y };
|
|
state.lastConnectorFront = point.front;
|
|
state.lastConnectorRect = rect;
|
|
return rendered;
|
|
}
|
|
|
|
/**
|
|
* Computes a 3D orbital hover target for a non-spotlight capsule card.
|
|
* Strictly bounded short tether (34-44px) locks to the geographic anchor on Earth,
|
|
* moves in 120fps lockstep with the globe in 3D space, and avoids collisions.
|
|
*/
|
|
export function calculateOrbitalTarget(state, globe, viewport, occupiedRects = []) {
|
|
const point = state.point || state.sourcePoint;
|
|
const isHoverExpanded = Boolean(state.hoverExpanded);
|
|
const w = isHoverExpanded ? Math.max(state.width || 340, 320) : (state.width || 220);
|
|
const h = isHoverExpanded ? Math.max(state.height || 340, 260) : (state.height || 42);
|
|
const vpWidth = viewport?.width || (typeof innerWidth !== "undefined" ? innerWidth : 1920);
|
|
const vpHeight = viewport?.height || (typeof innerHeight !== "undefined" ? innerHeight : 1080);
|
|
|
|
if (!point || !globe) {
|
|
return {
|
|
x: clamp(state.x || 100, 16, vpWidth - w - 16),
|
|
y: clamp(state.y || 100, 64, vpHeight - h - 76),
|
|
width: w,
|
|
height: h,
|
|
};
|
|
}
|
|
|
|
// Backside horizon limb silhouette projection:
|
|
// If point is on the backside, anchor emerges from the limb silhouette of the globe
|
|
// without penetrating the front surface.
|
|
let anchorX = point.x;
|
|
let anchorY = point.y;
|
|
if (!point.front && globe && Number.isFinite(globe.radius)) {
|
|
const dx = point.x - globe.x;
|
|
const dy = point.y - globe.y;
|
|
const dist = Math.hypot(dx, dy) || 1;
|
|
anchorX = globe.x + (dx / dist) * globe.radius;
|
|
anchorY = globe.y + (dy / dist) * globe.radius;
|
|
}
|
|
|
|
// Vector from globe center to geographic anchor on screen
|
|
const dy = anchorY - globe.y;
|
|
const dist = Math.hypot(anchorX - globe.x, dy) || 1;
|
|
const uy = dy / dist;
|
|
|
|
// Short tether distance (bounded stem: strictly 36px)
|
|
const stemDistance = 36;
|
|
|
|
// Bounds clamping to safe UI zones (avoid top header and bottom toolbar)
|
|
const minX = 16;
|
|
const maxX = Math.max(minX, vpWidth - w - 16);
|
|
const minY = 64;
|
|
const maxY = Math.max(minY, vpHeight - h - 76);
|
|
|
|
// Baseline capsule geometry (收缩态胶囊的基准位置):
|
|
const capsuleW = 220;
|
|
const capsuleH = 42;
|
|
const preferRight = (anchorX + stemDistance + capsuleW <= vpWidth - 16);
|
|
const rightBlocked = occupiedRects.some(r =>
|
|
(r.isLevel1Focus || r.isSpotlight) &&
|
|
r.x < (anchorX + stemDistance + capsuleW + 8) &&
|
|
(r.x + r.width) > (anchorX + stemDistance - 8) &&
|
|
r.y < (anchorY + capsuleH + 8) &&
|
|
(r.y + r.height) > (anchorY - 8)
|
|
);
|
|
let capsuleX;
|
|
if (preferRight && !rightBlocked) {
|
|
capsuleX = anchorX + stemDistance;
|
|
} else if (!preferRight && !rightBlocked) {
|
|
capsuleX = anchorX - stemDistance - capsuleW;
|
|
} else {
|
|
const leftBlocked = occupiedRects.some(r =>
|
|
(r.isLevel1Focus || r.isSpotlight) &&
|
|
r.x < (anchorX - stemDistance) &&
|
|
(r.x + r.width) > (anchorX - stemDistance - capsuleW) &&
|
|
r.y < (anchorY + capsuleH + 8) &&
|
|
(r.y + r.height) > (anchorY - 8)
|
|
);
|
|
capsuleX = (!leftBlocked && anchorX - stemDistance - capsuleW >= minX)
|
|
? (anchorX - stemDistance - capsuleW)
|
|
: (preferRight ? anchorX + stemDistance : anchorX - stemDistance - capsuleW);
|
|
}
|
|
let capsuleY = anchorY + stemDistance * uy * 0.3 - capsuleH / 2;
|
|
capsuleX = clamp(capsuleX, minX, Math.max(minX, vpWidth - capsuleW - 16));
|
|
capsuleY = clamp(capsuleY, minY, Math.max(minY, vpHeight - capsuleH - 76));
|
|
|
|
let x = capsuleX;
|
|
let y = capsuleY;
|
|
|
|
if (isHoverExpanded) {
|
|
const bottomLimit = vpHeight - 76;
|
|
const spaceBelow = bottomLimit - (capsuleY + capsuleH);
|
|
const spaceAbove = capsuleY - minY;
|
|
// Prefer downward expansion (natural reading flow, keeps header under cursor);
|
|
// only expand upwards if downward would overflow AND upward has more room:
|
|
const isExpandUp = (spaceBelow < (h - capsuleH)) && (spaceAbove > spaceBelow);
|
|
state.element?.classList?.toggle?.("hud-panel--expand-up", isExpandUp);
|
|
state.element?.classList?.toggle?.("hud-panel--expand-down", !isExpandUp);
|
|
|
|
// Continuous geometry to prevent hover flicker:
|
|
// When expanding up, align the bottom of the card with the bottom of the capsule,
|
|
// so the pointer that entered the capsule remains securely inside the expanded card!
|
|
if (isExpandUp) {
|
|
y = capsuleY + capsuleH - h;
|
|
} else {
|
|
// When expanding down, keep card's top anchored directly to capsule's top
|
|
y = capsuleY;
|
|
}
|
|
|
|
// Keep horizontal alignment with the capsule so the pointer stays inside
|
|
if (capsuleX + w > vpWidth - 16) {
|
|
x = Math.max(minX, vpWidth - w - 16);
|
|
} else {
|
|
x = capsuleX;
|
|
}
|
|
} else {
|
|
state.element?.classList?.remove?.("hud-panel--expand-up", "hud-panel--expand-down");
|
|
x = capsuleX;
|
|
y = capsuleY;
|
|
}
|
|
|
|
// Viewport margin protection: softly clamp if reaching near the viewport edge
|
|
x = clamp(x, minX, maxX);
|
|
y = clamp(y, minY, maxY);
|
|
|
|
// Clean stacking / collision avoidance with occupied rects in the same depth plane
|
|
const isFront = Boolean(point.front);
|
|
let adjustedY = y;
|
|
let adjustedX = x;
|
|
if (!isHoverExpanded) {
|
|
let passes = 0;
|
|
while (passes < 4) {
|
|
const collision = occupiedRects.find(r =>
|
|
(r.isFront == null || r.isFront === isFront) &&
|
|
adjustedX < r.x + r.width + 8 &&
|
|
adjustedX + w + 8 > r.x &&
|
|
adjustedY < r.y + r.height + 6 &&
|
|
adjustedY + h + 6 > r.y
|
|
);
|
|
if (!collision) break;
|
|
|
|
// Shift vertically to stack capsules neatly
|
|
if (anchorY >= collision.y + collision.height / 2) {
|
|
adjustedY = collision.y + collision.height + 6;
|
|
} else {
|
|
adjustedY = collision.y - h - 6;
|
|
}
|
|
|
|
if (adjustedY < minY || adjustedY > maxY) {
|
|
adjustedY = clamp(adjustedY, minY, maxY);
|
|
adjustedX += 20;
|
|
adjustedX = clamp(adjustedX, minX, maxX);
|
|
}
|
|
passes++;
|
|
}
|
|
}
|
|
|
|
const target = {
|
|
x: Math.round(adjustedX),
|
|
y: Math.round(adjustedY),
|
|
width: w,
|
|
height: h,
|
|
isFront,
|
|
};
|
|
// A hover-expanded card is a transient floating preview (z-index: 60);
|
|
// register only its base capsule footprint so neighboring cards stay stably anchored!
|
|
const obstacleRect = isHoverExpanded
|
|
? {
|
|
x: Math.round(capsuleX),
|
|
y: Math.round(capsuleY),
|
|
width: capsuleW,
|
|
height: capsuleH,
|
|
isFront,
|
|
}
|
|
: target;
|
|
occupiedRects.push(obstacleRect);
|
|
return target;
|
|
}
|
|
|
|
export function createCalloutController({root,getSpatialContext,getAnchorCandidates,project}) {
|
|
const cards=new Map(), byElement=new WeakMap();
|
|
let saved={};
|
|
try {saved=JSON.parse(localStorage.getItem(STORAGE_KEY)||"{}");} catch { /* Optional preferences. */ }
|
|
if(!saved || typeof saved!=="object" || Array.isArray(saved)) saved={};
|
|
let dirty=true, revision=0, mobile=false, lastViewport="", lastObstacleAt=-Infinity;
|
|
let obstacles=[], preferredKey=null, urgent=false;
|
|
let permanentSpotlightKeys=new Set(), lastGlobe=null;
|
|
let limits=getCalloutLimits();
|
|
if(typeof window !== "undefined") {
|
|
window.addEventListener("earth:callout-limits-change", (e) => {
|
|
if(e.detail) { limits = e.detail; urgent = true; invalidate(); }
|
|
});
|
|
}
|
|
const settle=createCalloutSettleGate();
|
|
const reducedMotion=window.matchMedia("(prefers-reduced-motion: reduce)");
|
|
const invalidate=()=>{dirty=true;revision++;};
|
|
const persist=()=>{try {localStorage.setItem(STORAGE_KEY,JSON.stringify(saved));} catch { /* Optional preferences. */ }};
|
|
|
|
function savePosition(state) {
|
|
saved[state.key]={x:state.targetX,y:state.targetY,viewportWidth:innerWidth,viewportHeight:innerHeight,
|
|
width:state.element.style.width,height:state.element.style.height};
|
|
persist();
|
|
}
|
|
function measure(state) {
|
|
state.width=state.element.offsetWidth;state.height=state.element.offsetHeight;
|
|
state.header=state.element.querySelector("header");
|
|
}
|
|
function writePosition(state,viewport) {
|
|
const ratio=window.devicePixelRatio || 1;
|
|
let x=state.x+(state.isSpotlight ? (state.floatX||0) : 0);
|
|
let y=state.y+(state.isSpotlight ? (state.floatY||0) : 0);
|
|
if(state.userPlaced && !state.dragging && state.isSpotlight && !legalDockRect({x,y,width:state.width,height:state.height},
|
|
[centralReadingExclusion(viewport)],viewport,0)) {x=state.x;y=state.y;}
|
|
x=Math.round(x*ratio)/ratio;y=Math.round(y*ratio)/ratio;
|
|
if(x!==state.displayX) state.element.style.left=`${x}px`;
|
|
if(y!==state.displayY) state.element.style.top=`${y}px`;
|
|
state.displayX=x;state.displayY=y;
|
|
}
|
|
const resizeObserver=new ResizeObserver(entries=>{
|
|
const scale=parseFloat(getComputedStyle(document.documentElement).getPropertyValue("--hud-scale"))||1;
|
|
for(const entry of entries) {
|
|
const state=byElement.get(entry.target);if(!state) continue;
|
|
for(const axis of ["width","height"]) {
|
|
const value=entry.target.style[axis];
|
|
if(/^[0-9.]+px$/.test(value)) entry.target.style[axis]=`calc(${parseFloat(value)/scale}px * var(--hud-scale))`;
|
|
}
|
|
const width=entry.target.offsetWidth,height=entry.target.offsetHeight;
|
|
if(width===state.width && height===state.height) continue;
|
|
state.width=width;state.height=height;
|
|
if(!state.morphing) {
|
|
invalidate();
|
|
}
|
|
}
|
|
});
|
|
function attach(item) {
|
|
const stored=saved[item.key];
|
|
if(stored?.width) item.element.style.width=stored.width;
|
|
if(stored?.height) item.element.style.height=stored.height;
|
|
const state={...item,x:NaN,y:NaN,targetX:NaN,targetY:NaN,userPlaced:false,hoverExpanded:false,
|
|
revealPhase:"pending",connector:new CalloutConnector({container:root,className:"callout-connector compute-connector"})};
|
|
// Historical stored coordinates are only soft layout hints; never treat newly attached/surfaced cards as user-placed!
|
|
if(Number.isFinite(stored?.x) && Number.isFinite(stored?.y) && state.isSpotlight) {
|
|
state.preference={x:stored.x*innerWidth/(stored.viewportWidth||innerWidth),
|
|
y:stored.y*innerHeight/(stored.viewportHeight||innerHeight)};
|
|
}
|
|
measure(state);
|
|
let leaveTimer = null;
|
|
const listeners=[
|
|
["pointerenter",()=>{
|
|
if (leaveTimer) {
|
|
clearTimeout(leaveTimer);
|
|
leaveTimer = null;
|
|
}
|
|
state.hovering=true;
|
|
if (!state.isSpotlight && !state.hoverExpanded) {
|
|
state.hoverExpanded=true;
|
|
state.hoverJustTriggered=true;
|
|
state.element.classList.add("is-hover-expanded");
|
|
state.element.classList.add("is-spotlight");
|
|
measure(state);
|
|
urgent=true;
|
|
invalidate();
|
|
}
|
|
}],
|
|
["pointerleave",()=>{
|
|
state.hovering=false;
|
|
if (leaveTimer) clearTimeout(leaveTimer);
|
|
leaveTimer = setTimeout(() => {
|
|
if (!state.hovering && state.hoverExpanded) {
|
|
state.hoverExpanded=false;
|
|
state.hoverJustExited=true;
|
|
state.element.classList.remove("is-hover-expanded");
|
|
if (!permanentSpotlightKeys.has(state.key)) {
|
|
state.isSpotlight=false;
|
|
state.element.classList.remove("is-spotlight");
|
|
}
|
|
measure(state);
|
|
urgent=true;
|
|
invalidate();
|
|
}
|
|
}, 140);
|
|
}],
|
|
["focusin",event=>{state.focused=Boolean(event.target.matches(":focus-visible, input, textarea, select, [contenteditable=true]"));}],
|
|
["focusout",event=>{if(!item.element.contains(event.relatedTarget)) state.focused=false;}],
|
|
];
|
|
listeners.forEach(([event,handler])=>item.element.addEventListener(event,handler));
|
|
item.element.addEventListener("click",click,true);
|
|
const stopDrag=bindHUDDrag(item.element,{
|
|
enabled:()=>!mobile && Number.isFinite(state.x),
|
|
readPosition:()=>({left:state.displayX??state.x,top:state.displayY??state.y}),
|
|
onStart:()=>{
|
|
if (leaveTimer) clearTimeout(leaveTimer);
|
|
for(const card of cards.values()) card.element.classList.toggle("is-callout-front",card===state);
|
|
state.motion=null;state.settlingDrop=false;state.dragging=true;state.floatX=state.floatY=0;
|
|
state.vx=0;state.vy=0;
|
|
state.lastUserDragAt=performance.now();
|
|
},
|
|
onMove:({left,top})=>{
|
|
state.x=state.targetX=clamp(left,EDGE,innerWidth-state.width-EDGE);
|
|
state.y=state.targetY=clamp(top,EDGE,innerHeight-state.height-EDGE);
|
|
state.preference={x:state.x,y:state.y};
|
|
state.userPlaced=true;
|
|
state.userPinned=true;
|
|
state.dragSyncedHemisphere=null;
|
|
state.vx=0;state.vy=0;
|
|
state.lastUserDragAt=performance.now();
|
|
item.element.dataset.dragged="true";
|
|
item.element.dataset.userPlaced="true";
|
|
writePosition(state,{width:innerWidth,height:innerHeight});
|
|
},
|
|
onEnd:moved=>{
|
|
state.dragging=false;
|
|
if(moved) {
|
|
state.userPlaced=true;
|
|
state.userPinned=true;
|
|
state.targetX=state.x;
|
|
state.targetY=state.y;
|
|
state.preference={x:state.x,y:state.y};
|
|
state.motion=null;
|
|
state.settlingDrop=false;
|
|
state.vx=0;state.vy=0;
|
|
state.lastUserDragAt=performance.now();
|
|
const globeCenterX = lastGlobe ? lastGlobe.x : innerWidth * 0.5;
|
|
const cardCenterX = state.x + (state.width || 320) * 0.5;
|
|
const pt = state.point?.front ? state.point : (state.sourcePoint || state.point);
|
|
const cardIsRight = cardCenterX >= globeCenterX;
|
|
const anchorIsRight = pt ? (pt.x >= globeCenterX) : cardIsRight;
|
|
state.dragSyncedHemisphere = (cardIsRight === anchorIsRight);
|
|
item.element.dataset.dragged="false";
|
|
item.element.dataset.userPlaced="true";
|
|
savePosition(state);
|
|
state.ignoreClickUntil=performance.now()+200;
|
|
urgent=true;
|
|
invalidate();
|
|
}
|
|
},
|
|
});
|
|
state.cleanup=()=>{
|
|
if (leaveTimer) clearTimeout(leaveTimer);
|
|
stopDrag();
|
|
item.element.classList.remove("is-callout-front");
|
|
item.element.removeEventListener("click",click,true);
|
|
listeners.forEach(([event,handler])=>item.element.removeEventListener(event,handler));
|
|
};
|
|
return state;
|
|
}
|
|
function set(items) {
|
|
const keys=new Set(items.map(item=>item.key));
|
|
for(const [key,state] of cards) if(!keys.has(key)) {
|
|
state.cleanup();state.connector.hide();state.connector.connectorEl?.remove();
|
|
resizeObserver.unobserve(state.element);byElement.delete(state.element);cards.delete(key);invalidate();
|
|
}
|
|
for(const item of items) {
|
|
const existing=cards.get(item.key);
|
|
if(existing) {
|
|
existing.anchor=item.anchor;existing.anchorCodes=item.anchorCodes;existing.anchorCandidates=item.anchorCandidates;
|
|
if(existing.header!==item.element.querySelector("header")) {measure(existing);invalidate();}
|
|
} else {
|
|
const state=attach(item);cards.set(item.key,state);byElement.set(item.element,state);resizeObserver.observe(item.element);
|
|
item.element.style.visibility="hidden";item.element.dataset.calloutPhase="pending";invalidate();
|
|
}
|
|
}
|
|
}
|
|
function measureObstacles(now) {
|
|
if(now-lastObstacleAt<500) return;lastObstacleAt=now;
|
|
const owned=new Set([...cards.values()].map(state=>state.element));
|
|
const next=[...document.querySelectorAll(".hud-panel, #desktop-news-ticker, #legend, #control-toolbar, #toolbar-hub, .hud-panel-brand")]
|
|
.filter(el=>!owned.has(el) && !el.matches(".compute-picker,.compute-evidence") && !el.hidden &&
|
|
!el.classList.contains("hud-panel-hidden") && getComputedStyle(el).visibility!=="hidden" &&
|
|
getComputedStyle(el).opacity!=="0" && el.getClientRects().length)
|
|
.map(el=>{const r=el.getBoundingClientRect();return {x:r.x - 8,y:r.y - 8,width:r.width + 16,height:r.height + 16};});
|
|
if(layoutObstaclesSignature(next)!==layoutObstaclesSignature(obstacles)) {obstacles=next;invalidate();}
|
|
}
|
|
function preparePlan(viewport) {
|
|
const items=[...cards.values()].map(state=>({key:state.key,point:state.point,
|
|
size:{width:state.width,height:state.height},
|
|
reading:state.hovering||state.focused,userPlaced:state.userPlaced,
|
|
previousPosition:Number.isFinite(state.targetX)?{x:state.targetX,y:state.targetY}:state.preference}));
|
|
const priorityKey=preferredKey || items.find(item=>item.reading)?.key;
|
|
|
|
// Multi-type Spotlight Stage positioning:
|
|
// Left Stage and Right Stage slot director.
|
|
// Both 1 Compute card and 1 News card can be concurrently displayed without collision.
|
|
// Hover-expanded cards stay anchored at their flag position and do not take up stage slots.
|
|
const spotlightCards = [...cards.values()].filter(s => s.isSpotlight && !s.hoverExpanded);
|
|
const spotlightTargets = new Map();
|
|
const spotlightObstacles = [];
|
|
|
|
const leftHudEdge = obstacles
|
|
.filter(o => o.x < 140 && o.y > 60 && o.y < viewport.height * 0.75)
|
|
.reduce((max, o) => Math.max(max, o.x + o.width), 0);
|
|
const minX = Math.max(leftHudEdge + 16, 24);
|
|
|
|
const topHudEdge = obstacles
|
|
.filter(o => o.y < 80 && o.x < viewport.width * 0.35)
|
|
.reduce((max, o) => Math.max(max, o.y + o.height), 0);
|
|
const minY = Math.max(topHudEdge + 12, 72);
|
|
|
|
const bottomToolbar = obstacles.find(o => o.y > viewport.height * 0.75 && (o.x + o.width > viewport.width * 0.35 && o.x < viewport.width * 0.65));
|
|
const maxStageBottom = bottomToolbar ? (bottomToolbar.y - 14) : (viewport.height - 110);
|
|
|
|
const rightHudEdge = obstacles
|
|
.filter(o => o.x > viewport.width * 0.60 && o.y > 60 && o.y < viewport.height * 0.75)
|
|
.reduce((min, o) => Math.min(min, o.x), viewport.width);
|
|
const stageRightX = (cardW) => {
|
|
const candidate = Math.min(viewport.width - cardW - 36, rightHudEdge - cardW - 16);
|
|
const minRight = lastGlobe ? lastGlobe.x + 50 : viewport.width * 0.5;
|
|
return Math.max(minRight, candidate);
|
|
};
|
|
const stageBaseX = Math.max(minX, 36);
|
|
|
|
// Partition spotlight cards into Left Stage and Right Stage lists:
|
|
const leftList = [];
|
|
const rightList = [];
|
|
const globeCenterX = lastGlobe ? lastGlobe.x : viewport.width / 2;
|
|
|
|
for (const sc of spotlightCards) {
|
|
if (sc.userPinned && Number.isFinite(sc.targetX)) {
|
|
spotlightTargets.set(sc.key, {
|
|
x: sc.targetX,
|
|
y: sc.targetY,
|
|
width: sc.width || 340,
|
|
height: sc.height || 360,
|
|
});
|
|
spotlightObstacles.push({
|
|
x: sc.targetX,
|
|
y: sc.targetY,
|
|
width: sc.width || 340,
|
|
height: sc.height || 360,
|
|
});
|
|
continue;
|
|
}
|
|
const pt = sc.sourcePoint || sc.point;
|
|
const curSide = sc.stageSide;
|
|
const isRight = pt ? (pt.x > globeCenterX + (curSide === "right" ? -25 : 25)) : false;
|
|
sc.stageSide = isRight ? "right" : "left";
|
|
if (isRight) rightList.push(sc);
|
|
else leftList.push(sc);
|
|
}
|
|
|
|
// Sort order within each stage lane: News card on top (slot 0), Compute card underneath (slot 1)
|
|
const sortStageCards = (list) => list.sort((a, b) => {
|
|
const aIsNews = a.element.classList.contains("news-region-card");
|
|
const bIsNews = b.element.classList.contains("news-region-card");
|
|
if (aIsNews && !bIsNews) return -1;
|
|
if (!aIsNews && bIsNews) return 1;
|
|
return 0;
|
|
});
|
|
|
|
sortStageCards(leftList);
|
|
sortStageCards(rightList);
|
|
|
|
const assignStageSlots = (list, isRight) => {
|
|
if (list.length === 0) return;
|
|
const getX = (w) => isRight ? stageRightX(w) : stageBaseX;
|
|
if (list.length === 1) {
|
|
const sc = list[0];
|
|
const w = sc.width || 340;
|
|
const h = sc.height || 360;
|
|
const availableH = maxStageBottom - minY;
|
|
const sy = Math.round(minY + Math.max(0, (availableH - h) * 0.40));
|
|
const target = { x: Math.round(getX(w)), y: sy, width: w, height: h };
|
|
spotlightTargets.set(sc.key, target);
|
|
spotlightObstacles.push(target);
|
|
} else if (list.length > 2) {
|
|
list.forEach((sc, index) => {
|
|
const width = sc.width || 340;
|
|
const height = sc.height || 340;
|
|
const travel = Math.max(0, maxStageBottom - minY - height);
|
|
const target = {
|
|
x: Math.round(getX(width)),
|
|
y: Math.round(minY + travel * index / (list.length - 1)),
|
|
width,
|
|
height,
|
|
};
|
|
spotlightTargets.set(sc.key, target);
|
|
spotlightObstacles.push(target);
|
|
});
|
|
} else {
|
|
const c0 = list[0];
|
|
const c1 = list[1];
|
|
const w0 = c0.width || 340, h0 = c0.height || 340;
|
|
const w1 = c1.width || 300, h1 = c1.height || 320;
|
|
const gap = 18;
|
|
const totalH = h0 + gap + h1;
|
|
let y0 = Math.round(minY + Math.max(0, (maxStageBottom - minY - totalH) / 2));
|
|
let y1 = y0 + h0 + gap;
|
|
if (y1 + h1 > maxStageBottom) {
|
|
y1 = maxStageBottom - h1;
|
|
y0 = Math.max(minY, y1 - gap - h0);
|
|
}
|
|
const t0 = { x: Math.round(getX(w0)), y: Math.round(y0), width: w0, height: h0 };
|
|
const t1 = { x: Math.round(getX(w1)), y: Math.round(y1), width: w1, height: h1 };
|
|
spotlightTargets.set(c0.key, t0);
|
|
spotlightTargets.set(c1.key, t1);
|
|
spotlightObstacles.push(t0, t1);
|
|
}
|
|
};
|
|
|
|
assignStageSlots(leftList, false);
|
|
assignStageSlots(rightList, true);
|
|
|
|
const occupiedForOrbit = [...spotlightObstacles];
|
|
|
|
// Assign final targets for both Spotlight Cards and Orbital/Capsule Cards
|
|
for (const [key, state] of cards) {
|
|
if (state.isSpotlight && !state.hoverExpanded) {
|
|
const target = spotlightTargets.get(key);
|
|
if (target) {
|
|
state.targetX = target.x;
|
|
state.targetY = target.y;
|
|
if (!Number.isFinite(state.x) || state.revealPhase !== "visible") {
|
|
state.x = target.x;
|
|
state.y = target.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
}
|
|
if (state.revealPhase === "visible") state.element.style.visibility = "";
|
|
}
|
|
} else {
|
|
if (state.userPinned && Number.isFinite(state.targetX)) {
|
|
// Keep pinned position
|
|
} else {
|
|
state.userPlaced = false;
|
|
state.motion = null;
|
|
const orbitalTarget = calculateOrbitalTarget(state, lastGlobe, viewport, occupiedForOrbit);
|
|
state.targetX = orbitalTarget.x;
|
|
state.targetY = orbitalTarget.y;
|
|
if (!Number.isFinite(state.x) || state.revealPhase !== "visible") {
|
|
state.x = orbitalTarget.x;
|
|
state.y = orbitalTarget.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
}
|
|
if (state.revealPhase === "visible") state.element.style.visibility = "";
|
|
}
|
|
}
|
|
}
|
|
preferredKey = null;
|
|
dirty = false;
|
|
urgent = false;
|
|
}
|
|
function update(deltaMs,now) {
|
|
if(!cards.size || root.hidden) return;
|
|
mobile=document.documentElement.classList.contains("layout-mode-mobile");
|
|
if(mobile) {for(const state of cards.values()) {state.connector.hide();state.element.style.visibility="";}return;}
|
|
const viewport={width:innerWidth,height:innerHeight};
|
|
const viewportKey=`${innerWidth}:${innerHeight}`;
|
|
if(viewportKey!==lastViewport) {lastViewport=viewportKey;for(const state of cards.values()) measure(state);urgent=true;invalidate();}
|
|
measureObstacles(now);
|
|
const context=getSpatialContext?.();if(!context?.earth || !context?.camera) return;
|
|
context.camera.updateMatrixWorld();
|
|
const center=new THREE.Vector3().setFromMatrixPosition(context.earth.matrixWorld);
|
|
const distance=context.camera.position.distanceTo(center);
|
|
const rim=center.clone().add(new THREE.Vector3().setFromMatrixColumn(context.camera.matrixWorld,0).multiplyScalar(CONFIG.earthRadius)).project(context.camera);
|
|
center.project(context.camera);
|
|
const globe={x:(center.x+1)*innerWidth/2,y:(1-center.y)*innerHeight/2,
|
|
radius:projectedSphereRadius(Math.abs(rim.x-center.x)*innerWidth/2,distance,CONFIG.earthRadius)};
|
|
lastGlobe=globe;
|
|
const busy=context.isDragging || context.activeTouchPoints>0 || context.hasActiveGlobeInertia ||
|
|
[...cards.values()].some(state=>state.dragging) || Boolean(document.querySelector(".hud-panel.is-dragging,.hud-panel.is-resizing"));
|
|
const sources=[...obstacles,...[...cards.values()].filter(state=>Number.isFinite(state.x))
|
|
.map(state=>({x:state.x,y:state.y,width:state.width,height:state.height}))];
|
|
for(const state of cards.values()) {
|
|
state.point=project(state.anchor,context.earth,context.camera,innerWidth,innerHeight);
|
|
if(!state.point) {state.connector.hide();continue;}
|
|
|
|
if (state.isSpotlight) {
|
|
const alternate=state.highlightAnchorState?.candidateKey!=null || !state.point.front || state.point.visibility<0.34;
|
|
const candidates=alternate?(state.anchorCandidates||getAnchorCandidates?.(state.anchorCodes)||[]):[];
|
|
const result=smoothHighlightAnchor({...state.highlightAnchorState,cardPoint:Number.isFinite(state.x)?
|
|
{x:state.x+state.width/2,y:state.y+state.height/2}:null},{preferred:state.point,
|
|
candidates:candidates.map(anchor=>project(anchor,context.earth,context.camera,innerWidth,innerHeight)).filter(Boolean),
|
|
globe,obstacles:sources,viewport,deltaMs});
|
|
state.highlightAnchorState=result.state;state.sourcePoint=result.source||state.point;
|
|
state.element.classList.toggle("is-rear",!state.sourcePoint.front);
|
|
} else {
|
|
state.sourcePoint=state.point;
|
|
state.element.classList.toggle("is-rear",!state.point.front);
|
|
}
|
|
|
|
if (state.userPinned && !state.dragging) {
|
|
if (state.hovering) {
|
|
state.lastUserDragAt = now;
|
|
}
|
|
const pt = state.point?.front ? state.point : (state.sourcePoint || state.point);
|
|
const curX = state.displayX ?? state.x;
|
|
const cardCenterX = curX + (state.width || 320) * 0.5;
|
|
const cardIsRight = cardCenterX >= globe.x;
|
|
const isDeepRear = pt.front === false && (pt.visibility ?? -1) < -0.15;
|
|
|
|
// If user dragged card to opposite hemisphere ahead of time, and the anchor has now rotated
|
|
// into that hemisphere, mark them as synced in the same hemisphere!
|
|
if (!state.dragSyncedHemisphere) {
|
|
const strictlyEnteredCardHemisphere = cardIsRight
|
|
? (pt.x >= globe.x + 8)
|
|
: (pt.x <= globe.x - 8);
|
|
if (strictlyEnteredCardHemisphere && pt.front !== false) {
|
|
state.dragSyncedHemisphere = true;
|
|
}
|
|
}
|
|
|
|
// Two-state unpin rule:
|
|
// 1. If already synced in same hemisphere (dragSyncedHemisphere === true):
|
|
// - While anchor stays in same hemisphere (top or bottom), never reset!
|
|
// - As soon as anchor crosses into the OTHER hemisphere (!inSameHemisphereNow),
|
|
// IMMEDIATELY unpin (0 delay) and carry the card to the other hemisphere!
|
|
// 2. If dragged to opposite hemisphere (dragSyncedHemisphere === false):
|
|
// - Wait for holdElapsed (2600ms) confirmation pause; if anchor still hasn't arrived, float back.
|
|
const shouldUnpin = isDeepRear;
|
|
|
|
if (shouldUnpin) {
|
|
state.userPinned = false;
|
|
state.userPlaced = false;
|
|
state.dragSyncedHemisphere = false;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
state.element.dataset.dragged = "false";
|
|
state.element.dataset.userPlaced = "false";
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// Auto-focus the region closest to the center of view when Earth rotates:
|
|
for (const type of ["compute", "news"]) {
|
|
const candidates = [...cards.entries()].filter(([k, s]) =>
|
|
(s.element.classList.contains("compute-card") ? "compute" : "news") === type &&
|
|
s.point && s.point.front
|
|
);
|
|
if (candidates.length === 0) continue;
|
|
|
|
let currentKey = [...permanentSpotlightKeys].find(k => {
|
|
const s = cards.get(k);
|
|
return s && (s.element.classList.contains("compute-card") ? "compute" : "news") === type;
|
|
});
|
|
|
|
const radius = globe.radius || 400;
|
|
|
|
// Find candidate closest to globe center
|
|
let bestKey = null;
|
|
let bestDist = Infinity;
|
|
for (const [k, s] of candidates) {
|
|
const p = s.point;
|
|
const centerDist = Math.hypot(p.x - globe.x, p.y - globe.y);
|
|
if (centerDist < bestDist) {
|
|
bestDist = centerDist;
|
|
bestKey = k;
|
|
}
|
|
}
|
|
|
|
if (!currentKey) {
|
|
if (bestKey) permanentSpotlightKeys.add(bestKey);
|
|
} else if (bestKey && bestKey !== currentKey) {
|
|
const currentCard = cards.get(currentKey);
|
|
const curPoint = currentCard?.point;
|
|
const curDist = curPoint?.front ? Math.hypot(curPoint.x - globe.x, curPoint.y - globe.y) : Infinity;
|
|
const isCurrentRecentlyDragged = Boolean(
|
|
currentCard?.dragging || currentCard?.userPinned
|
|
);
|
|
|
|
// Auto-switch criteria:
|
|
// 1. Current card has rotated to rear side (!front) -> switch immediately
|
|
// 2. Current card has drifted away from center and candidate is noticeably closer
|
|
// 3. Candidate is close to center and clearly closer than current
|
|
const isCurrentRear = !curPoint?.front;
|
|
const isCurrentDrifting = curDist > radius * 0.30 && bestDist < curDist - 15;
|
|
const isCandidateCentral = bestDist < radius * 0.45 && bestDist < curDist - 18;
|
|
|
|
if (!isCurrentRecentlyDragged && (isCurrentRear || isCurrentDrifting || isCandidateCentral)) {
|
|
permanentSpotlightKeys.delete(currentKey);
|
|
if (currentCard && !currentCard.hoverExpanded) {
|
|
currentCard.isSpotlight = false;
|
|
currentCard.element.classList.remove("is-spotlight");
|
|
}
|
|
permanentSpotlightKeys.add(bestKey);
|
|
const bestCard = cards.get(bestKey);
|
|
if (bestCard && !bestCard.userPinned) {
|
|
bestCard.userPlaced = false;
|
|
bestCard.element.dataset.dragged = "false";
|
|
bestCard.element.dataset.userPlaced = "false";
|
|
}
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
}
|
|
const primaryKey = permanentSpotlightKeys.has(currentKey) ? currentKey : bestKey;
|
|
const limit = Math.max(1, Math.floor(Number(limits[type]) || 1));
|
|
const eligible = [...cards.entries()].filter(([, state]) => (
|
|
(state.element.classList.contains("compute-card") ? "compute" : "news") === type &&
|
|
state.point && (state.point.front || state.dragging || state.userPinned)
|
|
));
|
|
const ranked = eligible.sort(([aKey, a], [bKey, b]) => (
|
|
Number(Boolean(b.dragging || b.userPinned)) - Number(Boolean(a.dragging || a.userPinned)) ||
|
|
Number(bKey === primaryKey) - Number(aKey === primaryKey) ||
|
|
Math.hypot(a.point.x - globe.x, a.point.y - globe.y) -
|
|
Math.hypot(b.point.x - globe.x, b.point.y - globe.y)
|
|
));
|
|
const selectedKeys = new Set(ranked.slice(0, limit).map(([key]) => key));
|
|
let selectionChanged = false;
|
|
for (const key of permanentSpotlightKeys) {
|
|
const state = cards.get(key);
|
|
if (!state || ((state.element.classList.contains("compute-card") ? "compute" : "news") === type &&
|
|
!selectedKeys.has(key))) {
|
|
permanentSpotlightKeys.delete(key);
|
|
selectionChanged = true;
|
|
}
|
|
}
|
|
for (const key of selectedKeys) {
|
|
if (!permanentSpotlightKeys.has(key)) {
|
|
permanentSpotlightKeys.add(key);
|
|
selectionChanged = true;
|
|
}
|
|
}
|
|
if (selectionChanged) {
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
}
|
|
|
|
// Check if any spotlight card needs stage handover (crossing central meridian):
|
|
let stageNeedsReplan = false;
|
|
for (const sc of cards.values()) {
|
|
if (sc.isSpotlight && !sc.hoverExpanded && !sc.userPinned && sc.point) {
|
|
const curSide = sc.stageSide;
|
|
const pt = sc.sourcePoint || sc.point;
|
|
const desiredSide = pt.x > (globe.x + (curSide === "right" ? -25 : 25)) ? "right" : "left";
|
|
if (curSide && curSide !== desiredSide) {
|
|
sc.stageSide = desiredSide;
|
|
stageNeedsReplan = true;
|
|
}
|
|
}
|
|
}
|
|
if (stageNeedsReplan) {
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
|
|
// Active spotlight keys include both the permanent spotlight and any hover-expanded card:
|
|
const nextSpotlightKeys = new Set(permanentSpotlightKeys);
|
|
for (const [key, state] of cards) {
|
|
if (state.hoverExpanded) {
|
|
nextSpotlightKeys.add(key);
|
|
}
|
|
}
|
|
|
|
let spotlightChanged = false;
|
|
for (const [key, state] of cards) {
|
|
const isSpotlight = nextSpotlightKeys.has(key);
|
|
if (state.isSpotlight !== isSpotlight) {
|
|
state.isSpotlight = isSpotlight;
|
|
state.element.classList.toggle("is-spotlight", isSpotlight);
|
|
state.morphing = true;
|
|
setTimeout(() => {
|
|
state.morphing = false;
|
|
measure(state);
|
|
urgent = true;
|
|
invalidate();
|
|
}, 380);
|
|
spotlightChanged = true;
|
|
}
|
|
}
|
|
if (spotlightChanged) {
|
|
for (const state of cards.values()) measure(state);
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
const settled=settle(now,busy,String(revision));
|
|
if(dirty && (!busy || urgent) && (settled||urgent)) preparePlan(viewport);
|
|
|
|
// Update non-spotlight (or hover-expanded) 3D orbital positions & Earth occlusion in real-time (120fps lockstep)
|
|
const occupiedInFrame = [];
|
|
for (const sc of cards.values()) {
|
|
if (sc.isSpotlight && !sc.hoverExpanded && Number.isFinite(sc.x)) {
|
|
occupiedInFrame.push({ x: sc.x, y: sc.y, width: sc.width, height: sc.height, isSpotlight: true });
|
|
}
|
|
}
|
|
const infoPanelEl = typeof document !== "undefined" ? document.getElementById("info-panel") : null;
|
|
if (infoPanelEl && !infoPanelEl.hidden && infoPanelEl.classList.contains("is-visible")) {
|
|
const ipRect = infoPanelEl.getBoundingClientRect();
|
|
if (ipRect.width > 0 && ipRect.height > 0) {
|
|
occupiedInFrame.push({
|
|
x: ipRect.x,
|
|
y: ipRect.y,
|
|
width: ipRect.width,
|
|
height: ipRect.height,
|
|
isLevel1Focus: true,
|
|
});
|
|
}
|
|
}
|
|
for (const state of cards.values()) {
|
|
if (state.isSpotlight && !state.hoverExpanded) {
|
|
state.element.classList.remove("is-backside");
|
|
state.element.style.zIndex = state.element.classList.contains("is-callout-front") ? "55" : "50";
|
|
state.element.style.opacity = "1";
|
|
state.element.style.setProperty("--spatial-scale", "1");
|
|
state.element.style.transform = "";
|
|
if (state.connector?.connectorEl) {
|
|
state.connector.connectorEl.style.opacity = "1";
|
|
}
|
|
continue;
|
|
}
|
|
if (!state.userPinned) {
|
|
state.userPlaced = false;
|
|
state.motion = null;
|
|
}
|
|
const point = state.point;
|
|
if (!point) continue;
|
|
|
|
if (state.hoverExpanded) {
|
|
state.element.classList.remove("is-backside", "is-rear");
|
|
state.element.style.zIndex = "60";
|
|
state.element.style.opacity = "1";
|
|
state.element.style.setProperty("--spatial-scale", "1");
|
|
state.element.style.transform = "scale(1)";
|
|
if (state.connector?.connectorEl) {
|
|
state.connector.connectorEl.style.opacity = "1";
|
|
}
|
|
state.element.style.pointerEvents = "auto";
|
|
state.element.style.visibility = "";
|
|
} else {
|
|
const vis = point.visibility ?? (point.front ? 1 : -1);
|
|
const isFront = point.front && vis > 0.04;
|
|
|
|
// Smooth continuous gradual dimming and depth scaling:
|
|
// Front (vis >= 0.12): alpha = 1.0, scale = 1.0
|
|
// Transitioning to backside (-0.50 <= vis < 0.12): smoothly dim to 0.42 and shrink to 0.75
|
|
// Deep backside (vis < -0.50): alpha = 0.42, scale = 0.75
|
|
const t = Math.max(0, Math.min(1, (vis - (-0.50)) / (0.12 - (-0.50))));
|
|
const ease = t * t * (3 - 2 * t);
|
|
const alpha = 0.42 + 0.58 * ease;
|
|
const scale = 0.75 + 0.25 * ease;
|
|
|
|
if (!isFront) {
|
|
state.element.classList.add("is-backside", "is-rear");
|
|
state.element.style.zIndex = "8";
|
|
} else {
|
|
state.element.classList.remove("is-backside", "is-rear");
|
|
state.element.style.zIndex = "30";
|
|
}
|
|
|
|
state.element.style.opacity = alpha.toFixed(3);
|
|
state.element.style.setProperty("--spatial-scale", scale.toFixed(3));
|
|
state.element.style.transform = `scale(${scale.toFixed(3)})`;
|
|
if (state.connector?.connectorEl) {
|
|
state.connector.connectorEl.style.opacity = (alpha * 0.75).toFixed(3);
|
|
}
|
|
state.element.style.pointerEvents = "auto";
|
|
state.element.style.visibility = "";
|
|
}
|
|
|
|
// Continuous smooth spring tracking with Earth rotation:
|
|
// Non-spotlight cards act like a silky flag tethered to the Earth's geographic anchor
|
|
if (!state.dragging && !state.userPinned) {
|
|
const orbital = calculateOrbitalTarget(state, globe, viewport, occupiedInFrame);
|
|
state.targetX = orbital.x;
|
|
state.targetY = orbital.y;
|
|
if (state.hoverJustTriggered) {
|
|
state.hoverJustTriggered = false;
|
|
state.x = orbital.x;
|
|
state.y = orbital.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
} else if (state.hoverJustExited) {
|
|
state.hoverJustExited = false;
|
|
state.x = orbital.x;
|
|
state.y = orbital.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
} else if (Number.isFinite(state.x) && state.revealPhase === "visible") {
|
|
const dt = Math.max(0.5, Math.min(2.5, deltaMs / 16.6));
|
|
const dx = orbital.x - state.x;
|
|
const dy = orbital.y - state.y;
|
|
const dist = Math.hypot(dx, dy);
|
|
if (dist > 45) {
|
|
const maxSpeed = 8.5;
|
|
const desiredSpeed = Math.min(maxSpeed, dist * 0.045);
|
|
const step = Math.min(dist, desiredSpeed * dt);
|
|
state.x += (dx / dist) * step;
|
|
state.y += (dy / dist) * step;
|
|
} else if (dist <= 1.5) {
|
|
state.x = orbital.x;
|
|
state.y = orbital.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
} else {
|
|
const followRate = Math.min(0.24, 0.14 * dt);
|
|
state.x += dx * followRate;
|
|
state.y += dy * followRate;
|
|
}
|
|
} else {
|
|
state.x = orbital.x;
|
|
state.y = orbital.y;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const state of cards.values()) {
|
|
if (!Number.isFinite(state.x)) continue;
|
|
const reading = state.hovering || state.focused;
|
|
|
|
// Continuous Fluid Balloon Follow for Spotlight Cards (轻柔气球空气阻尼漂浮):
|
|
if (state.isSpotlight && !state.hoverExpanded && !state.dragging) {
|
|
if (Number.isFinite(state.targetX) && Number.isFinite(state.targetY)) {
|
|
if (state.revealPhase !== "visible") {
|
|
state.x = state.targetX;
|
|
state.y = state.targetY;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
} else {
|
|
const dx = state.targetX - state.x;
|
|
const dy = state.targetY - state.y;
|
|
const dist = Math.hypot(dx, dy);
|
|
if (dist > 0.5) {
|
|
if (!reading) {
|
|
const dt = Math.max(0.5, Math.min(2.5, deltaMs / 16.6));
|
|
const maxSpeed = 8.0;
|
|
const desiredSpeed = Math.min(maxSpeed, dist * 0.032);
|
|
const desiredVx = (dx / dist) * desiredSpeed;
|
|
const desiredVy = (dy / dist) * desiredSpeed;
|
|
const inertiaBlend = Math.min(1, 0.12 * dt);
|
|
state.vx = (state.vx || 0) + (desiredVx - (state.vx || 0)) * inertiaBlend;
|
|
state.vy = (state.vy || 0) + (desiredVy - (state.vy || 0)) * inertiaBlend;
|
|
const stepX = state.vx * dt;
|
|
const stepY = state.vy * dt;
|
|
if (Math.hypot(stepX, stepY) >= dist) {
|
|
state.x = state.targetX;
|
|
state.y = state.targetY;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
} else {
|
|
state.x += stepX;
|
|
state.y += stepY;
|
|
}
|
|
}
|
|
} else {
|
|
state.x = state.targetX;
|
|
state.y = state.targetY;
|
|
state.vx = 0;
|
|
state.vy = 0;
|
|
}
|
|
}
|
|
}
|
|
const isSettled = Math.hypot((state.targetX || 0) - state.x, (state.targetY || 0) - state.y) < 2;
|
|
updateCalloutFloat(state, now, deltaMs, reducedMotion.matches || !isSettled || reading);
|
|
}
|
|
|
|
writePosition(state, viewport);
|
|
{
|
|
const rendered = renderSpatialCalloutConnector(state, { animate: state.revealPhase === "pending" && !reducedMotion.matches, globe });
|
|
const wasVisible = state.revealPhase === "visible";
|
|
state.revealPhase = advanceCalloutReveal(state, { now, rendered, animating: state.connector.isAnimating(), reducedMotion: reducedMotion.matches });
|
|
if (state.revealPhase === "visible" && !wasVisible) {
|
|
state.element.style.visibility = "";
|
|
if (!reducedMotion.matches) state.element.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 180, easing: "ease" });
|
|
}
|
|
}
|
|
if (state.element.dataset.calloutPhase !== state.revealPhase) state.element.dataset.calloutPhase = state.revealPhase;
|
|
}
|
|
}
|
|
const click=event=>{
|
|
const element=event.target.closest(".compute-card,.news-region-card");
|
|
const state=byElement.get(element);
|
|
if(!state || state.ignoreClickUntil>performance.now()) return;
|
|
if(state.key && !permanentSpotlightKeys.has(state.key)) {
|
|
const type = state.element.classList.contains("compute-card") ? "compute" : "news";
|
|
for (const k of permanentSpotlightKeys) {
|
|
const s = cards.get(k);
|
|
if (s && (s.element.classList.contains("compute-card") ? "compute" : "news") === type) {
|
|
permanentSpotlightKeys.delete(k);
|
|
s.isSpotlight = false;
|
|
s.hoverExpanded = false;
|
|
s.element.classList.remove("is-spotlight", "is-hover-expanded");
|
|
}
|
|
}
|
|
permanentSpotlightKeys.add(state.key);
|
|
state.isSpotlight = true;
|
|
state.hoverExpanded = false;
|
|
state.element.classList.add("is-spotlight");
|
|
state.element.classList.remove("is-hover-expanded");
|
|
state.morphing = true;
|
|
setTimeout(() => {
|
|
state.morphing = false;
|
|
measure(state);
|
|
urgent = true;
|
|
invalidate();
|
|
}, 380);
|
|
preferredKey = state.key;
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
if(event.target.closest('[data-action="card-collapse"]')) {
|
|
state.isSpotlight = false;
|
|
state.hoverExpanded = false;
|
|
state.element.classList.remove("is-spotlight", "is-hover-expanded");
|
|
permanentSpotlightKeys.delete(state.key);
|
|
preferredKey=state.key;
|
|
urgent = true;
|
|
invalidate();
|
|
}
|
|
};
|
|
return {set,update,invalidate,destroy(){set([]);resizeObserver.disconnect();}};
|
|
}
|