onWheel method

dynamic onWheel(
  1. dynamic event
)

Implementation

onWheel(event) {
  if (enabled && enableZoom) {
    String? modifier;

    if (event.ctrlKey || event.metaKey) {
      modifier = 'CTRL';
    } else if (event.shiftKey) {
      modifier = 'SHIFT';
    }

    var mouseOp = getOpFromAction('WHEEL', modifier);

    if (mouseOp != null) {
      event.preventDefault();
      dispatchEvent(_startEvent);

      var notchDeltaY = 125; //distance of one notch of mouse wheel
      var sgn = event.deltaY / notchDeltaY;

      double size = 1;

      if (sgn > 0) {
        size = 1 / scaleFactor;
      } else if (sgn < 0) {
        size = scaleFactor;
      }

      switch (mouseOp) {
        case 'ZOOM':
          updateTbState(State2.scale, true);

          if (sgn > 0) {
            size = 1 / (Math.pow(scaleFactor, sgn));
          } else if (sgn < 0) {
            size = Math.pow(scaleFactor, -sgn) + 0.0;
          }

          if (cursorZoom && enablePan) {
            var scalePoint;

            if (camera is OrthographicCamera) {
              scalePoint = unprojectOnTbPlane(camera, event.clientX, event.clientY, domElement)
                  .applyQuaternion(camera.quaternion)
                  .multiplyScalar(1 / camera.zoom)
                  .add(_gizmos.position);
            } else if (camera is PerspectiveCamera) {
              scalePoint = unprojectOnTbPlane(camera, event.clientX, event.clientY, domElement)
                  .applyQuaternion(camera.quaternion)
                  .add(_gizmos.position);
            }

            applyTransformMatrix(scale(size, scalePoint));
          } else {
            applyTransformMatrix(scale(size, _gizmos.position));
          }

          if (_grid != null) {
            disposeGrid();
            drawGrid();
          }

          updateTbState(State2.idle, false);

          dispatchEvent(_changeEvent);
          dispatchEvent(_endEvent);

          break;

        case 'FOV':
          if (camera is PerspectiveCamera) {
            updateTbState(State2.fov, true);

            //Vertigo effect

            //	  fov / 2
            //		|\
            //		| \
            //		|  \
            //	x	|	\
            //		| 	 \
            //		| 	  \
            //		| _ _ _\
            //			y

            //check for iOs shift shortcut
            if (event.deltaX != 0) {
              sgn = event.deltaX / notchDeltaY;

              size = 1;

              if (sgn > 0) {
                size = 1 / (Math.pow(scaleFactor, sgn));
              } else if (sgn < 0) {
                size = Math.pow(scaleFactor, -sgn) + 0.0;
              }
            }

            _v3_1.setFromMatrixPosition(_cameraMatrixState);
            var x = _v3_1.distanceTo(_gizmos.position);
            var xNew = x / size; //distance between camera and gizmos if scale(size, scalepoint) would be performed

            //check min and max distance
            xNew = MathUtils.clamp(xNew, minDistance, maxDistance);

            var y = x * Math.tan(MathUtils.deg2rad * camera.fov * 0.5);

            //calculate new fov
            var newFov = MathUtils.rad2deg * (Math.atan(y / xNew) * 2);

            //check min and max fov
            if (newFov > maxFov) {
              newFov = maxFov;
            } else if (newFov < minFov) {
              newFov = minFov;
            }

            var newDistance = y / Math.tan(MathUtils.deg2rad * (newFov / 2));
            size = x / newDistance;

            setFov(newFov);
            applyTransformMatrix(scale(size, _gizmos.position, false));
          }

          if (_grid != null) {
            disposeGrid();
            drawGrid();
          }

          updateTbState(State2.idle, false);

          dispatchEvent(_changeEvent);
          dispatchEvent(_endEvent);

          break;
      }
    }
  }
}