I am tryint to use OrbitControls
in THREE.js. If I removed the line let cameraControl = new OrbitControls(camera)
from below, there will be no error. But now, I have "Uncaught TypeError: Cannot read property 'addEventListener' of undefined"
I tried to change OrbitControls(camera)
to THREE.OrbitControls(camera)
, and then I had "Uncaught TypeError: THREE.OrbitControls is not a constructor".
I tried to import OrbitControls.js
by using <script src=...></script>
outside "module"
, instead of import {OrbitControls} from ...;
, but it doesn't work, I also tried to move let cameraControl = new OrbitControls(camera)
to other lines, but also doesn't work.
Any ideas how to fix?
<body>
<script type="module">
import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js';
import {OrbitControls} from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/controls/OrbitControls.js';
let scene, renderer, camera
let cube
function init() {
scene = new THREE.Scene()
renderer = new THREE.WebGLRenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
document.body.appendChild(renderer.domElement)
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 100)
let cameraControl = new OrbitControls(camera)
camera.position.set(10, 10, 10)
camera.lookAt(scene.position)
// cube
cube = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1))
scene.add(cube)
}
function render() {
requestAnimationFrame(render)
renderer.render(scene, camera)
}
init()
render()
</script>
</body>
let cameraControl = new OrbitControls(camera)
Always create the controls like so:
let cameraControl = new OrbitControls(camera, renderer.domElement);
The second parameter is mandatory now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With