JSFiddle - React, Tailwind, and code Playground
HTML
<script src="//cdn.rawgit.com/mrdoob/three.js/master/build/three.min.js"></script>
JavaScript
//set some scene specific variables
var numWheels = 3;
var wheelSeparation = 10;
var zoom = 50;
//find the point to center on
var actionCenter = ((numWheels - 1) * wheelSeparation) / 2;
var names = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
var numSides = names.length;
//setup the scene(generic)
var renderer = new THREE.WebGLRenderer();
renderer.setSize(1000, 1000);
document.body.appendChild(renderer.domElement);
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
camera.position.set(actionCenter, 0, zoom);
camera.lookAt(new THREE.Vector3(actionCenter, 0, 0));
var scene = new THREE.Scene();
//setup the scene(specific)
var cylinders = generateCylinders(names, numWheels);
for(i = 0; i < cylinders.length; i++) {
scene.add(cylinders[i]);
linearTranslate(cylinders[i], i*wheelSeparation, 0, 0);
cylinders[i].rotation.z = 1.572;
}
//setting increment for rotation
var rotation = [];
for(i = 0; i < numWheels; i++) {
rotation.push(0.1);
}
var animate = function () {
requestAnimationFrame( animate );
//spin the wheels
for(i = 0; i < numWheels; i++)
{
cylinders[i].rotation.x += rotation[i];
}
renderer.render(scene, camera);
};
/*TODO: fix logic, doesnot snap correctly, maybe
Takes a current rotation value, and snaps to the current face
*/
function snapTo(curRot) {
var rotationRange = 6.3;
var increment = numSides / rotationRange;
var floorRot = curRot/increment;
if((curRot * floorRot) < (increment / 2)) {
return floorRot;
}
return (floorRot + increment);
}
function randInt(max) {
return Math.floor((Math.random() * max) + 1);
}
//Generate the cylinders as part of setting the scene up
function generateCylinders(names, numWheels) {
var geometry = new THREE.CylinderGeometry( 15, 15,...