Skip to content

Panning

Demo

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({
      enabled: true,
      modifiers: [],
      eventTypes: ['leftMouseDown'],
    })
  }

  initGraph(options: any) {
    if (this.graph) {
      this.graph.dispose()
    }
    this.graph = new Graph({
      container: this.container,
      background: {
        color: '#F2F7FA',
      },
      panning: {
        ...options,
      },
    })

    this.graph.addNode({
      x: 320,
      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: 80,
      y: 50,
      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: 240,
      y: 200,
      width: 60,
      height: 60,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
        },
      },
    })

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

    this.graph.centerContent()
  }

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

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

  render() {
    return (
      <div className="panning-app">
        <div className="app-side">
          <Settings onChange={this.onSettingChanged} />
        </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

A regular canvas (without the Scroller plugin) can support panning by the panning option.

const graph = new Graph({
  panning: true, // If not provided, default is also true
})

// Equivalent to
const graph = new Graph({
  panning: {
    enabled: true,
  },
})

The supported options are as follows:

interface Options {
  enabled?: boolean
  modifiers?: ModifierKey
  eventTypes?: ('leftMouseDown' | 'rightMouseDown' | 'mouseWheel' | 'mouseWheelDown')[]
}

enabled

Whether to enable canvas panning interaction.

modifiers

Dragging may conflict with other operations, so you can set the modifiers parameter to specify a modifier key that needs to be pressed along with the mouse click to trigger canvas panning.

The type definition of ModifierKey is as follows:

type ModifierKey = string | ('alt' | 'ctrl' | 'meta' | 'shift' | 'space')[] | null

It supports the following forms:

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

eventTypes

The interaction types that trigger canvas panning. It supports four forms or their combinations:

  • leftMouseDown: Dragging by pressing the left mouse button
  • rightMouseDown: Dragging by pressing the right mouse button
  • mouseWheel: Dragging by scrolling the mouse wheel
  • mouseWheelDown: Dragging by pressing the mouse wheel

Methods

isPannable()

isPannable(): boolean

Returns whether canvas panning interaction is enabled.

enablePanning()

enablePanning(): this

Enables canvas panning.

disablePanning()

disablePanning(): this

Disables canvas panning.

togglePanning(…)

togglePanning(enabled?: boolean): this

Toggles the enabled state of canvas panning. The parameter is as follows:

Name Type Required Default Value Description
enabled boolean - Whether to enable canvas panning, defaults to toggling the enabled state.
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38
Site search
Documentation and examplesType to start searching

Search documentation, APIs, and examples

↑↓Select↵OpenEscClose