javascript - 将多个球体放在一个距离相同的动画中

标签 javascript animation async.js tween.js

我有一个执行动画的代码。球体从直线的起点移动到直线的终点。再次开始时再次结束运动。从第一个顶点开始,到直线的最后一个顶点结束。 我想放置 20 个左右的球体,做相同的动画,但同时在相同的距离内。 我该怎么做?

这是我的代码:

var vertices = mesh.geometry.vertices;
var duration = 10;

function startToEnd() {
    var i = 0;
    async.eachSeries(vertices, function(vertice, callback) {
        if (i !== 0) {
            sphere.position.copy(vertices[i - 1]);
            new TWEEN.Tween(sphere.position).to(vertices[i],  duration).delay(duration).onComplete(function() {
                callback(null);
            }).start();
        } else {
            callback(null);
        }
        i++;
    }, startToEnd);
}
startToEnd();

这张图片就是一个例子.. enter image description here

这是我的代码的结果 enter image description here

最佳答案

我得到了一些我认为非常接近你想要的东西:

var vertices = mesh.geometry.vertices;
var duration = 20;
var spheres = [];
var amountOfSpheres = 20;

for (var i = 0; i < amountOfSpheres; i++) {
  spheres.push(new THREE.Sprite(rttMaterial));
  scene.add(spheres[i]);
}

function endlessArrayIndex(index, arrayLength) {
    if (index >= arrayLength) {
    return index % arrayLength;
  }
  return index;
}

function startToEnd() {
  i = 0;
  async.each(spheres, function(sphere, cb1) {
    var j = 0;
    var verticeIndex = endlessArrayIndex(i * Math.round(vertices.length / amountOfSpheres), vertices.length);
    async.eachSeries(vertices, function(vertice, cb2) {
      if (verticeIndex !== 0) {
        var verticeToCopy = vertices[verticeIndex - 1];
        sphere.position.copy(verticeToCopy);
        new TWEEN.Tween(sphere.position).to(vertices[verticeIndex], duration).delay(duration).onComplete(function() {
          cb2(null);
        }).start();
      } else {
        cb2(null);
      }
      verticeIndex = endlessArrayIndex(verticeIndex + 1, vertices.length);
    }, cb1);
    i++;
  }, startToEnd);
}
startToEnd();

上述代码的结果:

Sphere movement result from the code above

关于javascript - 将多个球体放在一个距离相同的动画中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34301289/

相关文章:

javascript - 在 razor foreach 语句中调用 JS 函数

javascript - 滚动上的 CSS 技能栏动画

ios - 如何停止tableviewcell按钮上的动画?

javascript - 如何动态使用 for 循环,将具有不同参数的函数推送到数组?

node.js - 有没有类似于 async.waterfall 的东西,但有错误?

php - 如果我将 lat、lng 作为变量传递, map 会中断

javascript - 如何使用 jQuery 函数更改 id 名称?

javascript - Html 5视频停止事件

css - 如何制作滑动的CSS元素?

javascript - 如何打破 SIGTERM 上的异步系列?