当前位置:首页 > JavaScript

js实现星球转动

2026-03-15 13:28:01JavaScript

使用 Three.js 实现星球转动

Three.js 是一个基于 WebGL 的 JavaScript 库,用于创建 3D 图形。以下是实现星球转动的基本步骤:

  1. 引入 Three.js 在 HTML 文件中引入 Three.js 库:

    <script src="https://cdn.jsdelivr.net/npm/three@0.132.2/build/three.min.js"></script>
  2. 创建场景、相机和渲染器

    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
  3. 创建星球

    const geometry = new THREE.SphereGeometry(5, 32, 32);
    const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    const sphere = new THREE.Mesh(geometry, material);
    scene.add(sphere);
  4. 设置相机位置

    camera.position.z = 15;
  5. 实现动画循环

    function animate() {
        requestAnimationFrame(animate);
        sphere.rotation.y += 0.01;
        renderer.render(scene, camera);
    }
    animate();

使用 CSS 和 JavaScript 实现 2D 星球转动

  1. HTML 结构

    <div class="planet"></div>
  2. CSS 样式

    .planet {
        width: 100px;
        height: 100px;
        background: radial-gradient(circle at 30% 30%, #fff, #00f);
        border-radius: 50%;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
  3. JavaScript 动画

    const planet = document.querySelector('.planet');
    let angle = 0;
    const radius = 200;
    
    function animate() {
        angle += 0.01;
        const x = Math.cos(angle) * radius;
        const y = Math.sin(angle) * radius;
        planet.style.transform = `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;
        requestAnimationFrame(animate);
    }
    animate();

使用 Canvas 实现星球转动

  1. HTML 结构

    <canvas id="canvas"></canvas>
  2. JavaScript 代码

    js实现星球转动

    const canvas = document.getElementById('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
    
    let angle = 0;
    const radius = 100;
    const centerX = canvas.width / 2;
    const centerY = canvas.height / 2;
    
    function drawPlanet(x, y) {
        ctx.beginPath();
        ctx.arc(x, y, 30, 0, Math.PI * 2);
        ctx.fillStyle = 'blue';
        ctx.fill();
    }
    
    function animate() {
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        angle += 0.01;
        const x = centerX + Math.cos(angle) * radius;
        const y = centerY + Math.sin(angle) * radius;
        drawPlanet(x, y);
        requestAnimationFrame(animate);
    }
    animate();

注意事项

  • 性能优化:对于复杂的 3D 场景,建议使用 WebGL 或 Three.js 以获得更好的性能。
  • 交互性:可以通过事件监听器实现用户交互,例如拖动或缩放。
  • 兼容性:确保目标浏览器支持所使用的技术(如 WebGL 或 Canvas)。

标签: 星球js
分享给朋友:

相关文章

js实现全选

js实现全选

实现全选功能的方法 在JavaScript中实现全选功能通常涉及监听全选复选框的点击事件,并根据其状态控制其他复选框的选中状态。以下是几种常见的实现方式: 基础DOM操作实现 通过获取所有目标复选框…

原生js实现轮播图

原生js实现轮播图

基本结构搭建 HTML部分需要包含轮播图容器、图片列表及导航按钮。结构示例如下: <div class="slider-container"> <div class="slid…

js实现拷贝

js实现拷贝

实现文本拷贝 使用 document.execCommand 方法(已废弃但兼容性较好): function copyText(text) { const textarea = document…

js分组实现

js分组实现

分组实现方法 在JavaScript中,可以通过多种方式实现数组或对象的分组操作。以下是几种常见的方法: 使用Array.prototype.reduce() 通过reduce方法可以轻松实现数组分…

js实现瀑布流

js实现瀑布流

实现瀑布流布局 瀑布流布局是一种常见的网页布局方式,常用于图片展示、商品列表等场景。以下是使用 JavaScript 实现瀑布流布局的几种方法。 纯 JavaScript 实现 通过计算元素的位置和…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码 funct…