Friday, February 4, 2022

threejs 1 torus animation


npm init @vitejs/app
npx: installed 7 in 1.154s

@vitejs/create-app is deprecated, use npm init vite instead

√ Project name: ... threejs
√ Select a framework: » vanilla
√ Select a variant: » vanilla

cd threejs
npm install

npm install three
//main.js

import './style.css'
import * as THREE from 'three';

// Setup

const scene = new THREE.Scene();

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

const renderer = new THREE.WebGLRenderer({
  canvas: document.querySelector('#bg'),
});

renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
camera.position.setZ(30);

renderer.render(scene, camera);

const geometry = new THREE.TorusGeometry(10, 3, 16, 100);
const material = new THREE.MeshBasicMaterial({color: 0xFF6347, wireframe: true});
const torus = new THREE.Mesh(geometry, material);

scene.add(torus);

function animate() {
  requestAnimationFrame(animate);

  torus.rotation.x += 0.01;
  torus.rotation.y += 0.005;
  torus.rotation.z += 0.01;

  renderer.render(scene, camera);
}

animate()

----------------------------
//index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <link rel="icon" type="image/svg+xml" href="favicon.svg" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vite App</title>
  </head>
  <body>
    <canvas id="bg"></canvas>
    
    <script type="module" src="/main.js"></script>
  </body>
</html>

-------------------------
style.css

canvas{
  position: fixed;
  top: 0;
  left: 0;
}

reference:

vite - next gen front end development tool 

10x time faster than webpack

No comments:

Post a Comment