【问题标题】:How to prevent click on raycasted object during mousemove?如何防止在鼠标移动期间单击光线投射的对象?
【发布时间】:2019-02-18 21:21:13
【问题描述】:

我有一组可点击对象,可以使用滚轮或单击并拖动来旋转。我遇到的问题是,当您在对象顶部拖动然后释放时,它将启动单击动画。

Here 是一个链接到简化项目的小提琴。

有谁知道如何在拖动过程中阻止点击被记录?看起来有点困难,因为它们是光线投射的对象。

我的js函数:

document.addEventListener("mousedown", function () {
    isMouseDown = true
    startX = event.pageX
    startY = event.pageY
    document.body.style.cursor = 'grabbing';
  })

  document.addEventListener("mouseup", function () {
    isMouseDown = false
    document.body.style.cursor = 'grab';
  })

  document.addEventListener("mousemove", function (event) {
    if (isMouseDown) {
      document.body.style.cursor = 'grabbing'
    }

    aimX = ((window.innerWidth / 2) - event.pageX) * 0.35
    aimY = ((window.innerHeight / 2) - event.pageY) * 0.5

    mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
    mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;

    raycaster.setFromCamera(mouse, camera)

    const intersections = raycaster.intersectObjects(group.children)

    if (intersections.length > 0) {
      if(projectHov){
        if (INTERSECTED != intersections[0].object) {
            if (INTERSECTED)
            INTERSECTED.material.color.setHex(INTERSECTED.currentHex);
            INTERSECTED = intersections[0].object;
            INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
            //setting up new material on hover
            INTERSECTED.material.color.setHex( 0xadadad );
            if (INTERSECTED){
              if(projectHov){
                document.body.style.cursor = "pointer"
              }
            }
        }
      }

      } else {
          if (INTERSECTED) INTERSECTED.material.color.setHex(INTERSECTED.currentHex);
          document.body.style.cursor = "grab";
          INTERSECTED = null;
      }

// Moving the objects ==========================
    if(isMouseDown) {
      let currentRotation = new THREE.Matrix4();
              currentRotation.makeRotationFromEuler(group.rotation);

              let newEuler = new THREE.Euler(0, ((event.pageX - startX) + (event.pageY - startY)) / 325, 0);
              let newRotation = new THREE.Matrix4();
              newRotation.makeRotationFromEuler(newEuler);

              let finalRotation = new THREE.Matrix4();
              finalRotation.multiplyMatrices(newRotation, currentRotation);

              group.rotation.setFromRotationMatrix(finalRotation);

              startX = event.pageX;
              startY = event.pageY;
    }
  })
section.addEventListener('click', onDocumentMouseDown, false);

  function onDocumentMouseDown(event) {
      event.preventDefault();
      mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
      raycaster.setFromCamera(mouse, camera);
      const intersections = raycaster.intersectObjects(objects)

// Animation =================================
          if (intersections.length > 0){
            if(projectHov){
              tl.play();

              tl.to(backTag, 1, {
                delay: 1.25,
                autoAlpha:1,
              }, 0);

            }

// Animation ===================================
            if (intersections[0].object == old ) {
              if(projectHov){
                tlOld.play();
                projectHov = false
                tlOld.to(old, 1.5, {three:{scaleX: 1.5, scaleY: 1.5, x:0, y:0, z:0}, ease:Power2.easeInOut}, 0);
                tlOld.to(fnup, 1, {three:{y:-4000, opacity: 0 }, ease:Power2.easeInOut}, 0);
                tlOld.to(alex, 1, {three:{y:-4000, opacity: 0 }, ease:Power2.easeInOut}, 0);
                tlOld.to(cam, 1, {three:{y:-4000, opacity: 0 }, ease:Power2.easeInOut}, 0);
                tlOld.to(mirrorCube, 1, {three:{y:-400, opacity: 0 }, ease:Power2.easeInOut}, 0);
              groupRotate = false
            }

            } 

【问题讨论】:

    标签: javascript three.js click preventdefault


    【解决方案1】:

    您可能希望将intersections[0].object.uuid; 存储在一个变量中,并在mousedown 和mouseup 时更新它。然后在鼠标抬起时将存储的 uuid 与光线投射的对象 uuid 进行比较。仅当它们相等时才进一步处理。

    【讨论】:

    • 我对 uuid 不是很熟悉。我需要某种方式来保存变量吗?你能说得具体一点吗?
    • 啊,好吧,我明白你的意思了,但是我无法存储 uuid 以便将其与光线投射的对象 uuid 进行比较。谢谢你,没有意识到 uuid 是我可以用作带有三个.js 对象的数据的东西。
    猜你喜欢
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多