Implementing Canvas Panning with Fabric.js

Core Implementation

Fabric.js canvas objects do not support panning functionality by default. This behavior can be implemented through custom event handling that tracks mouse movements and adjusts the viewport accordingly.

The dragging mechanism follows three fundamental steps:

  1. Mouse button press initiates the drag operation
  2. Mouse movement updates the canvas position
  3. Mouse button release terminates the drag sequence

During movement, the system calculates the cursor displacement and applies corresponding transformations to the canvas viewport.

Complete Example

<canvas id="canvas" width="500" height="500" style="border: 1px solid #ccc;"></canvas>

<script src="../js/fabric.js"></script>
<script>
  const workspace = new fabric.Canvas('canvas', {
    allowTouchScrolling: true
  });

  // Create sample objects
  const box = new fabric.Rect({
    width: 100,
    height: 100,
    left: 10,
    top: 10,
    fill: 'pink'
  });

  const shape = new fabric.Triangle({
    top: 100,
    left: 100,
    width: 80,
    height: 100,
    fill: 'blue'
  });

  workspace.add(box, shape);

  // Mouse press handler
  workspace.on('mouse:down', function (eventData) {
    const mouseEvent = eventData.e;
    if (mouseEvent.altKey === true) {
      this.dragActive = true;
      this.previousX = mouseEvent.clientX;
      this.previousY = mouseEvent.clientY;
    }
  });

  // Mouse movement handler
  workspace.on('mouse:move', function (eventData) {
    if (this.dragActive) {
      const currentEvent = eventData.e;
      const transformMatrix = this.viewportTransform;
      
      transformMatrix[4] += currentEvent.clientX - this.previousX;
      transformMatrix[5] += currentEvent.clientY - this.previousY;
      
      this.requestRenderAll();
      this.previousX = currentEvent.clientX;
      this.previousY = currentEvent.clientY;
    }
  });

  // Mouse release handler
  workspace.on('mouse:up', function () {
    this.setViewportTransform(this.viewportTransform);
    this.dragActive = false;
  });
</script>

Event Breakdown

Mouse Press Event

The mouse:down listener activates panning mode when the Alt key is held during mouse click. Three custom properties track the interaction state:

  • dragActive: Boolean flag indicating active panning
  • previousX: X-coordinate of the initial mouse position
  • previousY: Y-coordinate of the initial mouse position

Recording the starting coordinates enables calculation of relative movement distances for accurate viewport adjustment.

Mouse Movement Processing

The mouse:move handler executes only when dragActive is true. It accesses the canvas's viewportTransform property, which represents the 2D transformation matrix controlling the visible area.

Array indices 4 and 5 correspond to horizontal and vertical translation respectively. The difference between current and previous mouse positions determines the displacement applied to these matrix elements.

After updating the transformation matrix, requestRenderAll() refreshes the display. The coordinate tracking variables then update to the current mouse position, preparing for the next movement calculation.

Mouse Release Cleanup

Upon releasing the mouse button, setViewportTransform() applies the final transformation state and disables panning mode by setting dragActive to false.

Tags: fabricjs Canvas panning mouse-events viewport-transform

Posted on Wed, 08 Jul 2026 16:29:06 +0000 by choppsta