3

我尝试实现一种方法来防止用鼠标更新值(实际上是在three.js动画开始时,通过单击按钮启动)。

目前,我有以下dat.GUI菜单:

在此处输入图像描述

单击“开始”按钮后,我想防止用户用鼠标修改参数“ Rotation x”和“ Rotation y”。

这是此菜单的相关代码部分:

// Create GUI
var gui = new dat.GUI({
      autoplace: false, 
      width: 350,
          height: 9 * 32 - 1
});

var params = {
      GreatCircle : '',
      Rotationx : torusRotationInitX,
      Rotationy : torusRotationInitY,
      StartingVector : '',
      ComponentVectorTheta : 15.0,
      ComponentVectorPhi : 15.0,
      CovariantDerivativeVector : '',
      ComponentCovariantDerivativeTheta : 15.0,
      ComponentCovariantDerivativePhi : 15.0
};

// Set parameters for GUI
gui.add(params, 'GreatCircle').name('Great Circle ');
controllerRotationx = gui.add(params, 'Rotationx', 0, 2*Math.PI, 0.001).name('Rotation x ');
controllerRotationy = gui.add(params, 'Rotationy', 0, 2*Math.PI, 0.001).name('Rotation y ');
...

当我单击重置按钮时,我调用以下函数:

  // Reset Button
  resetButton.onclick = function ResetParameters() {

  ...

  // Reinitialize parameters into gui
  params.Rotationx = torusRotationInitX; 
  params.Rotationy = torusRotationInitY; 

  for (var i in gui.__controllers) {
     gui.__controllers[i].updateDisplay();
  }

render();

}

我不知道控制器是否可以选择锁定这些通常会更改其值的滑块。可能吗?

更新 1

也许我可以将 dat.GUI 菜单包装成一个 div 并使这个 div 不可点击,这是一个解决方案吗?

更新 2

我试图应用方法上使用的方法来禁用 dat.gui 中的按钮?

遵循此解决方案,我已将扩展名添加到 中dat.gui,紧随其后:

dat.controllers.FunctionController = (function (Controller, dom, common) {

...

});

以下添加的代码片段是:

function blockEvent(event)
{
  event.stopPropagation();
}

Object.defineProperty(dat.controllers.FunctionController.prototype, "disabled", {
  get: function()
  {
    return this.domElement.hasAttribute("disabled");
  },
  set: function(value)
  {
    if (value)
    {
      this.domElement.setAttribute("disabled", "disabled");
      this.domElement.addEventListener("click", blockEvent, true);
    }
    else
    {
      this.domElement.removeAttribute("disabled");
      this.domElement.removeEventListener("click", blockEvent, true);
    }
  },
  enumerable: true
});

扩展代码是否很好地位于dat.GUI源代码中?

然后,我将属性“ disabled”设置到我的代码中,以防止用户controllerRotationx用鼠标滑动“”(一旦按下开始按钮):

if (animation)
controllerRotationx.__li.disabled = true;

不幸的是,我的方法不起作用:当动画开始时,我仍然可以将包含的滑块移动到“ controllerRotationx”中。

我看到上面的链接(在 dat.gui 中禁用按钮的方法?),这是关于按钮而不是滑块,它对我的​​情况有什么改变吗?

我没有找到滑块的显式控制器。

4

4 回答 4

10

我会这样做。滑块不是表单元素,在传统的 w3c 意义上没有什么可以禁用的。幸运的是,我们可以使用指针事件并正确禁用它,就好像它是只使用公共 dat.gui 属性的表单元素一样。

var speeder = menu.add(text, 'speed', -5, 5);
speeder.domElement.style.pointerEvents = "none"
speeder.domElement.style.opacity = .5;
于 2016-08-04T20:17:08.740 回答
1

@Radio 给出的解决方案效果很好。但是,对于滑块,滑块是文本框的 DOM 元素的兄弟。我们需要禁用包含所有控件的 div 上的指针事件(并且不直接由 dat.gui 公开)。所以,

var speeder = menu.add(text, 'speed', -5, 5);
// disables the text box
speeder.domElement.style.pointerEvents = "none"
// disables all controller elements related to "speeder"
speeder.domElement.parentElement.style.pointerEvents = 'none'
于 2017-03-28T22:25:17.887 回答
0

当按下开始按钮时,设置:

controllerRotationx.__li.setAttribute( "style", "display: none" );
于 2016-08-01T14:06:53.897 回答
0

感谢我身边的提示,我破解了通用控制器,因此能够链接。

gui.add(this, '_screenW').disable(true);

  Common.extend(controller,                                   {
    disable: function disable(v) {
      this.domElement.style.pointerEvents = v?"none":"auto";
      this.domElement.style.opacity = v?.5:1;
      return controller;
    },
于 2019-06-01T22:06:00.723 回答