Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

370
Vistas
Three.js How do I define variables and/or positions after a gsap position change

Basically, what im trying to do is, that I have three cubes, and on each click they switch positions. This works fine when done once. Now, I added 3 Lines in the end of the function called "rotWorld" like this:

var i1 = i3;
var i2 = i1;
var i3 = i2;

but i get errors all the time. How can I define the switches positions as new initial positions, to that the next time I click on the trigger, the objects will move again, making a full circle at 3 clicks?

Heres a fiddle: https://jsfiddle.net/ung8qapd/1/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Okay, what you need to do here is to create the circle from 3 positions.

let counter = -1;
const positions = [
    [5, 0, 5],
    [0, 0, 5.66],
    [-5, 0, 0]
];
const cubes = [i1, i2, i3];

With the counter, whenever you click on the button, the counter will increase and we will have something like:

  • 1st click: counter=0 => 0, 1, 2
  • 2nd click: counter=1 => 1, 2, 0
  • 3rd click: counter=2 => 2, 0, 1
  • 4th click: counter=0 => 0, 1, 2

and so on, you see, it loops, and [0, 1, 2] is the index of the position (x,y,z) that you will apply to the cube. On the 4th click when the counter is bigger than the total of positions, we set it back to the beginning.

This is the new rotWorld() function:

function rotWorld(){
    console.log('--- rotate world');

    // counter store the begin of position loop
    counter++;
    counter = counter < positions.length ? counter : 0;

    // loop through each cube
    cubes.forEach((cube, index) => {
        // get the position base on the counter
        const pos = (counter + index) % positions.length;
        console.log(pos);
        gsap.to(cube.position, {
            duration: 1,
            x: positions[pos][0],
            y: positions[pos][1],
            z: positions[pos][2],
        });
    });
}

I added some log in the console so you can test it out. You can also reverse the direction using the same logic. See Fiddle

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda