Hello,
I want to use troika-3d-text without using methods like ES Modules, Webpack, or Vite, similar to the sample code below. However, my sample code isn’t working, and I guess I need to add some other libraries as well, but I’m not sure which ones. Please guide me.
<!DOCTYPE html>
<html>
<head>
<title>Troika Three Text Example</title>
<style>
body {
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/three@latest/build/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@latest/examples/js/controls/OrbitControls.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/troika-three-text@0.52.4/dist/troika-three-text.min.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
const myText = new Text();
myText.text = "Troika Text";
myText.fontSize = 0.3;
myText.color = 0xffffff;
scene.add(myText);
myText.sync();
function updateTextPosition() {
const facePosition = new THREE.Vector3(0, 0, 0.51);
cube.localToWorld(facePosition);
myText.position.copy(facePosition);
}
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.enableZoom = true;
camera.position.z = 3;
function animate(time) {
requestAnimationFrame(animate);
controls.update();
updateTextPosition();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
In VS Code, when I build the project using npx vite, a file named index-xGub19vU.js is generated inside the dist\assets folder. With this file, I can run the initial code without any issues.
Right now, I just need to know which JavaScript files are bundled inside this index-xGub19vU.js file.
<!DOCTYPE html>
<html>
<head>
<title>Troika Three Text Example</title>
<style>
body {
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="./index-xGub19vU.js"></script>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const light = new THREE.DirectionalLight(0xffffff, 1);
light.position.set(5, 5, 5);
scene.add(light);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
const myText = new Text();
myText.text = "Troika Text";
myText.fontSize = 0.3;
myText.color = 0xffffff;
scene.add(myText);
myText.sync();
function updateTextPosition() {
const facePosition = new THREE.Vector3(0, 0, 0.51);
cube.localToWorld(facePosition);
myText.position.copy(facePosition);
}
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.enableZoom = true;
camera.position.z = 3;
function animate(time) {
requestAnimationFrame(animate);
controls.update();
updateTextPosition();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
There should be a package.json file in the root folder of your project, I’d assume all listed dependencies in that file as well as any dependencies not tree shaken within the node_modules folder are bundled into your index.js file, @seanwasere has provided a solid demonstration of using trioka with import maps above