Three.js 是一个非常流行的 JavaScript 3D 库,用于在网页上创建和显示3D图形。由于其广泛的用途和活跃的社区,Three.js 是开源的,并且可以免费下载和使用。以下是获取 Three.js 的几种方法:
Three.js 的官方网站提供了最新版本的下载链接,以及详细的文档和示例。
在下载页面,你可以选择以下几种方式获取 Three.js:
three.min.js
)和未压缩的开发版(three.js
),适合不同的使用场景。如果你使用的是前端构建工具(如 npm、yarn 等),可以通过包管理器安装 Three.js。
npm install three
安装完成后,你可以在项目中通过 import
或 require
引入 Three.js:
// 使用 ES6 模块
import * as THREE from 'three';
// 或者使用 CommonJS
const THREE = require('three');
yarn add three
如果你不想下载文件,可以直接在 HTML 文件中通过 CDN 链接引入 Three.js。这是快速开始使用 Three.js 的一种简便方法。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Three.js 示例</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<!-- 引入 Three.js CDN -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r152/three.min.js"></script>
<script>
// 你的 Three.js 代码
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);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
camera.position.z = 5;
function animate() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
如果你需要查看 Three.js 的源码、贡献代码或获取历史版本,可以访问其官方 GitHub 仓库。
在仓库中,你可以:
如果你使用 webpack、Parcel 或其他前端构建工具,可以通过包管理器安装 Three.js 并在项目中使用。例如,使用 webpack:
npm install three
然后在你的 JavaScript 文件中引入:
import * as THREE from 'three';
// 你的 Three.js 代码
领取专属 10元无门槛券
手把手带您无忧上云