超空间星隧道
Hyperspace Star Tunnel
星点从透视消失点向外扩散,随着深度缩短而加长,在画面中形成穿越星隧道的速度感。
适用场景
让旅行、加载或场景切换具有向前穿行的感觉。 把强烈的前进感留给简短的转场,并让目的地的名称始终清晰可读。
应用示例
博物馆的太空模拟器在访客选择外太阳系后,播放一小段星隧道转场。视角前进时,亮点从中心向外拉长,固定的说明栏显示目的地名称。
AI 提示词
为【舞台】制作星隧道。为每颗星点保存角度、半径和深度,匀速推进深度,并将线段的近端和远端投影到同一消失点。越过近裁剪面后重置,限制亮度,暂停时保留可读的背景。
示例代码
保存为 HTML 文件后,用浏览器打开即可运行。代码与页面里的演示一致。
查看 HTML
<!doctype html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>超空间星隧道</title>
<style>
:root {
color-scheme: dark;
--demo-bg: #171629;
--demo-ink: #f8f5ff;
--demo-accent: #b9a6ff;
--demo-muted: #bfbbd3;
font-family: system-ui, sans-serif;
color: var(--demo-ink);
background: var(--demo-bg);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; min-width: 0; }
button, input { font: inherit; }
.demo {
display: grid;
place-items: center;
align-content: center;
min-height: 100vh;
padding: 14px;
overflow: hidden;
background: radial-gradient(ellipse at 50% 24%, #28233f, var(--demo-bg) 78%);
}
.demo-button {
border: 1px solid #ffffff3d;
border-radius: 7px;
background: #ffffff12;
color: var(--demo-ink);
padding: 6px 11px;
font-size: 12px;
cursor: pointer;
}
:focus-visible {
outline: 2px solid var(--demo-accent);
outline-offset: 3px;
}
.specimen {
display: grid;
width: min(100%, 356px);
gap: 8px;
margin: 0;
justify-items: center;
}
.specimen canvas {
display: block;
width: 100%;
aspect-ratio: 2;
max-height: max(72px, calc(100vh - 65px));
border-radius: 11px;
background: linear-gradient(145deg, #232943, #14172c);
box-shadow: 0 8px 22px #090a16a8;
}
.specimen label {
display: grid;
width: 100%;
min-height: 25px;
grid-template-columns: minmax(0, auto) minmax(56px, 1fr);
align-items: center;
gap: 8px;
color: var(--demo-ink);
font-size: 12px;
}
.specimen input {
width: 100%;
margin: 0;
accent-color: #b9a6ff;
}
@media (prefers-reduced-motion: reduce) {
.specimen [data-loop-toggle] { display: none; }
}
</style>
</head>
<body>
<main class="demo">
<figure class="specimen">
<canvas role="img" aria-label="超空间星隧道" width="288" height="144">超空间星隧道</canvas>
<button class="demo-button" type="button" data-loop-toggle data-testid="effect-demo-loop-toggle">播放演示</button>
</figure>
</main>
<script>
(() => {
const canvas = document.querySelector('.specimen canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
document.querySelector('.specimen :is(input, button)').disabled = true;
return;
}
const width = 288;
const height = 144;
const turn = Math.PI * 2;
let elapsed = 0;
let variation = 0;
function dot(x, y, radius, ink) {
ctx.fillStyle = ink;
ctx.beginPath();
ctx.arc(x, y, radius, 0, turn);
ctx.fill();
}
function mix(a, b, amount) {
return a + (b - a) * amount;
}
const stars = Array.from({ length: 85 }, (_, i) => [
i * 2.39996, 9 + (i * 19 % 53), (i * 239 % 997) / 997,
]);
function draw(time) {
const travel = ctx.createRadialGradient(145, 73, 0, 145, 73, 157);
travel.addColorStop(0, '#4654a6');
travel.addColorStop(.32, '#231d52');
travel.addColorStop(1, '#111329');
ctx.fillStyle = travel;
ctx.fillRect(0, 0, width, height);
ctx.lineCap = 'round';
for (let i = 0; i < stars.length; i++) {
const [angle, radius, offset] = stars[i];
const cycle = ((offset - time * .23) % 1 + 1) % 1;
const depth = .18 + cycle * 1.65;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius * .54;
const opacity = (1 - cycle) * Math.min(1, cycle * 18);
ctx.globalAlpha = opacity;
ctx.strokeStyle = i % 7 === 0 ? '#f6d58e' : i % 3 === 0 ? '#9eddf7' : '#b6a0ff';
ctx.lineWidth = mix(.6, 2.3, 1 - cycle);
ctx.beginPath();
ctx.moveTo(144 + x / (depth + .13), 73 + y / (depth + .13));
ctx.lineTo(144 + x / depth, 73 + y / depth);
ctx.stroke();
}
}
function render() {
const ratio = Math.min(devicePixelRatio || 1, 2);
const pixelsWide = Math.max(1, Math.round(canvas.clientWidth * ratio));
const pixelsTall = Math.max(1, Math.round(canvas.clientHeight * ratio));
if (canvas.width !== pixelsWide || canvas.height !== pixelsTall) {
canvas.width = pixelsWide;
canvas.height = pixelsTall;
}
ctx.setTransform(pixelsWide / width, 0, 0, pixelsTall / height, 0, 0);
ctx.clearRect(0, 0, width, height);
ctx.save();
draw(elapsed, variation);
ctx.restore();
}
render();
new ResizeObserver(render).observe(canvas);
const button = document.querySelector('[data-loop-toggle]');
const preference = matchMedia('(prefers-reduced-motion: reduce)');
let playing = false;
let intersecting = false;
let pendingFrame = null;
let previousTime = 0;
function canAnimate() {
return playing && intersecting && !preference.matches && !document.hidden;
}
function tick(now) {
pendingFrame = null;
if (!canAnimate()) return;
elapsed += Math.min(Math.max(0, now - previousTime), 45) / 1000;
previousTime = now;
render();
pendingFrame = requestAnimationFrame(tick);
}
function syncPlayback() {
if (pendingFrame !== null) cancelAnimationFrame(pendingFrame);
pendingFrame = null;
if (canAnimate()) {
previousTime = performance.now();
pendingFrame = requestAnimationFrame(tick);
}
}
button.addEventListener('click', () => {
playing = !playing;
button.textContent = playing ? "暂停演示" : "播放演示";
button.closest('.specimen').toggleAttribute('data-playing', playing);
syncPlayback();
});
new IntersectionObserver(([entry]) => {
intersecting = entry.isIntersecting;
syncPlayback();
}).observe(canvas);
preference.addEventListener('change', syncPlayback);
document.addEventListener('visibilitychange', syncPlayback);
})();
</script>
</body>
</html>UI Skills 原创示例