当前位置:首页 > JavaScript

js实现球状

2026-04-06 16:46:30JavaScript

使用Three.js创建3D球体

Three.js是一个流行的JavaScript 3D库,可以轻松创建和渲染3D球体。

安装Three.js:

npm install three

基础球体创建代码:

import * as THREE from 'three';

// 创建场景
const scene = new THREE.Scene();

// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 5;

// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建球体几何体
const geometry = new THREE.SphereGeometry(1, 32, 32);

// 创建材质
const material = new THREE.MeshBasicMaterial({color: 0x00ff00});

// 创建网格对象
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

// 动画循环
function animate() {
    requestAnimationFrame(animate);
    sphere.rotation.x += 0.01;
    sphere.rotation.y += 0.01;
    renderer.render(scene, camera);
}
animate();

使用Canvas绘制2D球体

对于简单的2D球体效果,可以使用HTML5 Canvas:

const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');

function drawSphere(x, y, radius) {
    // 创建径向渐变
    const gradient = ctx.createRadialGradient(
        x, y, radius * 0.2,
        x, y, radius
    );
    gradient.addColorStop(0, 'white');
    gradient.addColorStop(1, 'blue');

    // 绘制球体
    ctx.beginPath();
    ctx.arc(x, y, radius, 0, Math.PI * 2);
    ctx.fillStyle = gradient;
    ctx.fill();
    ctx.closePath();

    // 添加高光
    ctx.beginPath();
    ctx.arc(x - radius/3, y - radius/3, radius/5, 0, Math.PI * 2);
    ctx.fillStyle = 'rgba(255, 255, 255, 0.8)';
    ctx.fill();
    ctx.closePath();
}

drawSphere(150, 150, 100);

使用CSS创建球体效果

纯CSS也可以创建逼真的3D球体效果:

<div class="sphere"></div>

<style>
.sphere {
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background: radial-gradient(circle at 30% 30%, 
        #fff 0%, 
        #aef 40%, 
        #6ae 70%, 
        #137 100%);
    box-shadow: inset -20px -20px 60px rgba(0,0,0,0.5),
                0 0 20px rgba(0,100,255,0.5);
}
</style>

物理引擎中的球体

使用物理引擎如Cannon.js或Ammo.js可以创建具有物理特性的球体:

import * as THREE from 'three';
import * as CANNON from 'cannon';

// Three.js部分
const sphereGeometry = new THREE.SphereGeometry(1, 32, 32);
const sphereMaterial = new THREE.MeshPhongMaterial({color: 0x00aaff});
const sphereMesh = new THREE.Mesh(sphereGeometry, sphereMaterial);
scene.add(sphereMesh);

// Cannon.js物理部分
const world = new CANNON.World();
world.gravity.set(0, -9.82, 0);

const sphereShape = new CANNON.Sphere(1);
const sphereBody = new CANNON.Body({
    mass: 5,
    shape: sphereShape,
    position: new CANNON.Vec3(0, 10, 0)
});
world.addBody(sphereBody);

// 动画循环中更新
function animate() {
    world.step(1/60);
    sphereMesh.position.copy(sphereBody.position);
    sphereMesh.quaternion.copy(sphereBody.quaternion);
    renderer.render(scene, camera);
    requestAnimationFrame(animate);
}
animate();

性能优化技巧

对于需要渲染大量球体的场景,考虑使用实例化渲染:

js实现球状

const geometry = new THREE.SphereGeometry(0.5, 16, 16);
const material = new THREE.MeshPhongMaterial({color: 0x00aaff});
const spheres = new THREE.InstancedMesh(geometry, material, 1000);

const matrix = new THREE.Matrix4();
const color = new THREE.Color();

for (let i = 0; i < 1000; i++) {
    matrix.setPosition(
        Math.random() * 100 - 50,
        Math.random() * 100 - 50,
        Math.random() * 100 - 50
    );
    spheres.setMatrixAt(i, matrix);
    spheres.setColorAt(i, color.setHex(Math.random() * 0xffffff));
}

scene.add(spheres);

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

相关文章

js 实现vue模板

js 实现vue模板

实现 Vue 模板的 JavaScript 方法 通过原生 JavaScript 可以实现类似 Vue 的模板渲染功能,主要包括数据绑定、指令处理和模板解析。以下是核心实现思路: 数据绑定与…

jquery js

jquery js

jQuery 简介 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。它兼容多种浏览器,提供易于使用的 API,使开发者能够更高…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现投球

js实现投球

实现投球动画的基本思路 使用JavaScript和CSS动画结合的方式模拟投球效果。核心是通过改变元素的位置、旋转和缩放属性,配合定时器或CSS过渡实现平滑动画。 创建基础HTML结构 <di…

js钟表实现

js钟表实现

实现JavaScript钟表的基本方法 创建一个简单的JavaScript钟表可以通过以下步骤完成,涵盖数字和模拟两种形式。 数字钟表实现 HTML结构只需一个显示时间的容器: <div i…

js实现vue路由

js实现vue路由

Vue 路由的基本实现 在 Vue.js 中实现路由功能通常使用 Vue Router 库。Vue Router 是 Vue.js 官方的路由管理器,用于构建单页面应用(SPA)。 安装 Vue R…