当前位置:首页 > JavaScript

js实现轨道

2026-04-06 05:36:38JavaScript

实现轨道动画的基本方法

使用JavaScript创建轨道动画通常涉及Canvas或SVG技术。以下是两种常见的实现方式:

Canvas实现轨道动画

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
let angle = 0;
const radius = 100;
const centerX = canvas.width/2;
const centerY = canvas.height/2;

function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 绘制轨道
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, 0, Math.PI*2);
    ctx.strokeStyle = '#ccc';
    ctx.stroke();

    // 计算轨道上的点位置
    const x = centerX + Math.cos(angle) * radius;
    const y = centerY + Math.sin(angle) * radius;

    // 绘制移动物体
    ctx.beginPath();
    ctx.arc(x, y, 10, 0, Math.PI*2);
    ctx.fillStyle = 'red';
    ctx.fill();

    angle += 0.01;
    requestAnimationFrame(animate);
}

animate();

SVG实现轨道动画

js实现轨道

const svg = document.getElementById('mySvg');
const orbit = document.createElementNS("http://www.w3.org/2000/svg", "circle");
orbit.setAttribute("cx", "150");
orbit.setAttribute("cy", "150");
orbit.setAttribute("r", "100");
orbit.setAttribute("stroke", "#ccc");
orbit.setAttribute("fill", "none");
svg.appendChild(orbit);

const object = document.createElementNS("http://www.w3.org/2000/svg", "circle");
object.setAttribute("r", "10");
object.setAttribute("fill", "red");
svg.appendChild(object);

let angle = 0;
function animate() {
    const radius = 100;
    const centerX = 150;
    const centerY = 150;

    const x = centerX + Math.cos(angle) * radius;
    const y = centerY + Math.sin(angle) * radius;

    object.setAttribute("cx", x);
    object.setAttribute("cy", y);

    angle += 0.01;
    requestAnimationFrame(animate);
}

animate();

椭圆轨道的实现方法

椭圆轨道需要调整x和y方向的半径:

// 在Canvas实现中修改位置计算
const radiusX = 150;  // 水平半径
const radiusY = 80;   // 垂直半径
const x = centerX + Math.cos(angle) * radiusX;
const y = centerY + Math.sin(angle) * radiusY;

多物体轨道系统

创建多个物体在轨道上运动:

js实现轨道

const objects = [];
const count = 5;

// 初始化多个物体
for(let i=0; i<count; i++) {
    objects.push({
        angle: Math.PI*2 * i/count,
        speed: 0.005 + Math.random()*0.01
    });
}

function animate() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // 绘制轨道
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius, 0, Math.PI*2);
    ctx.stroke();

    // 更新并绘制每个物体
    objects.forEach(obj => {
        obj.angle += obj.speed;
        const x = centerX + Math.cos(obj.angle) * radius;
        const y = centerY + Math.sin(obj.angle) * radius;

        ctx.beginPath();
        ctx.arc(x, y, 8, 0, Math.PI*2);
        ctx.fill();
    });

    requestAnimationFrame(animate);
}

性能优化建议

使用requestAnimationFrame而不是setInterval来实现动画循环 对于复杂轨道系统,考虑使用对象池技术重用对象 在不需要高精度时,可以适当降低帧率

3D轨道实现

使用Three.js等3D库可以实现更复杂的轨道系统:

import * as THREE from 'three';

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();

// 创建轨道环
const orbitGeometry = new THREE.TorusGeometry(5, 0.1, 16, 100);
const orbitMaterial = new THREE.MeshBasicMaterial({color: 0xaaaaaa});
const orbit = new THREE.Mesh(orbitGeometry, orbitMaterial);
scene.add(orbit);

// 创建轨道上的物体
const sphereGeometry = new THREE.SphereGeometry(0.5, 32, 32);
const sphereMaterial = new THREE.MeshBasicMaterial({color: 0xff0000});
const sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
scene.add(sphere);

let angle = 0;
function animate() {
    angle += 0.01;
    sphere.position.x = Math.cos(angle) * 5;
    sphere.position.z = Math.sin(angle) * 5;

    renderer.render(scene, camera);
    requestAnimationFrame(animate);
}

animate();

标签: 轨道js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callback) {…

js实现全选

js实现全选

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

js实现图表

js实现图表

在JavaScript中实现图表通常使用流行的图表库,以下是几种常见的方法和工具: 使用Chart.js Chart.js是一个简单灵活的库,适合快速生成响应式图表。安装方式包括CDN引入或npm安…

js实现vr

js实现vr

使用WebXR API实现VR体验 WebXR是浏览器中实现VR和AR体验的标准API,它取代了早期的WebVR。现代浏览器如Chrome、Edge和Firefox已支持WebXR。 // 初始化W…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。 &…

js实现交换

js实现交换

交换变量的方法 在JavaScript中,交换两个变量的值有多种方法。以下是常见的几种实现方式: 使用临时变量 通过引入一个临时变量来存储其中一个变量的值,实现交换: let a = 1; le…