0

There are multiple images in the canvas how to select one programmatically in fabric js

fabric.Image.fromURL(this.imageSrc, (img) => {
      let oImg = img.set({
        left: 0,
        top: 0,
        angle: 0,
      }).scale(1);
this.canvas.add(oImg).renderAll();

1 Answers1

0

You can assign an image to a variable like this:

var oImg;
fabric.Image.fromURL(this.imageSrc, (img) => {
  oImg = img.set({
    left: 0,
    top: 0,
    angle: 0
  });
  canvas.add(oImg).renderAll();
});

Since the image object is now stored as oImg, you can select it like this:

canvas.setActiveObject(oImg);

Or, you can select objects in the canvas based on their index like this:

var obj = canvas.item(0);
canvas.setActiveObject(obj);
melchiar
  • 2,782
  • 16
  • 27