<script type="importmap">
      {
        "imports": {
         "three": "https://unpkg.com/three@v0.162.0/build/three.module.js",
         "three/addons/": "https://unpkg.com/three@v0.162.0/examples/jsm/" 
        }
      }
</script>
<script type="module" style="position: absolute; z-index: 100;">
    import TWEEN from 'https://cdn.jsdelivr.net/npm/@tweenjs/tween.js@23.1.1/dist/tween.esm.js';
    import * as THREE from 'three';

    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    camera.position.z = 10;
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
     

    var cube = new THREE.Mesh(new THREE.BoxGeometry(2, 2, 2), new THREE.MeshBasicMaterial({
        color: "red",
        transparent: true
    }));
    scene.add(cube);


    window.onload=function(){
        var el_my_button = document.getElementById("my_button");

        el_my_button.addEventListener("click", function(event) {
            var tweenoff = new TWEEN.Tween(cube.material).to({
                opacity: 0
            }, 2000).onComplete(function(){
                cube.visible = false;
            });

            console.log( tweenoff );
            tweenoff.start();
        });
    }


    function animate() {
      requestAnimationFrame(animate);
      renderer.render(scene, camera);
    }
    animate();
</script>
<div id="my_button" style="background: #ccc; width:100; height: 50px; margin-top:50px; position: absolute; z-index: 101;">    
    my_button
</div>

立方体的不透明度没有改变,没有任何错误,但没有任何影响,以及从TWEEN,点击"my_button". 这里有什么问题?我想我错导入了tween.js库,或者这与three.js版本不兼容?但不确定感谢任何帮助.

推荐答案

你必须在你的动画循环中调用TWEEN.update();.

function animate() {
    requestAnimationFrame(animate);
    TWEEN.update();
    renderer.render(scene, camera);
}

完整演示:https://jsfiddle.net/ubh2mjq1/2/

Javascript相关问答推荐

序列查找器功能应用默认值而不是读取响应

单击更新页面的按钮后,页面刷新;测试/断言超时,有两个标题,但没有一个标题

Angular中计算信号和getter的区别

使用javascript将Plotly Expandable Sparkline转换为HighCharter Plot在bslib卡中

在网页上添加谷歌亵渎词

如何将Openjphjs与next.js一起使用?

如何在 cypress 中使用静态嵌套循环

如何在JavaScript文件中使用Json文件

ChartJs未呈现

有条件重定向到移动子域

查询参数中的JAVASCRIPT/REACT中的括号

Webpack在导入前混淆文件名

在验证和提交表单后使用useNavigate()进行react 重定向,使用带有加载器和操作的路由

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

如何组合Multer上传?

如何在Reaction中清除输入字段

在点击链接后重定向至url之前暂停

每隔一行文本段落进行镜像(Boustrophedon)

当一条路由在Reaction路由中命中时,如何有条件地渲染两个组件或更改两个插座?

JavaScript structuredClone在Chrome/Edge中获得了非法调用,但在NodeJS中没有