Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fire code everytime camera position changes in three.js

Tags:

three.js

How do I trigger code everytime my camera position changes?

This is what I have so far:

camera.addEventListener("change", function(event){
// my code
});
like image 795
Asperger Avatar asked Nov 29 '15 14:11

Asperger


1 Answers

If you are using OrbitControls.js, to control the camera, then that library fires a change event when it's update function causes the camera to zoom, pan or rotate;

https://github.com/mrdoob/three.js/blob/master/examples/js/controls/OrbitControls.js#L203

So for example, you can use something like this to respond to when the camera moves;

  function onPositionChange(o) {

    console.log("position changed in object");
    console.log(o);
  }

  controls.addEventListener('change', onPositionChange);
like image 156
Tom Avatar answered Sep 30 '22 18:09

Tom