JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas-element" width="640" height="360"></canvas>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const canvasElement = document.getElementById('canvas-element');
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, canvasElement.width/canvasElement.height);
camera.position.set(2, 2, 2);
const renderer = new THREE.WebGLRenderer({
antialias: true,
canvas: canvasElement,
});
renderer.shadowMap.enabled = true;
const controls = new OrbitControls(camera, canvasElement);
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
const light = new THREE.DirectionalLight();
light.position.set(1, 1, 1);
light.castShadow = true;
scene.add(light);
const boxGeo = new THREE.BoxGeometry(1, 1, 0.01);
const boxMat = new THREE.MeshLambertMaterial({
color: 0x00ff00,
});
const box = new THREE.Mesh(boxGeo, boxMat);
box.castShadow = true;
box.receiveShadow = true;
scene.add(box);
animate();