CSS 3D 空间立体多面棱镜:基于 clip-path 与透视矩阵的折光展台 - this
/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */
#content_views .toc,
/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */
#content_views.markdown_views > p:empty:has(+ .toc),
#content_views.markdown_views > .toc + p:empty,
/* 富文本旧版目录标记 */
#content_views.htmledit_views #main-toc,
#content_views.htmledit_views #hr-toc,
#content_views.htmledit_views p[id*="-toc"] {
display: none !important;
}
/* 目录去掉后,紧跟的首个标题不再多出一块上边距 */
#content_views.markdown_views > .toc + h1,
#content_views.markdown_views > .toc + h2,
#content_views.markdown_views > .toc + h3,
#content_views.markdown_views > .toc + h4,
#content_views.markdown_views > .toc + p:empty + h1,
#content_views.markdown_views > .toc + p:empty + h2,
#content_views.markdown_views > .toc + p:empty + h3,
#content_views.markdown_views > .toc + p:empty + h4 {
margin-top: 0 !important;
}
在现代高端品牌官网、奢侈品数字橱窗以及未来感十足的科技展台中,传统的水平轮播图(Carousel)早已被用户审美淘汰。取而代之的,是一种在三维空间中静静悬浮、通透折光的多面立体棱柱或多面棱镜(Prism Display)。
很多前端工程师的第一直觉是:“这必须上 Three.js 或 WebGPU 才能做。”
然而,引入几十上百 KB 的 3D 图形引擎不仅会增加初始包体积,更割裂了三维世界与宿主 DOM 树的自然融合——你很难在 WebGL 的纹理里无缝渲染带选中态的 HTML 原生文本、可访问的
`;
face.appendChild(content);
// 光学菲涅尔高光遮罩层 拼多多帮助力入口
const glare = document.createElement('div');
glare.className = 'prism-glare';
face.appendChild(glare);
this.rotator.appendChild(face);
}
this.container.appendChild(this.rotator);
}
private bindEvents(): void {
const el = this.container;
el.addEventListener('pointerdown', (e) => {
this.isDragging = true;
this.startX = e.clientX;
this.startRotationY = this.targetRotationY;
el.setPointerCapture(e.pointerId);
});
el.addEventListener('pointermove', (e) => {
if (!this.isDragging) return;
const deltaX = e.clientX - this.startX;
// 灵敏度系数 0.25 度/像素
this.targetRotationY = this.startRotationY + deltaX * 0.25;
});
const stopDrag = () => {
if (!this.isDragging) return;
this.isDragging = false;
// 磁吸对齐到最近的一个棱面
const stepAngle = 360 / this.faceCount;
const snappedIndex = Math.round(this.targetRotationY / stepAngle);
this.targetRotationY = snappedIndex * stepAngle;
};
el.addEventListener('pointerup', stopDrag);
el.addEventListener('pointercancel', stopDrag);
}
private startLoop(): void {
const tick = () => {
// 弹簧平滑插值 (Lerp)
this.currentRotationY += (this.targetRotationY - this.currentRotationY) * 0.08;
// 驱动核心容器旋转
this.rotator.style.transform = `translateZ(-${this.apothem}px) rotateY(${this.currentRotationY.toFixed(3)}deg)`;
// 动态更新每个面的法线折光强度
this.updateOptics();
this.animFrameId = requestAnimationFrame(tick);
};
this.animFrameId = requestAnimationFrame(tick);
}
private updateOptics(): void {
const stepAngle = 360 / this.faceCount;
const faces = this.rotator.children;
for (let i = 0; i < faces.length; i++) {
const face = faces[i] as HTMLElement;
const glare = face.querySelector('.prism-glare') as HTMLElement;
if (!glare) continue;
// 计算当前面相对于摄像机正视角的绝对夹角
const absoluteAngle = (stepAngle * i + this.currentRotationY) % 360;
const normalizedAngle = (absoluteAngle + 360) % 360; // 0 ~ 360
const rad = (normalizedAngle * Math.PI) / 180;
const cosVal = Math.cos(rad);
// cosVal 接近 1 时正对用户,接近 0 时处于侧切边缘
if (cosVal > 0) {
// 菲涅尔与镜面反射系数:在倾斜边缘时反光最强(次表面折射特征)
const specular = Math.pow(1 - cosVal, 2.5);
glare.style.opacity = (specular * 0.8 + 0.1).toFixed(3);
glare.style.background = `linear-gradient(${normalizedAngle * 2}deg, rgba(255,255,255,0.45) 0%, transparent 60%)`;
} else {
glare.style.opacity = '0';
}
}
}
public destroy(): void {
cancelAnimationFrame(this.animFrameId);
this.container.innerHTML = '';
}
}
光学灵魂:基于透视视角的菲涅尔折射遮罩
很多单纯使用 CSS 3D 的作品显得“假”,核心原因在于光照是静态的。当一个 3D 棱柱在旋转时,物体表面的反光高光必须根据观察者的视线向量(View Vector)动态流转。
在上述代码中,我们通过计算面法线与视线方向的余弦值 $\cos(\theta)$,在 JavaScript 中以极其廉价的开销动态更新 .prism-glare 的透明度与渐变角度。当一个棱面从斜后方旋转到正前方时,高光从一侧锋利闪烁,随后在正对镜头时退化为通透温润的微弱环境光——这种光学物理特征直接骗过了大脑的视觉皮层,产生如同真实水晶或磨砂亚克力一般的质感。
性能与合成器调优秘籍
transform-origin 深度补偿:在很多开发者的实现中,旋转时棱柱会产生前后晃动。这是因为旋转中心没有设置在棱柱的几何中心。代码中我们通过将 rotator 整体沿 Z 轴后移 translateZ(-apothem px),使得旋转的原点完美落在多边形的内心中心,旋转时所有面的运动轨迹严格贴合圆柱外缘。backface-visibility: hidden 的显存优化:背面朝向相机的面必须隐藏,这样浏览器硬件合成器可以直接剔除对应的面,减少 50% 以上的像素填充率(Fill Rate)。保留原生交互与 DOM 无障碍:棱柱上的按钮与文本是 100% 原生 DOM。无需任何射线碰撞检测(Raycasting),键盘 Tab 键、屏幕阅读器以及鼠标点击均可原生地聚焦在正面的卡片上。
结语
CSS 绝不仅仅是一门用来画方块和调整文字大小的排版语言。当数学的严密几何与现代 GPU 合成器管线结合,纯 CSS 同样拥有构筑震撼立体维度的无尽潜力。
在这座折光晶莹的多面棱镜中,每一次指尖的拖拽与滑动,都在屏幕上折射出理性算法与感性美学交融的璀璨微光。