File size: 24,087 Bytes
25cd845 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | <!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>宇宙行星球體空間扭曲現象</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000;
cursor: grab;
}
body.grabbing {
cursor: grabbing;
}
canvas {
display: block;
}
.info-panel {
position: absolute;
bottom: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 15px;
border-radius: 10px;
max-width: 300px;
font-family: 'Arial', sans-serif;
}
.title {
position: absolute;
top: 20px;
left: 50%;
transform: translateX(-50%);
color: white;
font-size: 2rem;
text-align: center;
text-shadow: 0 0 10px #4f8fff;
font-family: 'Arial', sans-serif;
}
.controls {
position: absolute;
top: 20px;
right: 20px;
display: flex;
flex-direction: column;
gap: 10px;
}
.control-btn {
background: rgba(0, 0, 0, 0.7);
color: white;
border: 1px solid #4f8fff;
border-radius: 5px;
padding: 8px 12px;
cursor: pointer;
transition: all 0.3s;
}
.control-btn:hover {
background: rgba(79, 143, 255, 0.3);
}
.star {
position: absolute;
background-color: white;
border-radius: 50%;
pointer-events: none;
}
</style>
</head>
<body>
<div class="title">宇宙行星球體空間扭曲現象</div>
<div class="controls">
<button id="addPlanet" class="control-btn">添加行星</button>
<button id="reset" class="control-btn">重置模擬</button>
<button id="toggleGravity" class="control-btn">切換引力</button>
<button id="toggleDistortion" class="control-btn">切換扭曲效果</button>
<button id="resetView" class="control-btn">重置視角</button>
</div>
<div class="info-panel">
<h3 class="text-xl font-bold mb-2 text-blue-300">空間扭曲現象</h3>
<p class="text-sm mb-2">當大質量天體在時空中移動時,會造成周圍空間的彎曲和扭曲,這正是愛因斯坦廣義相對論所描述的現象。</p>
<p class="text-sm">在此模擬中,行星的運動會在其周圍產生引力井效應,導致光線偏折和空間變形。</p>
<p class="text-sm mt-2 text-blue-200">提示: 用滑鼠拖曳可以改變視角</p>
</div>
<canvas id="spaceCanvas"></canvas>
<script>
// 初始化畫布
const canvas = document.getElementById('spaceCanvas');
const ctx = canvas.getContext('2d');
// 設置畫布大小為窗口大小
function resizeCanvas() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// 模擬參數
const params = {
gravity: true,
distortion: true,
distortionStrength: 0.3,
friction: 0.98,
bounce: 0.7,
starCount: 200
};
// 視角控制
const view = {
x: 0,
y: 0,
scale: 1,
isDragging: false,
lastX: 0,
lastY: 0
};
// 行星類
class Planet {
constructor(x, y, radius, mass, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.mass = mass;
this.color = color;
this.vx = (Math.random() - 0.5) * 5;
this.vy = (Math.random() - 0.5) * 5;
this.rotation = 0;
this.rotationSpeed = (Math.random() - 0.5) * 0.02;
this.texture = this.createTexture(radius, color);
}
createTexture(radius, color) {
const textureCanvas = document.createElement('canvas');
const textureCtx = textureCanvas.getContext('2d');
const size = radius * 2;
textureCanvas.width = size;
textureCanvas.height = size;
// 繪製行星紋理
const gradient = textureCtx.createRadialGradient(
radius, radius, 0,
radius, radius, radius
);
gradient.addColorStop(0, lightenColor(color, 30));
gradient.addColorStop(0.7, color);
gradient.addColorStop(1, darkenColor(color, 20));
textureCtx.beginPath();
textureCtx.arc(radius, radius, radius, 0, Math.PI * 2);
textureCtx.fillStyle = gradient;
textureCtx.fill();
// 添加一些隨機的斑點紋理
for (let i = 0; i < radius / 2; i++) {
const spotRadius = Math.random() * radius / 4;
const spotX = Math.random() * size;
const spotY = Math.random() * size;
// 確保斑點在行星內部
const dist = Math.sqrt(
Math.pow(spotX - radius, 2) +
Math.pow(spotY - radius, 2)
);
if (dist < radius - spotRadius) {
const spotColor = Math.random() > 0.5 ?
lightenColor(color, 15) :
darkenColor(color, 15);
textureCtx.beginPath();
textureCtx.arc(spotX, spotY, spotRadius, 0, Math.PI * 2);
textureCtx.fillStyle = spotColor;
textureCtx.fill();
}
}
return textureCanvas;
}
update() {
this.x += this.vx;
this.y += this.vy;
this.rotation += this.rotationSpeed;
// 邊界碰撞檢測 - 現在基於原始空間而非視口
const scaledWidth = canvas.width / view.scale;
const scaledHeight = canvas.height / view.scale;
if (this.x - this.radius < 0) {
this.x = this.radius;
this.vx = -this.vx * params.bounce;
} else if (this.x + this.radius > scaledWidth) {
this.x = scaledWidth - this.radius;
this.vx = -this.vx * params.bounce;
}
if (this.y - this.radius < 0) {
this.y = this.radius;
this.vy = -this.vy * params.bounce;
} else if (this.y + this.radius > scaledHeight) {
this.y = scaledHeight - this.radius;
this.vy = -this.vy * params.bounce;
}
// 應用摩擦力
this.vx *= params.friction;
this.vy *= params.friction;
}
draw() {
ctx.save();
// 應用視角變換
ctx.translate(view.x, view.y);
ctx.scale(view.scale, view.scale);
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
// 繪製行星
ctx.drawImage(
this.texture,
-this.radius,
-this.radius,
this.radius * 2,
this.radius * 2
);
// 繪製行星光暈
if (params.distortion) {
const gradient = ctx.createRadialGradient(
0, 0, this.radius,
0, 0, this.radius * 2
);
gradient.addColorStop(0, `rgba(${hexToRgb(this.color)}, 0.3)`);
gradient.addColorStop(1, 'rgba(0, 0, 0, 0)');
ctx.beginPath();
ctx.arc(0, 0, this.radius * 2, 0, Math.PI * 2);
ctx.fillStyle = gradient;
ctx.fill();
}
ctx.restore();
}
applyGravity(otherPlanets) {
if (!params.gravity) return;
otherPlanets.forEach(planet => {
if (planet !== this) {
const dx = planet.x - this.x;
const dy = planet.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// 避免除以零和極端力
if (distance > 5) {
const force = (planet.mass * this.mass) / (distance * distance) * 0.0001;
const fx = (dx / distance) * force;
const fy = (dy / distance) * force;
this.vx += fx / this.mass;
this.vy += fy / this.mass;
}
}
});
}
// 獲取行星在屏幕上的位置
getScreenPosition() {
return {
x: this.x * view.scale + view.x,
y: this.y * view.scale + view.y
};
}
}
// 恆星類
class Star {
constructor() {
this.x = Math.random() * (canvas.width / view.scale);
this.y = Math.random() * (canvas.height / view.scale);
this.size = Math.random() * 3;
this.brightness = Math.random();
this.color = `rgba(255, 255, 255, ${this.brightness})`;
this.speed = 0.1 + Math.random() * 0.5;
this.direction = Math.random() * Math.PI * 2;
}
update(planets) {
// 恆星受行星引力影響
if (params.gravity) {
planets.forEach(planet => {
const dx = planet.x - this.x;
const dy = planet.y - this.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < planet.radius * 3) {
const angle = Math.atan2(dy, dx);
const force = (planet.mass * 0.0001) / (distance * distance);
this.x -= Math.cos(angle) * force * 10;
this.y -= Math.sin(angle) * force * 10;
}
});
}
// 恆星基本運動
this.x += Math.cos(this.direction) * this.speed;
this.y += Math.sin(this.direction) * this.speed;
// 邊界檢查 - 現在基於原始空間而非視口
const scaledWidth = canvas.width / view.scale;
const scaledHeight = canvas.height / view.scale;
if (this.x < 0 || this.x > scaledWidth ||
this.y < 0 || this.y > scaledHeight) {
this.x = Math.random() * scaledWidth;
this.y = Math.random() * scaledHeight;
this.direction = Math.random() * Math.PI * 2;
}
}
draw() {
ctx.save();
ctx.translate(view.x, view.y);
ctx.scale(view.scale, view.scale);
ctx.fillStyle = this.color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fill();
ctx.restore();
}
}
// 工具函數
function lightenColor(color, percent) {
const num = parseInt(color.replace("#", ""), 16);
const amt = Math.round(2.55 * percent);
const R = (num >> 16) + amt;
const G = (num >> 8 & 0x00FF) + amt;
const B = (num & 0x0000FF) + amt;
return `#${(
0x1000000 +
(R < 255 ? (R < 1 ? 0 : R) : 255) * 0x10000 +
(G < 255 ? (G < 1 ? 0 : G) : 255) * 0x100 +
(B < 255 ? (B < 1 ? 0 : B) : 255)
).toString(16).slice(1)}`;
}
function darkenColor(color, percent) {
const num = parseInt(color.replace("#", ""), 16);
const amt = Math.round(2.55 * percent);
const R = (num >> 16) - amt;
const G = (num >> 8 & 0x00FF) - amt;
const B = (num & 0x0000FF) - amt;
return `#${(
0x1000000 +
(R > 0 ? (R < 255 ? R : 255) : 0) * 0x10000 +
(G > 0 ? (G < 255 ? G : 255) : 0) * 0x100 +
(B > 0 ? (B < 255 ? B : 255) : 0)
).toString(16).slice(1)}`;
}
function hexToRgb(hex) {
const r = parseInt(hex.slice(1, 3), 16);
const g = parseInt(hex.slice(3, 5), 16);
const b = parseInt(hex.slice(5, 7), 16);
return `${r}, ${g}, ${b}`;
}
function getRandomPlanetColor() {
const colors = [
'#4A6FA5', // 藍色
'#A54A4A', // 紅色
'#4AA54A', // 綠色
'#A54AA5', // 紫色
'#A5A54A', // 黃色
'#4AA5A5', // 青色
'#A56F4A' // 橙色
];
return colors[Math.floor(Math.random() * colors.length)];
}
// 初始化行星和恆星
let planets = [];
let stars = [];
function init() {
planets = [];
stars = [];
// 創建一些初始行星
for (let i = 0; i < 3; i++) {
const radius = 20 + Math.random() * 30;
planets.push(new Planet(
Math.random() * (canvas.width / view.scale - radius * 2) + radius,
Math.random() * (canvas.height / view.scale - radius * 2) + radius,
radius,
radius * radius * 0.1,
getRandomPlanetColor()
));
}
// 創建恆星背景
for (let i = 0; i < params.starCount; i++) {
stars.push(new Star());
}
// 重置視角
resetView();
}
// 重置視角
function resetView() {
view.x = 0;
view.y = 0;
view.scale = 1;
}
// 動畫循環
function animate() {
// 清除畫布
ctx.fillStyle = 'rgba(0, 0, 0, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 繪製扭曲網格
if (params.distortion) {
drawSpaceGrid();
}
// 更新和繪製恆星
stars.forEach(star => {
star.update(planets);
star.draw();
});
// 更新和繪製行星
planets.forEach(planet => {
planet.applyGravity(planets);
planet.update();
planet.draw();
});
requestAnimationFrame(animate);
}
// 繪製空間網格以展示扭曲效果
function drawSpaceGrid() {
const gridSize = 40;
const rows = Math.ceil(canvas.height / (gridSize * view.scale));
const cols = Math.ceil(canvas.width / (gridSize * view.scale));
ctx.save();
ctx.translate(view.x, view.y);
ctx.scale(view.scale, view.scale);
ctx.strokeStyle = 'rgba(100, 150, 255, 0.2)';
ctx.lineWidth = 1 / view.scale;
// 繪製水平線
for (let i = 0; i < rows; i++) {
const y = i * gridSize;
ctx.beginPath();
for (let j = 0; j < cols; j++) {
const x = j * gridSize;
let newX = x;
let newY = y;
// 應用行星引力扭曲
planets.forEach(planet => {
const dx = x - planet.x;
const dy = y - planet.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < planet.radius * 5) {
const strength = params.distortionStrength * (planet.mass / 100) * (1 - distance / (planet.radius * 5));
const angle = Math.atan2(dy, dx);
newX -= Math.cos(angle) * strength * 10;
newY -= Math.sin(angle) * strength * 10;
}
});
if (j === 0) {
ctx.moveTo(newX, newY);
} else {
ctx.lineTo(newX, newY);
}
}
ctx.stroke();
}
// 繪製垂直線
for (let j = 0; j < cols; j++) {
const x = j * gridSize;
ctx.beginPath();
for (let i = 0; i < rows; i++) {
const y = i * gridSize;
let newX = x;
let newY = y;
// 應用行星引力扭曲
planets.forEach(planet => {
const dx = x - planet.x;
const dy = y - planet.y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < planet.radius * 5) {
const strength = params.distortionStrength * (planet.mass / 100) * (1 - distance / (planet.radius * 5));
const angle = Math.atan2(dy, dx);
newX -= Math.cos(angle) * strength * 10;
newY -= Math.sin(angle) * strength * 10;
}
});
if (i === 0) {
ctx.moveTo(newX, newY);
} else {
ctx.lineTo(newX, newY);
}
}
ctx.stroke();
}
ctx.restore();
}
// 事件監聽器
document.getElementById('addPlanet').addEventListener('click', () => {
const radius = 20 + Math.random() * 30;
planets.push(new Planet(
Math.random() * (canvas.width / view.scale - radius * 2) + radius,
Math.random() * (canvas.height / view.scale - radius * 2) + radius,
radius,
radius * radius * 0.1,
getRandomPlanetColor()
));
});
document.getElementById('reset').addEventListener('click', init);
document.getElementById('toggleGravity').addEventListener('click', () => {
params.gravity = !params.gravity;
document.getElementById('toggleGravity').textContent =
params.gravity ? '關閉引力' : '開啟引力';
});
document.getElementById('toggleDistortion').addEventListener('click', () => {
params.distortion = !params.distortion;
document.getElementById('toggleDistortion').textContent =
params.distortion ? '關閉扭曲' : '開啟扭曲';
});
document.getElementById('resetView').addEventListener('click', resetView);
// 滑鼠拖曳事件
canvas.addEventListener('mousedown', (e) => {
view.isDragging = true;
view.lastX = e.clientX;
view.lastY = e.clientY;
document.body.classList.add('grabbing');
});
canvas.addEventListener('mousemove', (e) => {
if (view.isDragging) {
const dx = e.clientX - view.lastX;
const dy = e.clientY - view.lastY;
view.x += dx;
view.y += dy;
view.lastX = e.clientX;
view.lastY = e.clientY;
}
});
canvas.addEventListener('mouseup', () => {
view.isDragging = false;
document.body.classList.remove('grabbing');
});
canvas.addEventListener('mouseleave', () => {
view.isDragging = false;
document.body.classList.remove('grabbing');
});
// 滑鼠滾輪縮放
canvas.addEventListener('wheel', (e) => {
e.preventDefault();
// 獲取滑鼠位置相對於畫布的座標
const mouseX = e.clientX - canvas.getBoundingClientRect().left;
const mouseY = e.clientY - canvas.getBoundingClientRect().top;
// 計算滑鼠在原始空間中的位置
const worldX = (mouseX - view.x) / view.scale;
const worldY = (mouseY - view.y) / view.scale;
// 計算縮放因子
const delta = -e.deltaY;
const zoomFactor = delta > 0 ? 1.1 : 0.9;
// 限制縮放範圍
const newScale = view.scale * zoomFactor;
if (newScale > 0.1 && newScale < 5) {
view.scale = newScale;
// 調整視圖位置以保持滑鼠下的點不變
view.x = mouseX - worldX * view.scale;
view.y = mouseY - worldY * view.scale;
}
});
// 初始化並開始動畫
init();
animate();
</script>
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=engerl/space" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
</html> |