How to select Fabric.js object programmatically

人走茶凉 提交于 2019-11-27 11:10:41

问题


I want to programmatically select Fabrics.js object. What do I have to do? For example, I am adding two objects like this:

var canvas = new fabric.Canvas('canvas');
canvas.add(new fabric.Rect({
    left: 100,
    top: 100,
    width: 75,
    height: 50,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 3,
    padding: 10
  }));

 canvas.add(new fabric.Circle({
    left: 200,
    top: 200,
    radius: 30,
    fill: 'gray',
    stroke: 'black',
    strokeWidth: 3
  }));

and I have two button while clicking the button named

  1. select rectangle
  2. select second object

While clicking the select rectangle button it should select the rectangle shape and while clicking the select second object button it should select the second object circle.

Here is the Jsfiddle for workaround.

I goggled and fed up, here I am seeking for some point how to start.

EDIT

I like to have the ID for each object, it should be possible to select the object using that ID, why I am asking this is, while using the collaborative things we can't tell surely all the connected nodes will have the item in same index, so unique id will be useful.


回答1:


Yes, you can set id for each item by adding the below code in all.js

In the fabric.js build version 1.3.0 : In the Object declaration add

 var object = {
   id:   this.id,
   remaining properties in all.js
  }

Now you can set the object id with:

canvas.getActiveObject().id=your id value;

You can retrieve the object id with :

Myid= canvas.getActiveObject().get('id');



回答2:


You want to use:

 canvas.setActiveObject(canvas.item(0));

In the buttons click event

The number corresponds to the order in which the object was added to the canvas.

See here:

http://jsfiddle.net/ThzXM/1/




回答3:


To find the fabric object number (item number) of the selected object use:

canvas.on({
    'object:selected': selectedObject
});

function selectedObject(e) {
    var id = canvas.getObjects().indexOf(e.target);
}

var id is the same number if you programmatically set the object as in Dan Brown's reply:

canvas.setActiveObject(canvas.item(id)); //id = 0, 1, 2 etc.



回答4:


add an id to your objects.

var canvas = new fabric.Canvas('canvas');

canvas.add(new fabric.Rect({
    id: 123,
    left: 100,
    top: 100,
    width: 75,
    height: 50,
    fill: 'green',
    stroke: 'black',
    strokeWidth: 3,
    padding: 10
}));

canvas.add(new fabric.Circle({
    id: 456,
    left: 200,
    top: 200,
    radius: 30,
    fill: 'gray',
    stroke: 'black',
    strokeWidth: 3
}));

function removeSpot(canvas, id) {
    canvas.forEachObject(function(obj) {
        if (obj.id && obj.id === id) canvas.remove(obj);
    });
}

// remove rect
removeSpot(canvas, 123);

// remove circle
removeSpot(canvas, 456);


来源:https://stackoverflow.com/questions/20096949/how-to-select-fabric-js-object-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!