Skip to content

ViewportGizmo

A 3D camera orientation controller that provides a visual interface for manipulating the camera's viewing angle. The widget displays the current camera orientation and allows users to adjust the view directly by clicking or dragging.

Constructor

constructor( camera: PerspectiveCamera | OrthographicCamera, renderer: WebGLRenderer, options?: GizmoOptions )

  • cameraPerspectiveCamera | OrthographicCamera
    • The camera to be controlled by the gizmo.
  • rendererWebGLRenderer
    • The renderer used to render the scene.
  • optionsGizmoOptions (optional)
    • Configuration options for the gizmo.

Events

  • start — Triggered when a view change interaction begins.

    • This event occurs when:
      • Dragging starts
      • Clicking an axis initiates a view transition
  • change — Triggered during view changes.

    • This event occurs when:
      • Dragging updates the view
      • View transition animations are in progress
      • Any camera orientation updates happen
  • end — Triggered when a view change interaction ends.

    • This event occurs when:
      • Dragging stops
      • View transition animation completes

Properties

  • enabledboolean

    • Specifies if the gizmo is active and responsive to user input.
  • cameraOrthographicCamera | PerspectiveCamera

    • The camera controlled by this gizmo.
  • targetVector3

    • The point around which the camera rotates.
  • placementboolean

    • Sets and update the placement of the gizmo relative to its container.
  • animatedboolean

    • Specifies if view changes should be animated.
  • speednumber

    • Controls animation speed, where higher values result in faster animations.
  • animatingboolean (readonly)

    • True if the gizmo is currently animating view changes.

Methods

  • set( options?: GizmoOptions ): ViewportGizmo

    • Regenerates the gizmo with the new options.

      WARNING

      • Not recommended for use in real-time rendering or animation loops
      • Provides a way to completely rebuild the gizmo with new options
      • Can be computationally expensive, so use sparingly
  • render(): ViewportGizmo

    • Renders the gizmo within the viewport.
  • domUpdate(): ViewportGizmo

    • Updates the gizmo's DOM properties based on position and size in the document.
  • cameraUpdate(): ViewportGizmo

    • Updates the gizmo's orientation to match the current camera orientation.
  • update( controls?: boolean ): ViewportGizmo

    • Performs a full update of the gizmo, including DOM and camera-related updates.
  • attachControls( controls: OrbitControls ): void

    • Links OrbitControls with the gizmo, managing interaction states and updates.
  • detachControls(): void

    • Removes all event listeners and detaches controls if previously attached.
  • dispose(): void

    • Cleans up all resources, including geometries, materials, textures, and event listeners.

Interfaces

GizmoOptions

typescript
type GizmoOptions = {
  container?: HTMLElement | string;
  type?: "sphere" | "cube";
  size?: number;
  placement?:
    | "top-left"
    | "top-center"
    | "top-right"
    | "center-left"
    | "center-center"
    | "center-right"
    | "bottom-left"
    | "bottom-center"
    | "bottom-right";

  offset?: {
    left?: number;
    top?: number;
    right?: number;
    bottom?: number;
  };

  animated?: boolean;
  speed?: number;
  resolution?: number;
  lineWidth?: number;
  id?: string;
  className?: string;

  font?: {
    family?: string;
    weight?: string | number;
  };

  background?: {
    enabled?: boolean;
    color?: ColorRepresentation;
    opacity?: number;

    hover?: {
      color?: ColorRepresentation;
      opacity?: number;
    };
  };

  corners?: {
    enabled?: boolean;
    color?: ColorRepresentation;
    opacity?: number;
    scale?: number;
    radius?: number;
    smoothness?: number;
    hover?: {
      color?: ColorRepresentation;
      opacity?: number;
      scale?: number;
    };
  };

  edges?: {
    enabled?: boolean;
    color?: ColorRepresentation;
    opacity?: number;
    scale?: number;
    radius?: number;
    smoothness?: number;
    hover?: {
      color?: ColorRepresentation;
      opacity?: number;
      scale?: number;
    };
  };

  radius?: number;
  smoothness?: number;

  x?: GizmoAxisOptions;
  y?: GizmoAxisOptions;
  z?: GizmoAxisOptions;
  nx?: GizmoAxisOptions;
  ny?: GizmoAxisOptions;
  nz?: GizmoAxisOptions;

  right?: GizmoAxisOptions;
  top?: GizmoAxisOptions;
  front?: GizmoAxisOptions;
  left?: GizmoAxisOptions;
  bottom?: GizmoAxisOptions;
  back?: GizmoAxisOptions;
};

type GizmoAxisOptions = {
  enabled?: boolean;
  label?: string;
  opacity?: number;
  scale?: number;
  line?: boolean;
  color?: ColorRepresentation;
  labelColor?: ColorRepresentation;

  border?: {
    size: number;
    color: ColorRepresentation;
  };

  hover?: {
    color?: ColorRepresentation;
    labelColor?: ColorRepresentation;
    opacity?: number;
    scale?: number;
    border?: {
      size: number;
      color: ColorRepresentation;
    };
  };
};

Defines comprehensive configuration options for the ViewportGizmo. Each option customizes the appearance or behavior of the gizmo in the viewport.

  • containerHTMLElement | string

    • Specifies the parent container for the gizmo. Can be an HTML element or a CSS selector string.
  • type"sphere" | "cube"

    • Determines the gizmo configuration type. Defaults to "sphere".
  • sizenumber

    • Sets the size of the gizmo widget in pixels. Defaults to 128.
  • placementGizmoDomPlacement

    • Determines the position of the gizmo in the viewport. Valid values include:
      • "top-left"
      • "top-center"
      • "top-right"
      • "center-left"
      • "center-center"
      • "center-right"
      • "bottom-left"
      • "bottom-center"
      • "bottom-right"
  • offsetobject

    • Configures offset from container edges in pixels.
    • leftnumber
      • Offset from the left edge.
    • topnumber
      • Offset from the top edge.
    • rightnumber
      • Offset from the right edge.
    • bottomnumber
      • Offset from the bottom edge.
  • animatedboolean

    • Enables or disables animations for view changes. Defaults to true.
  • speednumber

    • Adjusts the animation speed for view transitions. Defaults to 1.
  • resolutionnumber

    • Adjusts the texture resolution. Defaults to 64 for sphere, 128 for cube.
  • lineWidthnumber

    • Sets the width of the axes lines in pixels.
  • idstring

    • Sets the HTML id attribute for the gizmo container element.
  • classNamestring

    • Sets the HTML class attribute for the gizmo container element.
  • fontobject

    • Configuration for the font used in axis labels.
    • familystring
      • Specifies the font family for the axis labels.
    • weightstring | number
      • Sets the font weight for axis labels.
  • backgroundobject

    • Configures the background sphere/cube.
    • enabledboolean
      • Enables or disables the background.
    • colorColorRepresentation
      • Sets the background color in normal state.
    • opacitynumber
      • Sets the background opacity in normal state.
    • hoverobject
      • colorColorRepresentation
        • Sets the background color when hovered.
      • opacitynumber
        • Sets the background opacity when hovered.
  • cornersobject

    • Configures corner indicators.
    • enabledboolean
      • Enables or disables corner indicators.
    • colorColorRepresentation
      • Sets the base color of corner indicators.
    • opacitynumber
      • Sets the opacity of corner indicators.
    • scalenumber
      • Sets the scale multiplier for corner indicators.
    • radiusnumber
      • Sets the radius of corner indicators.
    • smoothnessnumber
      • Controls the smoothness of corner indicators.
    • hoverobject
      • colorColorRepresentation
        • Sets the color of corner indicators when hovered.
      • opacitynumber
        • Sets the opacity of corner indicators when hovered.
      • scalenumber
        • Sets the scale of corner indicators when hovered.
  • edgesobject

    • Configures edge indicators.
    • enabledboolean
      • Enables or disables edge indicators.
    • colorColorRepresentation
      • Sets the base color of edge indicators.
    • opacitynumber
      • Sets the opacity of edge indicators.
    • scalenumber
      • Sets the scale multiplier for edge indicators.
    • radiusnumber
      • Sets the radius of edge indicators.
    • smoothnessnumber
      • Controls the smoothness of edge indicators.
    • hoverobject
      • colorColorRepresentation
        • Sets the color of edge indicators when hovered.
      • opacitynumber
        • Sets the opacity of edge indicators when hovered.
      • scalenumber
        • Sets the scale of edge indicators when hovered.
  • Axis Configurations

    • Configurations for x, y, z, nx, ny, nz
    • Alias names for cube mode: right, left, top, bottom, front, back
    • Each axis configuration supports:
      • enabledboolean
        • Toggles the axis visibility.
      • labelstring
        • Custom text label for the axis.
      • opacitynumber
        • Sets the axis opacity.
      • scalenumber
        • Sets the scale multiplier for the indicator size.
      • lineboolean
        • Toggles the axis line visibility.
      • colorColorRepresentation
        • Sets the axis indicator background color.
      • labelColorColorRepresentation
        • Sets the axis label color.
      • borderobject
        • sizenumber
          • Sets the border size around the axis indicator.
        • colorColorRepresentation
          • Sets the border color around the axis indicator.
      • hoverobject
        • colorColorRepresentation
          • Sets the fill color on hover.
        • labelColorColorRepresentation
          • Sets the label text color on hover.
        • opacitynumber
          • Sets the opacity when hovered.
        • scalenumber
          • Sets the indicator scale when hovered.
        • borderobject
          • sizenumber
            • Sets the hover border size.
          • colorColorRepresentation
            • Sets the hover border color.

ViewportGizmoEventMap

Defines custom events emitted by ViewportGizmo. This extends Object3DEventMap with additional events for gizmo interactions.

  • start — Triggered when a view change interaction begins.
  • change — Triggered during view changes.
  • end — Triggered when a view change interaction ends.