How to select programmatically one image within set of images in Fabric.js rendered

425 views Asked by At

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

There are 1 answers

3
melchiar On

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);