Skip to content

Mousewheel

Demonstration

Hold down the Command key and use the mouse wheel to zoom in/out of the canvas.

import './index.less'
import { createRoot } from 'react-dom/client'
import React from 'react'
import { Graph } from '@antv/x6'
import { Settings, State } from './settings.tsx'

export default class Example extends React.Component {
  private container: HTMLDivElement
  private graph: Graph

  componentDidMount() {
    this.initGraph({
      zoomAtMousePosition: true,
    })
  }

  initGraph(options: State) {
    if (this.graph) {
      this.graph.dispose()
    }

    this.graph = new Graph({
      container: this.container,
      height: 280,
      background: {
        color: '#F2F7FA',
      },
      mousewheel: {
        enabled: true,
        global: true,
        modifiers: ['ctrl', 'meta'],
        ...options,
      },
    })

    this.graph.addNode({
      x: 200,
      y: 100,
      width: 100,
      height: 40,
      label: 'Rect',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const source = this.graph.addNode({
      x: 32,
      y: 32,
      width: 100,
      height: 40,
      label: 'Hello',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const target = this.graph.addNode({
      shape: 'circle',
      x: 160,
      y: 180,
      width: 60,
      height: 60,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    this.graph.addEdge({
      source,
      target,
      attrs: {
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
        },
      },
    })

    this.graph.centerContent()
  }

  onSettingsChanged = (options: State) => {
    this.initGraph(options)
  }

  refContainer = (container: HTMLDivElement) => {
    this.container = container
  }

  render() {
    return (
      <div className="mousewheel-app">
        <div className="app-side">
          <Settings onChange={this.onSettingsChanged} />
        </div>
        <div className="app-content" ref={this.refContainer} />
      </div>
    )
  }
}

const root = createRoot(document.getElementById('container')!)
root.render(<Example />)
window.addEventListener('pagehide', () => root.unmount(), { once: true })

Configuration

You can use the mousewheel configuration to zoom in/out of the canvas, often used in combination with modifier keys. The usage is as follows:

const graph = new Graph({
  mousewheel: {
    enabled: true,
    modifiers: ['ctrl', 'meta'],
  },
})

Supported options are as follows:

interface MouseWheelOptions {
  enabled?: boolean
  global?: boolean
  factor?: number
  zoomAtMousePosition?: boolean
  modifiers?: string | ('alt' | 'ctrl' | 'meta' | 'shift')[] | null
  guard?: (this: Graph, e: WheelEvent) => boolean
}

enabled

Whether to enable mouse wheel zooming interaction.

factor

The zoom factor. Defaults to 1.2.

zoomAtMousePosition

Whether to zoom in/out at the mouse position. Defaults to true.

global

Whether to bind the wheel event globally. If set to true, the wheel event is bound to the Document, otherwise it is bound to the canvas container. Defaults to false.

modifiers

Modifier keys (alt, ctrl, meta, shift), which can be set to resolve conflicts between default wheel behavior and canvas zooming. Modifier keys support the following formats:

  • alt represents pressing the alt key.
  • [alt, ctrl] represents pressing either alt or ctrl.
  • alt|ctrl represents pressing either alt or ctrl.
  • alt&ctrl represents pressing both alt and ctrl simultaneously.
  • alt|ctrl&shift represents pressing both alt and shift simultaneously or both ctrl and shift simultaneously.

guard

Determines whether a wheel event should be handled, returning false to ignore the event.

new Graph({
  mousewheel: {
    enabled: true,
    guard(e: WheelEvent) {
      if (e.altKey) {
        // Ignore all wheel events when the alt key is pressed
        return false
      }
      return true
    },
  },
})

Methods

isMouseWheelEnabled()

isMouseWheelEnabled(): boolean

Returns whether mouse wheel zooming is enabled.

enableMouseWheel()

enableMouseWheel(): this

Enables mouse wheel zooming.

disableMouseWheel()

disableMouseWheel(): this

Disables mouse wheel zooming.

toggleMouseWheel(…)

toggleMouseWheel(enabled?: boolean): this

Toggles the enabled state of mouse wheel zooming.

Name Type Required Default Description
enabled boolean - Whether to enable mouse wheel zooming, toggles the state if not provided.
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38
Site search
Documentation and examplesType to start searching

Search documentation, APIs, and examples

↑↓Select↵OpenEscClose