当前位置:首页 > 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实现轨道动画

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;

多物体轨道系统

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

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库可以实现更复杂的轨道系统:

js实现轨道

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实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…

js实现正交

js实现正交

正交的概念 正交在数学和计算机科学中通常指两个向量垂直或线性无关。在编程中,正交性常被用于设计模块化、低耦合的系统。 向量正交判断 判断两个向量是否正交可以通过点积是否为0来实现: fun…

js验证码的实现

js验证码的实现

验证码的基本实现原理 验证码(CAPTCHA)的核心目标是区分人类用户和自动化程序。JavaScript可用于生成或验证客户端验证码,但需注意纯前端验证可能被绕过,通常需结合后端验证。 纯前端验证码…

js实现上传文件

js实现上传文件

文件上传的基本实现 使用HTML的<input type="file">元素配合JavaScript的File API可以实现文件上传功能。 <input type="file"…

js 实现跳转

js 实现跳转

使用 window.location.href 进行跳转 通过修改 window.location.href 可以跳转到指定 URL,浏览器会加载新页面: window.location.hre…

js实现显示隐藏

js实现显示隐藏

显示隐藏的实现方法 在JavaScript中,实现元素的显示和隐藏可以通过多种方式完成。以下是几种常见的方法: 修改CSS的display属性 通过改变元素的display属性可以在none(隐藏)…