How to implement canvas panning with Fabric.js


entities

I have a Fabric.js canvas and I want to achieve full canvas panning which is usually performed by a "manual" tool. This is when you press one of the mouse buttons, then move on the canvas while holding down the mouse button, and the visible part of the canvas changes accordingly.

You can see what I am trying to achieve in this video .

To implement this functionality I wrote the following code:

$(canvas.wrapperEl).on('mousemove', function(evt) {
    if (evt.button == 2) { // 2 is the right mouse button
        canvas.absolutePan({
            x: evt.clientX,
            y: evt.clientY
        });
    }
});

But it doesn't work. You can see what happened in this video .

How to modify the code in order:

  1. For panning to work like the first video?

  2. In order to have the event handler consume the event? Context menus should be avoided when the user presses or releases the right mouse button.

Michael Laslow

An easy way to pan the Fabric canvas in response to mouse movement is to calculate the cursor displacement between mouse events and pass it to relativePan.

Observe how we use the screenXsum property of the previous mouse event screenYto calculate the relative position of the current mouse event:

function startPan(event) {
  if (event.button != 2) {
    return;
  }
  var x0 = event.screenX,
      y0 = event.screenY;
  function continuePan(event) {
    var x = event.screenX,
        y = event.screenY;
    fc.relativePan({ x: x - x0, y: y - y0 });
    x0 = x;
    y0 = y;
  }
  function stopPan(event) {
    $(window).off('mousemove', continuePan);
    $(window).off('mouseup', stopPan);
  };
  $(window).mousemove(continuePan);
  $(window).mouseup(stopPan);
  $(window).contextmenu(cancelMenu);
};
function cancelMenu() {
  $(window).off('contextmenu', cancelMenu);
  return false;
}
$(canvasWrapper).mousedown(startPan);

We start panning mousedownand continue panning mousemove. In mouseup, we cancel the panning; we also cancel the mouseup-cancelling function itself.

Cancel the right-click menu (also known as the context menu) by going back false. Canceling the menu function also cancels itself. So if you then click outside the canvas wrapper, the context menu will work.

Here is the page that demonstrates this method:

http://michaellaszlo.com/so/fabric-pan/

You will see three images on the Fabric canvas (it may take a minute or two to load the images). You will be able to use standard Fabric functionality. You can left-click on images to move them, stretch them and rotate them. However, when you right-click in the canvas container, the entire Fabric canvas will be panned with the mouse.

Related


How to implement canvas panning with Fabric.js

Dmitrii Pisarenko: I have a Fabric.js canvas and I want to implement the full canvas panning that packages usually do with "manual" tools. This is when you press one of the mouse buttons, then move on the canvas while holding down the mouse button, and the vis

How to implement canvas panning with Fabric.js

Dmitrii Pisarenko: I have a Fabric.js canvas and I want to implement the full canvas panning that packages usually do with "manual" tools. This is when you press one of the mouse buttons, then move on the canvas while holding down the mouse button, and the vis

How to implement canvas panning with Fabric.js

Dmitrii Pisarenko: I have a Fabric.js canvas and I want to implement the full canvas panning that packages usually do with "manual" tools. This is when you press one of the mouse buttons, then move on the canvas while holding down the mouse button, and the vis

How to implement camera panning in 3dsMax?

BPL What math is necessary to achieve the camera pan effect used in 3ds max? In 3ds max, the distance between the cursor and the grid remains constant throughout the movement (mouse_down + mouse_motion + mouse_up). My naive and failed attempts have been trying

Canvas Rotation - Fabric js

Mutu I have created a canvas element and done the rotation function using fabric js. everything is normal. But I only see a set of dotted lines pointing to the center of the canvas in Chrome on Windows 7, and in some devices too, but not in all Windows 7 and c

How to change SVG color on canvas - Fabric.js

Nick Rameau I am creating an application with Fabricjs . I have to add an SVG file to the canvas and change the color every time the Minicolors input changes . I first made the browser display the SVG image as SVG code like this: $('img[src$=".svg"]').each(fun

How to trigger custom event on canvas in fabric.js?

Piyush Dholariya I want to register javascript events on canvas in fabric.js. How to trigger custom event on canvas? Peter Gerasimenko If you want to extend the fabricjs event system with custom events, it's simple: var canvas = new fabric.Canvas('c'); // list

How to draw Fabric js strokeRect in html5 canvas?

Graphics 123 I am using html5 canvas library Fabric js to draw shapes: Code:canvas.add(new fabric.Rect({top: 100, left: 100, width: 50, height: 50, fill: 'empty', stroke: 'white', strokeWidth: 1})); But it fills the middle area with black which I don't want. I

How to save image using canvas in fabric.js

Bhupendra Emerald I'm creating an app for t-shirt customization, in which I have put a canvas on an image using CSS, but the problem is saving the image as a canvas. toDataURLJust gives a part of the canvas area, but I want the whole image. There are other sol

How to change SVG color on canvas - Fabric.js

Nick Rameau I am creating an application with Fabricjs . I have to add an SVG file to the canvas and change the color every time the Minicolors input changes . I first made the browser display the SVG image as SVG code like this: $('img[src$=".svg"]').each(fun

How to change SVG color on canvas - Fabric.js

Nick Rameau I am creating an application with Fabricjs . I have to add an SVG file to the canvas and change the color every time the Minicolors input changes . I first made the browser display the SVG image as SVG code like this: $('img[src$=".svg"]').each(fun

How to change SVG color on canvas - Fabric.js

Nick Rameau I am creating an application with Fabricjs . I have to add an SVG file to the canvas and change the color every time the Minicolors input changes . I first made the browser display the SVG image as SVG code like this: $('img[src$=".svg"]').each(fun

How to display pdf on canvas using angular7 and fabric js

Anjali I want to display pdf as image on canvas using angular7 and fabric js I can't find any code to try in angular7 Anjali Finally I was able to fix this...I have used pdfjsLib and I have imported in my ts file (import * from 'pdfjs-dist/build/pdf' as pdfjsL

How to override canvas.add() method in Fabric js

Mayur kukadiya I'm working in an image editor where I want to add an image to a canvas using animation. I tried overriding the canvas.add() method, but the animation doesn't work. I am using Fabric js version 1.7.22. when i try to google it. I found a solution

How to set repeating background image of canvas in Fabric JS?

Nisha I want to set image as background image of canvas. This canvas.setBackgroundImagemethod is possible . But this doesn't repeat throughout the background. How to make it repeatable? I've seen demos where patterns are used to set repeating background images

How to trigger custom event on canvas in fabric.js?

Piyush Dholariya I want to register javascript events on canvas in fabric.js. How to trigger custom event on canvas? Peter Gerasimenko If you want to extend the fabricjs event system with custom events, it's simple: var canvas = new fabric.Canvas('c'); // list

How to remove image from Fabric.js canvas using button

explain I am new to canvas and Fabric.js. I followed some tutorials and now I can add the image to the canvas. But don't know how to remove it. by clicking a button HTML <div id="container"> <input type="file" id="imageLoader" name="imageLoader" /> <inpu

How to draw a line on canvas using fabric.js

username I am using fabric.js to draw a line on a canvas. Here is my code, but there is no output: $("#Line").click(function() { // alert("Line"); canvas.add(new fabric.Line([50, 100, 200, 200], { left: 170, top: 150, fill: 'red

How to set canvas border circle using fabric js in angular

jk123 How can I make my canvas circular so my objects/logos don't go beyond the circular shirt color, please see the attached picture that the canvas is square by default, what I want is the circular behavior of the canvas. I have tried using css but the behav

How to remove image from Fabric.js canvas using button

explain I am new to canvas and Fabric.js. I followed some tutorials and now I can add the image to the canvas. But don't know how to remove it. by clicking a button HTML <div id="container"> <input type="file" id="imageLoader" name="imageLoader" /> <inpu

How to save image using canvas in fabric.js

Bhupendra Emerald I'm creating an app for t-shirt customization, in which I have put a canvas on an image using CSS, but the problem is saving the image as a canvas. toDataURLJust gives a part of the canvas area, but I want the whole image. There are other sol