简体   繁体   English

ThreeJS动画

[英]ThreeJS Animation

I'm trying my hand at some ThreeJS, and I'm toying with the idea of having multiple objects on the scene and adding transition on these objects on a button click.我正在尝试使用一些 ThreeJS,并且正在考虑在场景中放置多个对象并通过单击按钮在这些对象上添加过渡的想法。

I've wrapped my head around having multiple objects and adding objects to the scene on a click.我已经开始考虑拥有多个对象并通过单击将对象添加到场景中。 But I want to a smooth transition when I click a button.但是当我点击一个按钮时我想要一个平滑的过渡。 I've created this codepen to demonstrate我创建了这个代码笔来演示

https://codepen.io/ben456789/pen/rvaJGm https://codepen.io/ben456789/pen/rvaJGm

var boxLarge = new THREE.SphereGeometry(5, 5, 5);
var boxMiddle = new THREE.SphereGeometry(5, 5, 5);
var boxSmall = new THREE.SphereGeometry(5, 5, 5);

These are the initial values for the created spheres.这些是创建的球体的初始值。 And I want each one to transition on each button click to be.我希望每个按钮都可以在每次单击按钮时进行转换。

var boxLarge = new THREE.SphereGeometry(170, 70, 60);
var boxMiddle = new THREE.SphereGeometry(100, 50, 40);
var boxSmall = new THREE.SphereGeometry(40, 10, 10);

What I want is to expand the geometry of the spheres as the user clicks to make it look like the Spheres originate from the original sphere in the middle.我想要的是在用户单击时扩展球体的几何形状,使其看起来像球体源自中间的原始球体。

Any help would be appreciated!任何帮助将不胜感激!

I use a library called gsap for the scale up animation, you may need to adjust the scale properties accordingly, may be use an object to store the sphere and the desired scale/size, anyway here is the code:我使用一个名为gsap的库来放大动画,您可能需要相应地调整缩放属性,可能使用一个对象来存储球体和所需的缩放/大小,无论如何这里是代码:

const spheres = [cubeSmall, cubeMiddle, cubeLarge];

let index = 0;
renderer.domElement.addEventListener('click', clickHandler);

function clickHandler(e) {
  if (index <= 3) {
    gsap.to(spheres[index].scale, 3, {
      x: 2,
      y: 2,
      z: 2
    });
    index++;
  }
}

If you copy and paste that in your codepen it should work ,hope that helps.如果您将其复制并粘贴到代码笔中,它应该可以工作,希望有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM