Files
personal/lib/imageLayers.ts
T
virtheart eac0785cf6 feat: 移植 Selected Work 区块(10 个作品卡片 + WebGL 图像层注册表),修复 canvas 吞掉 DOM 悬停
- WorkSection.tsx:1:1 还原原站 work 区块(selected-work 网格、rowGap=clientWidth/12、纵横比缓存、Coding Project 徽章、元数据行、外链/内链卡片)
- workItems.ts:从 d59f7a97 数据文件提取的 10 个作品条目(passcode 保护的 /2026 TikTok 条目按原站默认过滤)
- imageLayers.ts:重构为注册表(defs/targets/订阅版本号),ImageLayers 通过 useSyncExternalStore 响应 DOM 卡片注册
- HeroScene:Canvas 容器强制 pointer-events:none,指针跟踪全部走窗口级,避免吞掉 work 卡片悬停
- Header:Work 按钮滚动至 selected-work
2026-09-01 16:37:19 +08:00

79 lines
2.2 KiB
TypeScript

/**
* Shared DOM-target registry for WebGL image layers (original `t.layers`:
* `{ key, imageUrl, hoverImageUrl, targetRef }`).
*
* - The page component attaches a DOM element per key and registers its
* texture def (SelectedWorkSection, AboutMedia).
* - The canvas-side ImageLayers component subscribes to the registry and
* renders one layer per (key, def) pair.
* - Both live in the client bundle, so it is one shared module instance.
*
* Registration is a plain data write (safe during render, idempotent);
* `touchLayerRegistry()` bumps the version so subscribers re-render — call
* it from an effect after the DOM refs have been attached.
*/
export type ImageLayerTarget = { current: HTMLElement | null };
export type ImageLayerDef = {
imageUrl: string;
hoverImageUrl?: string;
};
const defs = new Map<string, ImageLayerDef>([
["about", { imageUrl: "/img/m3.png" }],
]);
const targets: Record<string, ImageLayerTarget> = {
about: { current: null },
};
const listeners = new Set<() => void>();
let version = 0;
function emit() {
for (const cb of listeners) cb();
}
export function setLayerDef(key: string, def: ImageLayerDef) {
const prev = defs.get(key);
if (prev && prev.imageUrl === def.imageUrl && prev.hoverImageUrl === def.hoverImageUrl) {
if (!targets[key]) targets[key] = { current: null };
return;
}
defs.set(key, def);
if (!targets[key]) targets[key] = { current: null };
}
export function getLayerTarget(key: string): ImageLayerTarget {
if (!targets[key]) targets[key] = { current: null };
return targets[key];
}
export function getLayerKeys(): string[] {
return [...defs.keys()];
}
export function getLayerDef(key: string): ImageLayerDef | undefined {
return defs.get(key);
}
export function subscribeLayerRegistry(cb: () => void): () => void {
listeners.add(cb);
return () => {
listeners.delete(cb);
};
}
export function getLayerRegistryVersion(): number {
return version;
}
/** Notify subscribers that defs/targets changed. Safe to call repeatedly. */
export function touchLayerRegistry() {
version += 1;
emit();
}
// Back-compat: the about target accessed directly by AboutMedia.
export const imageLayerTargets = targets;