var canvas = new fabric.Canvas('c');
// Add some example shapes
var circle = new fabric.Circle({
radius: 20, fill: 'green', left: 100, top: 100
});
var triangle = new fabric.Triangle({
width: 20, height: 30, fill: 'blue', left: 50, top: 50
});
canvas.add(circle, triangle);
// Add some example text
var text1 = new fabric.Text('hello world', { left: 100, top: 100 });
var text2 = new fabric.Text('test', { left: 0, top: 0 });
canvas.add(text1, text2);
// Select all objects
function selectAllCanvasObjects(){
var objs = canvas.getObjects().filter(function(o) {
if (o.get('type') === 'text') {
return o.set('active', true);
}
});
var group = new fabric.Group(objs, {
originX: 'center',
originY: 'center'
});
canvas._activeObject = null;
canvas.setActiveGroup(group.setCoords()).renderAll();
}
$(document).on('click', '#change-btn', function(event){
selectAllCanvasObjects();
})
我的意图当我点击按钮时,它将选择画布中的所有文本对象,我得到了朋友的帮助并完成了它,但问题现在是我想要设置此功能按钮,但当我点击按钮多次按组位置跳转,如何使组位置保持在同一位置?
答案 0 :(得分:1)
这应该解决它(我将canvas.discardActiveGroup()
添加到您的函数顶部):
// Select all text objects
function selectAllCanvasObjects(){
canvas.discardActiveGroup();
var objs = canvas.getObjects('text').filter(function(o) {
return o.set('active', true);
});
var group = new fabric.Group(objs, {
originX: 'center',
originY: 'center'
});
canvas._activeObject = null;
canvas.setActiveGroup(group.setCoords()).renderAll();
}
(我还清理了getObjects()
因为你可以传递一个字符串来获取你的文本对象
此处您的JSFiddle已更新,https://jsfiddle.net/rekrah/9m2oxcj6/2/。
希望有所帮助,如果您有任何其他问题,请与我联系。