JSFiddle - React, Tailwind, and code Playground
HTML
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.170.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.170.0/examples/jsm/"
}
}
</script>
<div class="horizental-line"></div>
<div class="vertical-line"></div>
CSS
body {
margin: 0;
overflow: hidden;
bckground: #ff
}
body::before,
body::after {
content: "";
position: fixed;
background-color: #fff;
z-index: 1000;
}
body::before {
top: 0;
bottom: 0;
left: 50%;
width: 1px;
transform: translateX(-50%);
}
body::after {
left: 0;
right: 0;
top: 50%;
height: 1px;
transform: translateY(-50%);
}
JavaScript
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// init
const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.01, 1000 );
camera.position.z = 25;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x333333);
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshNormalMaterial();
const mesh = new THREE.Mesh( geometry, material );
mesh.scale.setScalar(3);
updateMeshPosition();
scene.add( mesh );
const renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.setAnimationLoop( animation );
document.body.appendChild( renderer.domElement );
const controls = new OrbitControls( camera, renderer.domElement );
controls.addEventListener("change", updateMeshPosition);
// animation
function animation( time ) {
mesh.rotation.x = time / 2000;
mesh.rotation.y = time / 1000;
//controls.update();
renderer.render( scene, camera );
}
function updateMeshPosition() {
const distance = Math.abs(camera.position.z - mesh.position.z);
const halfHeight = Math.tan((camera.fov * Math.PI) / 360) * distance;
const halfWidth = halfHeight * camera.aspect;
mesh.position.x = halfWidth / 2;
}
window.onresize = () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
updateMeshPosition();
renderer.setSize(window.innerWidth, window.innerHeight);
}